state tests fixed and clean up

This commit is contained in:
jonesmarvin8 2025-12-06 20:03:05 -05:00
parent ea27eeb929
commit 3a3c6f5507
4 changed files with 79 additions and 48 deletions

View File

@ -23,8 +23,8 @@ pub struct Account {
pub nonce: Nonce,
}
#[derive(Serialize, Deserialize, Clone, Default)]
#[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))]
#[derive(Serialize, Deserialize, Clone, Default, PartialEq)]
#[cfg_attr(any(feature = "host", test), derive(Debug, Eq))]
pub struct AccountWithMetadata {
pub account: Account,
pub is_authorized: bool,

View File

@ -12,8 +12,8 @@ pub struct ProgramInput<T> {
pub instruction: T,
}
#[derive(Serialize, Deserialize, Clone, Default)]
#[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))]
#[derive(Serialize, Deserialize, Clone, Default, PartialEq)]
#[cfg_attr(any(feature = "host", test), derive(Debug, Eq))]
pub struct ChainedCall {
pub program_id: ProgramId,
pub instruction_data: InstructionData,

View File

@ -652,7 +652,6 @@ fn add_liquidity(pre_states: &[AccountWithMetadata],
panic!("Vault A was not provided");
}
// TODO: need to check this one
if pool_def_data.liquidity_pool_id != pool_definition_lp.account_id {
panic!("LP definition mismatch");
}
@ -816,12 +815,10 @@ fn remove_liquidity(pre_states: &[AccountWithMetadata],
panic!("Pool is inactive");
}
// TODO: need to check this one
if pool_def_data.liquidity_pool_id != pool_definition_lp.account_id {
panic!("LP definition mismatch");
}
if vault_a.account_id != pool_def_data.vault_a_addr {
panic!("Vault A was not provided");
}
@ -955,7 +952,7 @@ mod tests {
vault_b_wrong_acc_id,
pool_lp_uninit,
pool_lp_init,
pool_lp_wrong_acc_id, //TODO use?
pool_lp_wrong_acc_id,
user_holding_lp_uninit,
user_holding_lp_init,
pool_definition_uninit,
@ -986,12 +983,12 @@ mod tests {
remove_min_amount_b,
remove_actual_a_successful,
remove_min_amount_b_low,
remove_min_amount_a_low, //TODO use?
remove_min_amount_a_low,
remove_amount_lp,
remove_amount_lp_1,
add_max_amount_a_low,
add_max_amount_b_low,
add_max_amount_b_high, //TODO use?
add_max_amount_b_high,
add_max_amount_a,
add_max_amount_b,
add_min_amount_lp,
@ -1835,7 +1832,7 @@ mod tests {
_ => panic!("Invalid selection"),
}
}
/*/
#[should_panic(expected = "Invalid number of input accounts")]
#[test]
fn test_call_new_pool_with_invalid_number_of_accounts_1() {
@ -2059,7 +2056,7 @@ mod tests {
assert!(chained_call_a == helper_chained_call_constructor(ChainedCallsEnum::cc_token_a_initialization));
assert!(chained_call_b == helper_chained_call_constructor(ChainedCallsEnum::cc_token_b_initialization));
}
*/
#[should_panic(expected = "Invalid number of input accounts")]
#[test]
fn test_call_remove_liquidity_with_invalid_number_of_accounts_1() {
@ -2190,6 +2187,25 @@ mod tests {
helper_balance_constructor(BalanceEnum::remove_min_amount_b)],
);
}
#[should_panic(expected = "LP definition mismatch")]
#[test]
fn test_call_remove_liquidity_lp_def_mismatch() {
let pre_states = vec![
helper_account_constructor(AccountEnum::pool_definition_init),
helper_account_constructor(AccountEnum::vault_a_init),
helper_account_constructor(AccountEnum::vault_b_init),
helper_account_constructor(AccountEnum::pool_lp_wrong_acc_id),
helper_account_constructor(AccountEnum::user_holding_a),
helper_account_constructor(AccountEnum::user_holding_b),
helper_account_constructor(AccountEnum::user_holding_lp_init),
];
let _post_states = remove_liquidity(&pre_states,
&[helper_balance_constructor(BalanceEnum::remove_amount_lp),
helper_balance_constructor(BalanceEnum::remove_min_amount_a),
helper_balance_constructor(BalanceEnum::remove_min_amount_b)],
);
}
#[should_panic(expected = "Invalid liquidity account provided")]
#[test]
@ -2484,6 +2500,25 @@ mod tests {
);
}
#[should_panic(expected = "LP definition mismatch")]
#[test]
fn test_call_add_liquidity_lp_def_mismatch() {
let pre_states = vec![
helper_account_constructor(AccountEnum::pool_definition_init),
helper_account_constructor(AccountEnum::vault_a_init),
helper_account_constructor(AccountEnum::vault_b_init),
helper_account_constructor(AccountEnum::pool_lp_wrong_acc_id),
helper_account_constructor(AccountEnum::user_holding_a),
helper_account_constructor(AccountEnum::user_holding_b),
helper_account_constructor(AccountEnum::user_holding_lp_init),
];
let _post_states = add_liquidity(&pre_states,
&[helper_balance_constructor(BalanceEnum::add_max_amount_a),
helper_balance_constructor(BalanceEnum::add_max_amount_b),
helper_balance_constructor(BalanceEnum::add_min_amount_lp),],
);
}
#[should_panic(expected = "Both max-balances must be nonzero")]
#[test]
fn test_call_add_liquidity_zero_balance_1() {
@ -2497,9 +2532,9 @@ mod tests {
helper_account_constructor(AccountEnum::user_holding_lp_init),
];
let _post_states = add_liquidity(&pre_states,
&[0,
helper_balance_constructor(BalanceEnum::add_max_amount_b),
helper_balance_constructor(BalanceEnum::add_min_amount_lp),],
&[helper_balance_constructor(BalanceEnum::add_min_amount_lp),
0,
helper_balance_constructor(BalanceEnum::add_max_amount_b),],
);
}
@ -2535,9 +2570,9 @@ mod tests {
helper_account_constructor(AccountEnum::user_holding_lp_init),
];
let _post_states = add_liquidity(&pre_states,
&[helper_balance_constructor(BalanceEnum::add_max_amount_a),
helper_balance_constructor(BalanceEnum::add_max_amount_b),
0],);
&[0,
helper_balance_constructor(BalanceEnum::add_max_amount_a),
helper_balance_constructor(BalanceEnum::add_max_amount_b),],);
}
#[should_panic(expected = "Vaults' balances must be at least the reserve amounts")]
@ -2685,9 +2720,9 @@ mod tests {
helper_account_constructor(AccountEnum::user_holding_lp_init),
];
let (post_states, chained_calls) = add_liquidity(&pre_states,
&[helper_balance_constructor(BalanceEnum::add_max_amount_a),
helper_balance_constructor(BalanceEnum::add_max_amount_b),
helper_balance_constructor(BalanceEnum::add_min_amount_lp),],
&[helper_balance_constructor(BalanceEnum::add_min_amount_lp),
helper_balance_constructor(BalanceEnum::add_max_amount_a),
helper_balance_constructor(BalanceEnum::add_max_amount_b),],
);
let pool_post = post_states[0].clone();

