Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add common utils package #476

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions staking/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"programs/*"
"programs/*",
"common-utils",
]

resolver = "2"
Expand Down
9 changes: 9 additions & 0 deletions staking/common-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "common-utils"
version = "0.1.0"
edition = "2021"

[features]
mock-clock = []

[dependencies]
Original file line number Diff line number Diff line change
@@ -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<u8>,
}
Expand All @@ -33,10 +24,7 @@ impl BoolArray {

#[cfg(test)]
mod tests {
use {
super::*,
publisher_caps::MAX_CAPS,
};
use super::*;

#[test]
fn test_bool_array() {
Expand All @@ -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));
Expand Down
7 changes: 7 additions & 0 deletions staking/common-utils/src/frac64.rs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions staking/common-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod bool_array;
pub mod frac64;
1 change: 1 addition & 0 deletions staking/programs/integrity-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion staking/programs/integrity-pool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
anchor_lang::prelude::*,
common_utils::frac64::frac64,
context::*,
error::IntegrityPoolError,
staking::state::positions::TargetWithParameters,
Expand All @@ -9,7 +10,6 @@ use {
UNLOCKING_DURATION,
},
constants::POOL_CONFIG,
types::frac64,
},
};

Expand Down
6 changes: 2 additions & 4 deletions staking/programs/integrity-pool/src/state/event.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down
14 changes: 8 additions & 6 deletions staking/programs/integrity-pool/src/state/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ use {
MAX_EVENTS,
MAX_PUBLISHERS,
},
types::{
frac64,
BoolArray,
FRAC_64_MULTIPLIER_U128,
},
},
},
anchor_lang::prelude::*,
Expand All @@ -25,6 +20,13 @@ use {
Pod,
Zeroable,
},
common_utils::{
bool_array::BoolArray,
frac64::{
frac64,
FRAC_64_MULTIPLIER_U128,
},
},
publisher_caps::{
PublisherCaps,
MAX_CAPS,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion staking/programs/integrity-pool/src/state/slash.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::utils::types::frac64,
anchor_lang::prelude::*,
borsh::BorshSchema,
common_utils::frac64::frac64,
};

#[account]
Expand Down
1 change: 0 additions & 1 deletion staking/programs/integrity-pool/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod clock;
pub mod constants;
pub mod types;
2 changes: 1 addition & 1 deletion staking/programs/integrity-pool/tests/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
anchor_lang::AccountDeserialize,
anchor_spl::token::TokenAccount,
common_utils::frac64::FRAC_64_MULTIPLIER,
integrity_pool::{
error::IntegrityPoolError,
state::{
Expand All @@ -11,7 +12,6 @@ use {
},
slash::SlashEvent,
},
utils::types::FRAC_64_MULTIPLIER,
},
solana_sdk::{
program_error::ProgramError,
Expand Down
2 changes: 1 addition & 1 deletion staking/programs/integrity-pool/tests/staking_slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion staking/programs/integrity-pool/tests/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use integrity_pool::utils::types::{
use common_utils::frac64::{
frac64,
FRAC_64_MULTIPLIER,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
InstructionData,
ToAccountMetas,
},
integrity_pool::utils::types::frac64,
common_utils::frac64::frac64,
solana_sdk::{
instruction::Instruction,
pubkey::Pubkey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
InstructionData,
ToAccountMetas,
},
integrity_pool::utils::types::frac64,
common_utils::frac64::frac64,
litesvm::types::TransactionResult,
solana_sdk::{
compute_budget::ComputeBudgetInstruction,
Expand Down
Loading