diff --git a/staking/Cargo.lock b/staking/Cargo.lock index 222f9cbd..11b98756 100644 --- a/staking/Cargo.lock +++ b/staking/Cargo.lock @@ -853,6 +853,10 @@ dependencies = [ "unreachable", ] +[[package]] +name = "common-utils" +version = "0.1.0" + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -1574,6 +1578,7 @@ dependencies = [ "anchor-spl", "bytemuck", "byteorder", + "common-utils", "litesvm", "publisher-caps", "pyth-staking-program", diff --git a/staking/Cargo.toml b/staking/Cargo.toml index 06653f19..30524f0a 100644 --- a/staking/Cargo.toml +++ b/staking/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ - "programs/*" + "programs/*", + "common-utils", ] resolver = "2" diff --git a/staking/common-utils/Cargo.toml b/staking/common-utils/Cargo.toml new file mode 100644 index 00000000..90c48049 --- /dev/null +++ b/staking/common-utils/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "common-utils" +version = "0.1.0" +edition = "2021" + +[features] +mock-clock = [] + +[dependencies] diff --git a/staking/programs/integrity-pool/src/utils/types.rs b/staking/common-utils/src/bool_array.rs similarity index 63% rename from staking/programs/integrity-pool/src/utils/types.rs rename to staking/common-utils/src/bool_array.rs index c48d1b6a..afc0497c 100644 --- a/staking/programs/integrity-pool/src/utils/types.rs +++ b/staking/common-utils/src/bool_array.rs @@ -1,12 +1,3 @@ -#[allow(non_camel_case_types)] -// It is used to store fractional numbers with 6 decimal places -// The number 6 is coming from the decimal places of the PYTH token -pub type frac64 = u64; - -pub const FRAC_64_MULTIPLIER: u64 = 1_000_000; -pub const FRAC_64_MULTIPLIER_U128: u128 = FRAC_64_MULTIPLIER as u128; - - pub struct BoolArray { pub data: Vec, } @@ -33,10 +24,7 @@ impl BoolArray { #[cfg(test)] mod tests { - use { - super::*, - publisher_caps::MAX_CAPS, - }; + use super::*; #[test] fn test_bool_array() { @@ -47,8 +35,8 @@ mod tests { assert!(arr.get(i)); } - let mut arr = BoolArray::new(MAX_CAPS); - for i in 0..MAX_CAPS { + let mut arr = BoolArray::new(1024); + for i in 0..1024 { assert!(!arr.get(i)); arr.set(i); assert!(arr.get(i)); diff --git a/staking/common-utils/src/frac64.rs b/staking/common-utils/src/frac64.rs new file mode 100644 index 00000000..21fc1495 --- /dev/null +++ b/staking/common-utils/src/frac64.rs @@ -0,0 +1,7 @@ +#[allow(non_camel_case_types)] +// It is used to store fractional numbers with 6 decimal places +// The number 6 is coming from the decimal places of the PYTH token +pub type frac64 = u64; + +pub const FRAC_64_MULTIPLIER: u64 = 1_000_000; +pub const FRAC_64_MULTIPLIER_U128: u128 = FRAC_64_MULTIPLIER as u128; diff --git a/staking/common-utils/src/lib.rs b/staking/common-utils/src/lib.rs new file mode 100644 index 00000000..d49b0c87 --- /dev/null +++ b/staking/common-utils/src/lib.rs @@ -0,0 +1,2 @@ +pub mod bool_array; +pub mod frac64; diff --git a/staking/programs/integrity-pool/Cargo.toml b/staking/programs/integrity-pool/Cargo.toml index 7a370861..a0054968 100644 --- a/staking/programs/integrity-pool/Cargo.toml +++ b/staking/programs/integrity-pool/Cargo.toml @@ -18,6 +18,7 @@ anchor-spl = {workspace = true} bytemuck = {version = "1.4.0", features = ["derive", "min_const_generics"]} pyth-staking-program = {path = "../staking", features = ["cpi"]} publisher-caps = {path = "../publisher-caps", features = ["no-entrypoint"]} +common-utils = {path = "../../common-utils"} [dev-dependencies] diff --git a/staking/programs/integrity-pool/src/lib.rs b/staking/programs/integrity-pool/src/lib.rs index e6a78b1e..d40480df 100644 --- a/staking/programs/integrity-pool/src/lib.rs +++ b/staking/programs/integrity-pool/src/lib.rs @@ -1,5 +1,6 @@ use { anchor_lang::prelude::*, + common_utils::frac64::frac64, context::*, error::IntegrityPoolError, staking::state::positions::TargetWithParameters, @@ -9,7 +10,6 @@ use { UNLOCKING_DURATION, }, constants::POOL_CONFIG, - types::frac64, }, }; diff --git a/staking/programs/integrity-pool/src/state/event.rs b/staking/programs/integrity-pool/src/state/event.rs index 2ff3cd4e..a39a640a 100644 --- a/staking/programs/integrity-pool/src/state/event.rs +++ b/staking/programs/integrity-pool/src/state/event.rs @@ -1,13 +1,11 @@ use { - crate::utils::{ - constants::MAX_PUBLISHERS, - types::frac64, - }, + crate::utils::constants::MAX_PUBLISHERS, anchor_lang::prelude::*, bytemuck::{ Pod, Zeroable, }, + common_utils::frac64::frac64, std::fmt::Debug, }; diff --git a/staking/programs/integrity-pool/src/state/pool.rs b/staking/programs/integrity-pool/src/state/pool.rs index 49546743..b7d857d2 100644 --- a/staking/programs/integrity-pool/src/state/pool.rs +++ b/staking/programs/integrity-pool/src/state/pool.rs @@ -12,11 +12,6 @@ use { MAX_EVENTS, MAX_PUBLISHERS, }, - types::{ - frac64, - BoolArray, - FRAC_64_MULTIPLIER_U128, - }, }, }, anchor_lang::prelude::*, @@ -25,6 +20,13 @@ use { Pod, Zeroable, }, + common_utils::{ + bool_array::BoolArray, + frac64::{ + frac64, + FRAC_64_MULTIPLIER_U128, + }, + }, publisher_caps::{ PublisherCaps, MAX_CAPS, @@ -427,8 +429,8 @@ impl PoolConfig { mod tests { use { super::*, - crate::utils::types::FRAC_64_MULTIPLIER, anchor_lang::Discriminator, + common_utils::frac64::FRAC_64_MULTIPLIER, publisher_caps::{ PublisherCap, MAX_CAPS, diff --git a/staking/programs/integrity-pool/src/state/slash.rs b/staking/programs/integrity-pool/src/state/slash.rs index 9877dc80..a80d539a 100644 --- a/staking/programs/integrity-pool/src/state/slash.rs +++ b/staking/programs/integrity-pool/src/state/slash.rs @@ -1,7 +1,7 @@ use { - crate::utils::types::frac64, anchor_lang::prelude::*, borsh::BorshSchema, + common_utils::frac64::frac64, }; #[account] diff --git a/staking/programs/integrity-pool/src/utils/mod.rs b/staking/programs/integrity-pool/src/utils/mod.rs index fb9e8cdc..a77dd20d 100644 --- a/staking/programs/integrity-pool/src/utils/mod.rs +++ b/staking/programs/integrity-pool/src/utils/mod.rs @@ -1,3 +1,2 @@ pub mod clock; pub mod constants; -pub mod types; diff --git a/staking/programs/integrity-pool/tests/claim.rs b/staking/programs/integrity-pool/tests/claim.rs index a7058faa..f703ebfe 100644 --- a/staking/programs/integrity-pool/tests/claim.rs +++ b/staking/programs/integrity-pool/tests/claim.rs @@ -2,7 +2,7 @@ use { anchor_lang::AccountDeserialize, - integrity_pool::utils::types::FRAC_64_MULTIPLIER, + common_utils::frac64::FRAC_64_MULTIPLIER, solana_sdk::{ signature::Keypair, signer::Signer, diff --git a/staking/programs/integrity-pool/tests/integrity_pool_slash.rs b/staking/programs/integrity-pool/tests/integrity_pool_slash.rs index 6c3cdcba..c212e133 100644 --- a/staking/programs/integrity-pool/tests/integrity_pool_slash.rs +++ b/staking/programs/integrity-pool/tests/integrity_pool_slash.rs @@ -1,6 +1,7 @@ use { anchor_lang::AccountDeserialize, anchor_spl::token::TokenAccount, + common_utils::frac64::FRAC_64_MULTIPLIER, integrity_pool::{ error::IntegrityPoolError, state::{ @@ -11,7 +12,6 @@ use { }, slash::SlashEvent, }, - utils::types::FRAC_64_MULTIPLIER, }, solana_sdk::{ program_error::ProgramError, diff --git a/staking/programs/integrity-pool/tests/staking_slash.rs b/staking/programs/integrity-pool/tests/staking_slash.rs index a3fd1285..fdc09927 100644 --- a/staking/programs/integrity-pool/tests/staking_slash.rs +++ b/staking/programs/integrity-pool/tests/staking_slash.rs @@ -2,7 +2,7 @@ use { crate::utils::account::fetch_account_data_bytemuck, anchor_lang::AccountDeserialize, anchor_spl::token::TokenAccount, - integrity_pool::utils::types::FRAC_64_MULTIPLIER, + common_utils::frac64::FRAC_64_MULTIPLIER, solana_sdk::{ signature::Keypair, signer::Signer, diff --git a/staking/programs/integrity-pool/tests/utils/constants.rs b/staking/programs/integrity-pool/tests/utils/constants.rs index f7ac1275..3e223334 100644 --- a/staking/programs/integrity-pool/tests/utils/constants.rs +++ b/staking/programs/integrity-pool/tests/utils/constants.rs @@ -1,4 +1,4 @@ -use integrity_pool::utils::types::{ +use common_utils::frac64::{ frac64, FRAC_64_MULTIPLIER, }; diff --git a/staking/programs/integrity-pool/tests/utils/integrity_pool/reward_program.rs b/staking/programs/integrity-pool/tests/utils/integrity_pool/reward_program.rs index 55bb002a..f8bbe7d0 100644 --- a/staking/programs/integrity-pool/tests/utils/integrity_pool/reward_program.rs +++ b/staking/programs/integrity-pool/tests/utils/integrity_pool/reward_program.rs @@ -5,7 +5,7 @@ use { get_associated_token_address, spl_associated_token_account, }, - integrity_pool::utils::types::FRAC_64_MULTIPLIER, + common_utils::frac64::FRAC_64_MULTIPLIER, litesvm::types::TransactionResult, solana_program::pubkey::Pubkey, solana_sdk::{ diff --git a/staking/programs/integrity-pool/tests/utils/integrity_pool/slash.rs b/staking/programs/integrity-pool/tests/utils/integrity_pool/slash.rs index c6399776..3a911eab 100644 --- a/staking/programs/integrity-pool/tests/utils/integrity_pool/slash.rs +++ b/staking/programs/integrity-pool/tests/utils/integrity_pool/slash.rs @@ -18,10 +18,8 @@ use { Key, ToAccountMetas, }, - integrity_pool::utils::{ - constants::SLASH_EVENT, - types::frac64, - }, + common_utils::frac64::frac64, + integrity_pool::utils::constants::SLASH_EVENT, litesvm::types::TransactionResult, solana_sdk::{ instruction::Instruction, diff --git a/staking/programs/integrity-pool/tests/utils/staking/create_position.rs b/staking/programs/integrity-pool/tests/utils/staking/create_position.rs index 8c417e3f..73b9d1a0 100644 --- a/staking/programs/integrity-pool/tests/utils/staking/create_position.rs +++ b/staking/programs/integrity-pool/tests/utils/staking/create_position.rs @@ -11,7 +11,7 @@ use { InstructionData, ToAccountMetas, }, - integrity_pool::utils::types::frac64, + common_utils::frac64::frac64, solana_sdk::{ instruction::Instruction, pubkey::Pubkey, diff --git a/staking/programs/integrity-pool/tests/utils/staking/slash.rs b/staking/programs/integrity-pool/tests/utils/staking/slash.rs index 0a3db835..27c798a3 100644 --- a/staking/programs/integrity-pool/tests/utils/staking/slash.rs +++ b/staking/programs/integrity-pool/tests/utils/staking/slash.rs @@ -12,7 +12,7 @@ use { InstructionData, ToAccountMetas, }, - integrity_pool::utils::types::frac64, + common_utils::frac64::frac64, litesvm::types::TransactionResult, solana_sdk::{ compute_budget::ComputeBudgetInstruction,