View File

@ -2353,27 +2353,13 @@ impl PoolDefinition {
}
enum AccountsEnum {
amm, //TODO
pool_definition_uninit, //TODO
pool_definition_diff_amm, //Should point to AMM
user_token_a_holding,
user_token_a_holding_auth,
user_token_b_holding,
user_token_b_holding_auth,
//TODO all below (unless noted)
user_token_lp_holding,
user_token_lp_holding_auth,
pool_definition_init,
user_token_a_holding_init,
user_token_b_holding_init,
user_token_lp_holding_init,
pool_definition_remove,
user_token_a_holding_remove,
user_token_b_holding_remove,
user_token_lp_holding_remove,
token_a_definition_acc,// added
token_b_definition_acc,//added
token_lp_definition_acc,//added
token_a_definition_acc,
token_b_definition_acc,
token_lp_definition_acc,
vault_a_init,
vault_b_init,
vault_a_swap_1,
@ -2389,8 +2375,8 @@ impl PoolDefinition {
vault_a_add,
vault_b_add,
user_token_a_holding_add,
user_token_b_holding_add,
user_token_lp_holding_add,
user_token_b_holding_add,
user_token_lp_holding_add,
pool_definition_add,
token_lp_definition_add,
}
@ -2484,8 +2470,8 @@ impl PoolDefinition {
BalancesEnum::vault_b_balance_swap_2 => 2_084,
BalancesEnum::user_token_a_holding_swap_2 => 9_000,
BalancesEnum::user_token_b_holding_swap_2 => 10_416,
BalancesEnum::vault_a_balance_add => 0,
BalancesEnum::vault_b_balance_add => 0,
BalancesEnum::vault_a_balance_add => 7_000,
BalancesEnum::vault_b_balance_add => 3_500,
BalancesEnum::user_token_a_holding_add => 8_000,
BalancesEnum::user_token_b_holding_add => 9_000,
BalancesEnum::user_token_lp_holding_add => 4_000,
@ -2517,7 +2503,7 @@ impl PoolDefinition {
&PublicKey::new_from_private_key(&helper_private_keys_constructor(PrivateKeysEnum::amm_key))),
IdEnum::pool_definition_id => AccountId::from(
&PublicKey::new_from_private_key(&helper_private_keys_constructor(PrivateKeysEnum::pool_definition_key))),
IdEnum::pool_definition_diff_id => AccountId::from(
IdEnum::pool_definition_diff_id => AccountId::from( //TODO delete?
&PublicKey::new_from_private_key(&helper_private_keys_constructor(PrivateKeysEnum::pool_definition_diff_key))),
IdEnum::vault_a_id => AccountId::from(
&PublicKey::new_from_private_key(&helper_private_keys_constructor(PrivateKeysEnum::vault_a_key))),
@ -2542,8 +2528,6 @@ impl PoolDefinition {
fn helper_account_constructor(selection: AccountsEnum) -> Account {
//TODO
match selection {
AccountsEnum::amm => panic!("TODO"),
AccountsEnum::pool_definition_uninit => panic!("TODO"),
AccountsEnum::user_token_a_holding => Account {
program_owner: Program::token().id(),
balance: 0u128,
@ -2845,16 +2829,28 @@ impl PoolDefinition {
data: TokenHolding::into_data(
TokenHolding{
account_type: 1u8,
definition_id: helper_id_constructor(IdEnum::token_b_definition_id),
balance: helper_balances_constructor(BalancesEnum::user_token_b_holding_add),
definition_id: helper_id_constructor(IdEnum::token_lp_definition_id),
balance: helper_balances_constructor(BalancesEnum::user_token_lp_holding_add),
}),
nonce: 0,
},
AccountsEnum::token_lp_definition_add => Account {
program_owner: Program::token().id(),
balance: 0u128,
data: TokenDefinition::into_data(
TokenDefinition {
account_type: 0u8,
name: [1u8;6],
total_supply: helper_balances_constructor(BalancesEnum::token_lp_supply_add)
}
),
nonce: 1,
},
_ => panic!("Invalid selection TODO1")
}
}
/*
/* TODO delete
let expected_pool = helper_account_constructor(AccountsEnum::pool_definition_add);
let expected_vault_a = helper_account_constructor(AccountsEnum::vault_a_add);
let expected_vault_b = helper_account_constructor(AccountsEnum::vault_b_add);