Skip to content

Commit

Permalink
add state tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Dec 8, 2023
1 parent 05ac384 commit 3f64391
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
7 changes: 5 additions & 2 deletions programs/drift/src/state/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use crate::math::safe_unwrap::SafeUnwrap;
use crate::state::traits::Size;
use crate::{LAMPORTS_PER_SOL_U64, PERCENTAGE_PRECISION_U64};

#[cfg(test)]
mod tests;

#[account]
#[derive(Default)]
#[repr(C)]
Expand Down Expand Up @@ -78,7 +81,7 @@ impl State {
}

pub fn max_number_of_sub_accounts(&self) -> u64 {
if self.max_number_of_sub_accounts < 100 {
if self.max_number_of_sub_accounts <= 100 {
return self.max_number_of_sub_accounts as u64;
}
(self.max_number_of_sub_accounts as u64).saturating_mul(100)

Check warning on line 87 in programs/drift/src/state/state.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/state.rs#L87

Added line #L87 was not covered by tests
Expand All @@ -92,7 +95,7 @@ impl State {
let account_space_utilization: u64 = self
.number_of_sub_accounts
.safe_mul(PERCENTAGE_PRECISION_U64)?

Check warning on line 97 in programs/drift/src/state/state.rs

View check run for this annotation

Codecov / codecov/patch

programs/drift/src/state/state.rs#L96-L97

Added lines #L96 - L97 were not covered by tests
.safe_div(self.max_number_of_sub_accounts())?;
.safe_div(self.max_number_of_sub_accounts().max(1))?;

let init_fee: u64 = if account_space_utilization > target_utilization {
max_init_fee
Expand Down
60 changes: 60 additions & 0 deletions programs/drift/src/state/state/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
mod get_init_user_fee {
use crate::State;

#[test]
fn it_works() {
let state = State::default();
let init_user_fee = state.get_init_user_fee().unwrap();
assert_eq!(init_user_fee, 0);

let state = State {
max_initialize_user_fee: 1,
max_number_of_sub_accounts: 100,
number_of_sub_accounts: 80,
..State::default()
};

let init_user_fee = state.get_init_user_fee().unwrap();
assert_eq!(init_user_fee, 0);

let state = State {
max_initialize_user_fee: 1,
max_number_of_sub_accounts: 100,
number_of_sub_accounts: 81,
..State::default()
};

let init_user_fee = state.get_init_user_fee().unwrap();
assert_eq!(init_user_fee, 500000);

let state = State {
max_initialize_user_fee: 1,
max_number_of_sub_accounts: 100,
number_of_sub_accounts: 90,
..State::default()
};

let init_user_fee = state.get_init_user_fee().unwrap();
assert_eq!(init_user_fee, 5000000);

let state = State {
max_initialize_user_fee: 1,
max_number_of_sub_accounts: 100,
number_of_sub_accounts: 100,
..State::default()
};

let init_user_fee = state.get_init_user_fee().unwrap();
assert_eq!(init_user_fee, 10000000);

let state = State {
max_initialize_user_fee: 100,
max_number_of_sub_accounts: 100,
number_of_sub_accounts: 100,
..State::default()
};

let init_user_fee = state.get_init_user_fee().unwrap();
assert_eq!(init_user_fee, 1000000000);
}
}
2 changes: 1 addition & 1 deletion sdk/src/math/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function calculateInitUserFee(stateAccount: StateAccount): BN {
}

export function getMaxNumberOfSubAccounts(stateAccount: StateAccount): BN {
if (stateAccount.maxNumberOfSubAccounts < 100) {
if (stateAccount.maxNumberOfSubAccounts <= 100) {
return new BN(stateAccount.maxNumberOfSubAccounts);
}
return new BN(stateAccount.maxNumberOfSubAccounts).muln(100);
Expand Down

0 comments on commit 3f64391

Please sign in to comment.