diff --git a/artifacts/program_methods/amm.bin b/artifacts/program_methods/amm.bin index da0ed533..ab0cbe5f 100644 Binary files a/artifacts/program_methods/amm.bin and b/artifacts/program_methods/amm.bin differ diff --git a/artifacts/program_methods/authenticated_transfer.bin b/artifacts/program_methods/authenticated_transfer.bin index 056d6ff8..91400866 100644 Binary files a/artifacts/program_methods/authenticated_transfer.bin and b/artifacts/program_methods/authenticated_transfer.bin differ diff --git a/artifacts/program_methods/pinata.bin b/artifacts/program_methods/pinata.bin index 7c7c66a5..f27b1061 100644 Binary files a/artifacts/program_methods/pinata.bin and b/artifacts/program_methods/pinata.bin differ diff --git a/artifacts/program_methods/pinata_token.bin b/artifacts/program_methods/pinata_token.bin index d0073e59..b02c564d 100644 Binary files a/artifacts/program_methods/pinata_token.bin and b/artifacts/program_methods/pinata_token.bin differ diff --git a/artifacts/program_methods/privacy_preserving_circuit.bin b/artifacts/program_methods/privacy_preserving_circuit.bin index e5131ebe..aee7c136 100644 Binary files a/artifacts/program_methods/privacy_preserving_circuit.bin and b/artifacts/program_methods/privacy_preserving_circuit.bin differ diff --git a/artifacts/program_methods/token.bin b/artifacts/program_methods/token.bin index edd10086..5de3f363 100644 Binary files a/artifacts/program_methods/token.bin and b/artifacts/program_methods/token.bin differ diff --git a/artifacts/test_program_methods/burner.bin b/artifacts/test_program_methods/burner.bin index bba11c26..3af0656b 100644 Binary files a/artifacts/test_program_methods/burner.bin and b/artifacts/test_program_methods/burner.bin differ diff --git a/artifacts/test_program_methods/chain_caller.bin b/artifacts/test_program_methods/chain_caller.bin index d0b28ca6..fdd29bd9 100644 Binary files a/artifacts/test_program_methods/chain_caller.bin and b/artifacts/test_program_methods/chain_caller.bin differ diff --git a/artifacts/test_program_methods/changer_claimer.bin b/artifacts/test_program_methods/changer_claimer.bin index 1189fafd..139da2a4 100644 Binary files a/artifacts/test_program_methods/changer_claimer.bin and b/artifacts/test_program_methods/changer_claimer.bin differ diff --git a/artifacts/test_program_methods/claimer.bin b/artifacts/test_program_methods/claimer.bin index 459b4049..c92813a9 100644 Binary files a/artifacts/test_program_methods/claimer.bin and b/artifacts/test_program_methods/claimer.bin differ diff --git a/artifacts/test_program_methods/data_changer.bin b/artifacts/test_program_methods/data_changer.bin index 44ffa8cb..8878b168 100644 Binary files a/artifacts/test_program_methods/data_changer.bin and b/artifacts/test_program_methods/data_changer.bin differ diff --git a/artifacts/test_program_methods/extra_output.bin b/artifacts/test_program_methods/extra_output.bin index bfb0beda..4e57b792 100644 Binary files a/artifacts/test_program_methods/extra_output.bin and b/artifacts/test_program_methods/extra_output.bin differ diff --git a/artifacts/test_program_methods/malicious_authorization_changer.bin b/artifacts/test_program_methods/malicious_authorization_changer.bin index 961d3bd1..b9c707f0 100644 Binary files a/artifacts/test_program_methods/malicious_authorization_changer.bin and b/artifacts/test_program_methods/malicious_authorization_changer.bin differ diff --git a/artifacts/test_program_methods/minter.bin b/artifacts/test_program_methods/minter.bin index 7f59d988..1e464b89 100644 Binary files a/artifacts/test_program_methods/minter.bin and b/artifacts/test_program_methods/minter.bin differ diff --git a/artifacts/test_program_methods/missing_output.bin b/artifacts/test_program_methods/missing_output.bin index af956dfd..56547658 100644 Binary files a/artifacts/test_program_methods/missing_output.bin and b/artifacts/test_program_methods/missing_output.bin differ diff --git a/artifacts/test_program_methods/modified_transfer.bin b/artifacts/test_program_methods/modified_transfer.bin index 48b55139..a6a66d1f 100644 Binary files a/artifacts/test_program_methods/modified_transfer.bin and b/artifacts/test_program_methods/modified_transfer.bin differ diff --git a/artifacts/test_program_methods/nonce_changer.bin b/artifacts/test_program_methods/nonce_changer.bin index cdb931f9..75f117e3 100644 Binary files a/artifacts/test_program_methods/nonce_changer.bin and b/artifacts/test_program_methods/nonce_changer.bin differ diff --git a/artifacts/test_program_methods/noop.bin b/artifacts/test_program_methods/noop.bin index 6a90fcd4..aa9c8e84 100644 Binary files a/artifacts/test_program_methods/noop.bin and b/artifacts/test_program_methods/noop.bin differ diff --git a/artifacts/test_program_methods/program_owner_changer.bin b/artifacts/test_program_methods/program_owner_changer.bin index d85fb360..12fb4a5a 100644 Binary files a/artifacts/test_program_methods/program_owner_changer.bin and b/artifacts/test_program_methods/program_owner_changer.bin differ diff --git a/artifacts/test_program_methods/simple_balance_transfer.bin b/artifacts/test_program_methods/simple_balance_transfer.bin index 3e87f257..fa446158 100644 Binary files a/artifacts/test_program_methods/simple_balance_transfer.bin and b/artifacts/test_program_methods/simple_balance_transfer.bin differ diff --git a/programs/amm/Cargo.toml b/programs/amm/Cargo.toml index cee580af..4c3ded21 100644 --- a/programs/amm/Cargo.toml +++ b/programs/amm/Cargo.toml @@ -11,5 +11,4 @@ token_core.workspace = true amm_core.workspace = true [features] -with-nssa = ["nssa"] -test-utils = [] \ No newline at end of file +with-nssa = ["nssa"] \ No newline at end of file diff --git a/programs/amm/src/lib.rs b/programs/amm/src/lib.rs index 1219592c..923f99da 100644 --- a/programs/amm/src/lib.rs +++ b/programs/amm/src/lib.rs @@ -7,7 +7,5 @@ pub mod new_definition; pub mod remove; pub mod swap; -#[cfg(all(test, feature = "with-nssa"))] -mod full_tests; #[cfg(test)] mod tests; diff --git a/programs/amm/src/tests.rs b/programs/amm/src/tests.rs index 97561516..11161cf2 100644 --- a/programs/amm/src/tests.rs +++ b/programs/amm/src/tests.rs @@ -4,6 +4,10 @@ use amm_core::{ PoolDefinition, compute_liquidity_token_pda, compute_liquidity_token_pda_seed, compute_pool_pda, compute_vault_pda, compute_vault_pda_seed, }; +#[cfg(feature = "with-nssa")] +use nssa::{ + PrivateKey, PublicKey, PublicTransaction, V02State, program::Program, public_transaction, +}; use nssa_core::{ account::{Account, AccountId, AccountWithMetadata, Data}, program::{ChainedCall, ProgramId}, @@ -20,7 +24,30 @@ const AMM_PROGRAM_ID: ProgramId = [42; 8]; struct BalanceForTests; struct ChainedCallForTests; struct IdForTests; -struct AccountForTests; +struct AccountWithMetadataForTests; +#[cfg(feature = "with-nssa")] +struct PrivateKeysForTests; +#[cfg(feature = "with-nssa")] +struct IdForExeTests; +#[cfg(feature = "with-nssa")] +struct BalanceForExeTests; +#[cfg(feature = "with-nssa")] +struct AccountsForExeTests; + +#[cfg(feature = "with-nssa")] +impl PrivateKeysForTests { + fn user_token_a_key() -> PrivateKey { + PrivateKey::try_new([31; 32]).expect("Keys constructor expects valid private key") + } + + fn user_token_b_key() -> PrivateKey { + PrivateKey::try_new([32; 32]).expect("Keys constructor expects valid private key") + } + + fn user_token_lp_key() -> PrivateKey { + PrivateKey::try_new([33; 32]).expect("Keys constructor expects valid private key") + } +} impl BalanceForTests { fn vault_a_reserve_init() -> u128 { @@ -158,8 +185,8 @@ impl ChainedCallForTests { ChainedCall::new( TOKEN_PROGRAM_ID, vec![ - AccountForTests::user_holding_a(), - AccountForTests::vault_a_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::vault_a_init(), ], &token_core::Instruction::Transfer { amount_to_transfer: BalanceForTests::add_max_amount_a(), @@ -170,12 +197,12 @@ impl ChainedCallForTests { fn cc_swap_token_b_test_1() -> ChainedCall { let swap_amount: u128 = 166; - let mut vault_b_auth = AccountForTests::vault_b_init(); + let mut vault_b_auth = AccountWithMetadataForTests::vault_b_init(); vault_b_auth.is_authorized = true; ChainedCall::new( TOKEN_PROGRAM_ID, - vec![vault_b_auth, AccountForTests::user_holding_b()], + vec![vault_b_auth, AccountWithMetadataForTests::user_holding_b()], &token_core::Instruction::Transfer { amount_to_transfer: swap_amount, }, @@ -189,12 +216,12 @@ impl ChainedCallForTests { fn cc_swap_token_a_test_2() -> ChainedCall { let swap_amount: u128 = 285; - let mut vault_a_auth = AccountForTests::vault_a_init(); + let mut vault_a_auth = AccountWithMetadataForTests::vault_a_init(); vault_a_auth.is_authorized = true; ChainedCall::new( TOKEN_PROGRAM_ID, - vec![vault_a_auth, AccountForTests::user_holding_a()], + vec![vault_a_auth, AccountWithMetadataForTests::user_holding_a()], &token_core::Instruction::Transfer { amount_to_transfer: swap_amount, }, @@ -209,8 +236,8 @@ impl ChainedCallForTests { ChainedCall::new( TOKEN_PROGRAM_ID, vec![ - AccountForTests::user_holding_b(), - AccountForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::vault_b_init(), ], &token_core::Instruction::Transfer { amount_to_transfer: BalanceForTests::add_max_amount_b(), @@ -222,8 +249,8 @@ impl ChainedCallForTests { ChainedCall::new( TOKEN_PROGRAM_ID, vec![ - AccountForTests::user_holding_a(), - AccountForTests::vault_a_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::vault_a_init(), ], &token_core::Instruction::Transfer { amount_to_transfer: BalanceForTests::add_successful_amount_a(), @@ -235,8 +262,8 @@ impl ChainedCallForTests { ChainedCall::new( TOKEN_PROGRAM_ID, vec![ - AccountForTests::user_holding_b(), - AccountForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::vault_b_init(), ], &token_core::Instruction::Transfer { amount_to_transfer: BalanceForTests::add_successful_amount_b(), @@ -245,12 +272,15 @@ impl ChainedCallForTests { } fn cc_add_pool_lp() -> ChainedCall { - let mut pool_lp_auth = AccountForTests::pool_lp_init(); + let mut pool_lp_auth = AccountWithMetadataForTests::pool_lp_init(); pool_lp_auth.is_authorized = true; ChainedCall::new( TOKEN_PROGRAM_ID, - vec![pool_lp_auth, AccountForTests::user_holding_lp_init()], + vec![ + pool_lp_auth, + AccountWithMetadataForTests::user_holding_lp_init(), + ], &token_core::Instruction::Mint { amount_to_mint: 282, }, @@ -261,12 +291,12 @@ impl ChainedCallForTests { } fn cc_remove_token_a() -> ChainedCall { - let mut vault_a_auth = AccountForTests::vault_a_init(); + let mut vault_a_auth = AccountWithMetadataForTests::vault_a_init(); vault_a_auth.is_authorized = true; ChainedCall::new( TOKEN_PROGRAM_ID, - vec![vault_a_auth, AccountForTests::user_holding_a()], + vec![vault_a_auth, AccountWithMetadataForTests::user_holding_a()], &token_core::Instruction::Transfer { amount_to_transfer: BalanceForTests::remove_actual_a_successful(), }, @@ -278,12 +308,12 @@ impl ChainedCallForTests { } fn cc_remove_token_b() -> ChainedCall { - let mut vault_b_auth = AccountForTests::vault_b_init(); + let mut vault_b_auth = AccountWithMetadataForTests::vault_b_init(); vault_b_auth.is_authorized = true; ChainedCall::new( TOKEN_PROGRAM_ID, - vec![vault_b_auth, AccountForTests::user_holding_b()], + vec![vault_b_auth, AccountWithMetadataForTests::user_holding_b()], &token_core::Instruction::Transfer { amount_to_transfer: 70, }, @@ -295,12 +325,15 @@ impl ChainedCallForTests { } fn cc_remove_pool_lp() -> ChainedCall { - let mut pool_lp_auth = AccountForTests::pool_lp_init(); + let mut pool_lp_auth = AccountWithMetadataForTests::pool_lp_init(); pool_lp_auth.is_authorized = true; ChainedCall::new( TOKEN_PROGRAM_ID, - vec![pool_lp_auth, AccountForTests::user_holding_lp_init()], + vec![ + pool_lp_auth, + AccountWithMetadataForTests::user_holding_lp_init(), + ], &token_core::Instruction::Burn { amount_to_burn: BalanceForTests::remove_amount_lp(), }, @@ -314,8 +347,8 @@ impl ChainedCallForTests { ChainedCall::new( TOKEN_PROGRAM_ID, vec![ - AccountForTests::user_holding_a(), - AccountForTests::vault_a_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::vault_a_init(), ], &token_core::Instruction::Transfer { amount_to_transfer: BalanceForTests::add_successful_amount_a(), @@ -327,8 +360,8 @@ impl ChainedCallForTests { ChainedCall::new( TOKEN_PROGRAM_ID, vec![ - AccountForTests::user_holding_b(), - AccountForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::vault_b_init(), ], &token_core::Instruction::Transfer { amount_to_transfer: BalanceForTests::add_successful_amount_b(), @@ -340,8 +373,8 @@ impl ChainedCallForTests { ChainedCall::new( TOKEN_PROGRAM_ID, vec![ - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_lp_uninit(), ], &token_core::Instruction::Mint { amount_to_mint: BalanceForTests::lp_supply_init(), @@ -403,7 +436,7 @@ impl IdForTests { } } -impl AccountForTests { +impl AccountWithMetadataForTests { fn user_holding_a() -> AccountWithMetadata { AccountWithMetadata { account: Account { @@ -975,6 +1008,813 @@ impl AccountForTests { } } +#[cfg(feature = "with-nssa")] +impl BalanceForExeTests { + fn user_token_a_holding_init() -> u128 { + 10_000 + } + + fn user_token_b_holding_init() -> u128 { + 10_000 + } + + fn user_token_lp_holding_init() -> u128 { + 2_000 + } + + fn vault_a_balance_init() -> u128 { + 5_000 + } + + fn vault_b_balance_init() -> u128 { + 2_500 + } + + fn pool_lp_supply_init() -> u128 { + 5_000 + } + + fn token_a_supply() -> u128 { + 100_000 + } + + fn token_b_supply() -> u128 { + 100_000 + } + + fn token_lp_supply() -> u128 { + 5_000 + } + + fn remove_lp() -> u128 { + 1_000 + } + + fn remove_min_amount_a() -> u128 { + 500 + } + + fn remove_min_amount_b() -> u128 { + 500 + } + + fn add_min_amount_lp() -> u128 { + 1_000 + } + + fn add_max_amount_a() -> u128 { + 2_000 + } + + fn add_max_amount_b() -> u128 { + 1_000 + } + + fn swap_amount_in() -> u128 { + 1_000 + } + + fn swap_min_amount_out() -> u128 { + 200 + } + + fn vault_a_balance_swap_1() -> u128 { + 3_572 + } + + fn vault_b_balance_swap_1() -> u128 { + 3_500 + } + + fn user_token_a_holding_swap_1() -> u128 { + 11_428 + } + + fn user_token_b_holding_swap_1() -> u128 { + 9_000 + } + + fn vault_a_balance_swap_2() -> u128 { + 6_000 + } + + fn vault_b_balance_swap_2() -> u128 { + 2_084 + } + + fn user_token_a_holding_swap_2() -> u128 { + 9_000 + } + + fn user_token_b_holding_swap_2() -> u128 { + 10_416 + } + + fn vault_a_balance_add() -> u128 { + 7_000 + } + + fn vault_b_balance_add() -> u128 { + 3_500 + } + + fn user_token_a_holding_add() -> u128 { + 8_000 + } + + fn user_token_b_holding_add() -> u128 { + 9_000 + } + + fn user_token_lp_holding_add() -> u128 { + 4_000 + } + + fn token_lp_supply_add() -> u128 { + 7_000 + } + + fn vault_a_balance_remove() -> u128 { + 4_000 + } + + fn vault_b_balance_remove() -> u128 { + 2_000 + } + + fn user_token_a_holding_remove() -> u128 { + 11_000 + } + + fn user_token_b_holding_remove() -> u128 { + 10_500 + } + + fn user_token_lp_holding_remove() -> u128 { + 1_000 + } + + fn token_lp_supply_remove() -> u128 { + 4_000 + } + + fn user_token_a_holding_new_definition() -> u128 { + 5_000 + } + + fn user_token_b_holding_new_definition() -> u128 { + 7_500 + } + + fn lp_supply_init() -> u128 { + // isqrt(vault_a_balance_init * vault_b_balance_init) = isqrt(5_000 * 2_500) = 3535 + (BalanceForExeTests::vault_a_balance_init() * BalanceForExeTests::vault_b_balance_init()) + .isqrt() + } +} + +#[cfg(feature = "with-nssa")] +impl IdForExeTests { + fn pool_definition_id() -> AccountId { + amm_core::compute_pool_pda( + Program::amm().id(), + IdForExeTests::token_a_definition_id(), + IdForExeTests::token_b_definition_id(), + ) + } + + fn token_lp_definition_id() -> AccountId { + amm_core::compute_liquidity_token_pda( + Program::amm().id(), + IdForExeTests::pool_definition_id(), + ) + } + + fn token_a_definition_id() -> AccountId { + AccountId::new([3; 32]) + } + + fn token_b_definition_id() -> AccountId { + AccountId::new([4; 32]) + } + + fn user_token_a_id() -> AccountId { + AccountId::from(&PublicKey::new_from_private_key( + &PrivateKeysForTests::user_token_a_key(), + )) + } + + fn user_token_b_id() -> AccountId { + AccountId::from(&PublicKey::new_from_private_key( + &PrivateKeysForTests::user_token_b_key(), + )) + } + + fn user_token_lp_id() -> AccountId { + AccountId::from(&PublicKey::new_from_private_key( + &PrivateKeysForTests::user_token_lp_key(), + )) + } + + fn vault_a_id() -> AccountId { + amm_core::compute_vault_pda( + Program::amm().id(), + IdForExeTests::pool_definition_id(), + IdForExeTests::token_a_definition_id(), + ) + } + + fn vault_b_id() -> AccountId { + amm_core::compute_vault_pda( + Program::amm().id(), + IdForExeTests::pool_definition_id(), + IdForExeTests::token_b_definition_id(), + ) + } +} + +#[cfg(feature = "with-nssa")] +impl AccountsForExeTests { + fn user_token_a_holding() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::user_token_a_holding_init(), + }), + nonce: 0, + } + } + + fn user_token_b_holding() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::user_token_b_holding_init(), + }), + nonce: 0, + } + } + + fn pool_definition_init() -> Account { + Account { + program_owner: Program::amm().id(), + balance: 0u128, + data: Data::from(&PoolDefinition { + definition_token_a_id: IdForExeTests::token_a_definition_id(), + definition_token_b_id: IdForExeTests::token_b_definition_id(), + vault_a_id: IdForExeTests::vault_a_id(), + vault_b_id: IdForExeTests::vault_b_id(), + liquidity_pool_id: IdForExeTests::token_lp_definition_id(), + liquidity_pool_supply: BalanceForExeTests::pool_lp_supply_init(), + reserve_a: BalanceForExeTests::vault_a_balance_init(), + reserve_b: BalanceForExeTests::vault_b_balance_init(), + fees: 0u128, + active: true, + }), + nonce: 0, + } + } + + fn token_a_definition_account() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenDefinition::Fungible { + name: String::from("test"), + total_supply: BalanceForExeTests::token_a_supply(), + metadata_id: None, + }), + nonce: 0, + } + } + + fn token_b_definition_acc() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenDefinition::Fungible { + name: String::from("test"), + total_supply: BalanceForExeTests::token_b_supply(), + metadata_id: None, + }), + nonce: 0, + } + } + + fn token_lp_definition_acc() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenDefinition::Fungible { + name: String::from("LP Token"), + total_supply: BalanceForExeTests::token_lp_supply(), + metadata_id: None, + }), + nonce: 0, + } + } + + fn vault_a_init() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::vault_a_balance_init(), + }), + nonce: 0, + } + } + + fn vault_b_init() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::vault_b_balance_init(), + }), + nonce: 0, + } + } + + fn user_token_lp_holding() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_lp_definition_id(), + balance: BalanceForExeTests::user_token_lp_holding_init(), + }), + nonce: 0, + } + } + + fn vault_a_swap_1() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::vault_a_balance_swap_1(), + }), + nonce: 0, + } + } + + fn vault_b_swap_1() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::vault_b_balance_swap_1(), + }), + nonce: 0, + } + } + + fn pool_definition_swap_1() -> Account { + Account { + program_owner: Program::amm().id(), + balance: 0u128, + data: Data::from(&PoolDefinition { + definition_token_a_id: IdForExeTests::token_a_definition_id(), + definition_token_b_id: IdForExeTests::token_b_definition_id(), + vault_a_id: IdForExeTests::vault_a_id(), + vault_b_id: IdForExeTests::vault_b_id(), + liquidity_pool_id: IdForExeTests::token_lp_definition_id(), + liquidity_pool_supply: BalanceForExeTests::pool_lp_supply_init(), + reserve_a: BalanceForExeTests::vault_a_balance_swap_1(), + reserve_b: BalanceForExeTests::vault_b_balance_swap_1(), + fees: 0u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_a_holding_swap_1() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::user_token_a_holding_swap_1(), + }), + nonce: 0, + } + } + + fn user_token_b_holding_swap_1() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::user_token_b_holding_swap_1(), + }), + nonce: 1, + } + } + + fn vault_a_swap_2() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::vault_a_balance_swap_2(), + }), + nonce: 0, + } + } + + fn vault_b_swap_2() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::vault_b_balance_swap_2(), + }), + nonce: 0, + } + } + + fn pool_definition_swap_2() -> Account { + Account { + program_owner: Program::amm().id(), + balance: 0u128, + data: Data::from(&PoolDefinition { + definition_token_a_id: IdForExeTests::token_a_definition_id(), + definition_token_b_id: IdForExeTests::token_b_definition_id(), + vault_a_id: IdForExeTests::vault_a_id(), + vault_b_id: IdForExeTests::vault_b_id(), + liquidity_pool_id: IdForExeTests::token_lp_definition_id(), + liquidity_pool_supply: BalanceForExeTests::pool_lp_supply_init(), + reserve_a: BalanceForExeTests::vault_a_balance_swap_2(), + reserve_b: BalanceForExeTests::vault_b_balance_swap_2(), + fees: 0u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_a_holding_swap_2() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::user_token_a_holding_swap_2(), + }), + nonce: 1, + } + } + + fn user_token_b_holding_swap_2() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::user_token_b_holding_swap_2(), + }), + nonce: 0, + } + } + + fn vault_a_add() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::vault_a_balance_add(), + }), + nonce: 0, + } + } + + fn vault_b_add() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::vault_b_balance_add(), + }), + nonce: 0, + } + } + + fn pool_definition_add() -> Account { + Account { + program_owner: Program::amm().id(), + balance: 0u128, + data: Data::from(&PoolDefinition { + definition_token_a_id: IdForExeTests::token_a_definition_id(), + definition_token_b_id: IdForExeTests::token_b_definition_id(), + vault_a_id: IdForExeTests::vault_a_id(), + vault_b_id: IdForExeTests::vault_b_id(), + liquidity_pool_id: IdForExeTests::token_lp_definition_id(), + liquidity_pool_supply: BalanceForExeTests::token_lp_supply_add(), + reserve_a: BalanceForExeTests::vault_a_balance_add(), + reserve_b: BalanceForExeTests::vault_b_balance_add(), + fees: 0u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_a_holding_add() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::user_token_a_holding_add(), + }), + nonce: 1, + } + } + + fn user_token_b_holding_add() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::user_token_b_holding_add(), + }), + nonce: 1, + } + } + + fn user_token_lp_holding_add() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_lp_definition_id(), + balance: BalanceForExeTests::user_token_lp_holding_add(), + }), + nonce: 0, + } + } + + fn token_lp_definition_add() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenDefinition::Fungible { + name: String::from("LP Token"), + total_supply: BalanceForExeTests::token_lp_supply_add(), + metadata_id: None, + }), + nonce: 0, + } + } + + fn vault_a_remove() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::vault_a_balance_remove(), + }), + nonce: 0, + } + } + + fn vault_b_remove() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::vault_b_balance_remove(), + }), + nonce: 0, + } + } + + fn pool_definition_remove() -> Account { + Account { + program_owner: Program::amm().id(), + balance: 0u128, + data: Data::from(&PoolDefinition { + definition_token_a_id: IdForExeTests::token_a_definition_id(), + definition_token_b_id: IdForExeTests::token_b_definition_id(), + vault_a_id: IdForExeTests::vault_a_id(), + vault_b_id: IdForExeTests::vault_b_id(), + liquidity_pool_id: IdForExeTests::token_lp_definition_id(), + liquidity_pool_supply: BalanceForExeTests::token_lp_supply_remove(), + reserve_a: BalanceForExeTests::vault_a_balance_remove(), + reserve_b: BalanceForExeTests::vault_b_balance_remove(), + fees: 0u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_a_holding_remove() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::user_token_a_holding_remove(), + }), + nonce: 0, + } + } + + fn user_token_b_holding_remove() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::user_token_b_holding_remove(), + }), + nonce: 0, + } + } + + fn user_token_lp_holding_remove() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_lp_definition_id(), + balance: BalanceForExeTests::user_token_lp_holding_remove(), + }), + nonce: 1, + } + } + + fn token_lp_definition_remove() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenDefinition::Fungible { + name: String::from("LP Token"), + total_supply: BalanceForExeTests::token_lp_supply_remove(), + metadata_id: None, + }), + nonce: 0, + } + } + + fn token_lp_definition_init_inactive() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenDefinition::Fungible { + name: String::from("LP Token"), + total_supply: 0, + metadata_id: None, + }), + nonce: 0, + } + } + + fn vault_a_init_inactive() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: 0, + }), + nonce: 0, + } + } + + fn vault_b_init_inactive() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: 0, + }), + nonce: 0, + } + } + + fn pool_definition_inactive() -> Account { + Account { + program_owner: Program::amm().id(), + balance: 0u128, + data: Data::from(&PoolDefinition { + definition_token_a_id: IdForExeTests::token_a_definition_id(), + definition_token_b_id: IdForExeTests::token_b_definition_id(), + vault_a_id: IdForExeTests::vault_a_id(), + vault_b_id: IdForExeTests::vault_b_id(), + liquidity_pool_id: IdForExeTests::token_lp_definition_id(), + liquidity_pool_supply: 0, + reserve_a: 0, + reserve_b: 0, + fees: 0u128, + active: false, + }), + nonce: 0, + } + } + + fn user_token_a_holding_new_init() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_a_definition_id(), + balance: BalanceForExeTests::user_token_a_holding_new_definition(), + }), + nonce: 1, + } + } + + fn user_token_b_holding_new_init() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_b_definition_id(), + balance: BalanceForExeTests::user_token_b_holding_new_definition(), + }), + nonce: 1, + } + } + + fn user_token_lp_holding_new_init() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_lp_definition_id(), + balance: BalanceForExeTests::lp_supply_init(), + }), + nonce: 0, + } + } + + fn token_lp_definition_new_init() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenDefinition::Fungible { + name: String::from("LP Token"), + total_supply: BalanceForExeTests::lp_supply_init(), + metadata_id: None, + }), + nonce: 0, + } + } + + fn pool_definition_new_init() -> Account { + Account { + program_owner: Program::amm().id(), + balance: 0u128, + data: Data::from(&PoolDefinition { + definition_token_a_id: IdForExeTests::token_a_definition_id(), + definition_token_b_id: IdForExeTests::token_b_definition_id(), + vault_a_id: IdForExeTests::vault_a_id(), + vault_b_id: IdForExeTests::vault_b_id(), + liquidity_pool_id: IdForExeTests::token_lp_definition_id(), + liquidity_pool_supply: BalanceForExeTests::lp_supply_init(), + reserve_a: BalanceForExeTests::vault_a_balance_init(), + reserve_b: BalanceForExeTests::vault_b_balance_init(), + fees: 0u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_lp_holding_init_zero() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_lp_definition_id(), + balance: 0, + }), + nonce: 0, + } + } +} + #[test] fn test_pool_pda_produces_unique_id_for_token_pair() { assert!( @@ -994,13 +1834,13 @@ fn test_pool_pda_produces_unique_id_for_token_pair() { #[test] fn test_call_add_liquidity_vault_a_omitted() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_with_wrong_id(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_with_wrong_id(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a(), BalanceForTests::add_max_amount_b(), @@ -1011,13 +1851,13 @@ fn test_call_add_liquidity_vault_a_omitted() { #[test] fn test_call_add_liquidity_vault_b_omitted() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_with_wrong_id(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_with_wrong_id(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a(), BalanceForTests::add_max_amount_b(), @@ -1028,13 +1868,13 @@ fn test_call_add_liquidity_vault_b_omitted() { #[test] fn test_call_add_liquidity_lp_definition_mismatch() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_with_wrong_id(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_with_wrong_id(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a(), BalanceForTests::add_max_amount_b(), @@ -1045,13 +1885,13 @@ fn test_call_add_liquidity_lp_definition_mismatch() { #[test] fn test_call_add_liquidity_zero_balance_1() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), 0, BalanceForTests::add_max_amount_b(), @@ -1062,13 +1902,13 @@ fn test_call_add_liquidity_zero_balance_1() { #[test] fn test_call_add_liquidity_zero_balance_2() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), 0, BalanceForTests::add_max_amount_a(), @@ -1079,13 +1919,13 @@ fn test_call_add_liquidity_zero_balance_2() { #[test] fn test_call_add_liquidity_vault_insufficient_balance_1() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init_zero(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init_zero(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_max_amount_a()).unwrap(), BalanceForTests::add_max_amount_b(), BalanceForTests::add_min_amount_lp(), @@ -1096,13 +1936,13 @@ fn test_call_add_liquidity_vault_insufficient_balance_1() { #[test] fn test_call_add_liquidity_vault_insufficient_balance_2() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init_zero(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init_zero(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_max_amount_a()).unwrap(), BalanceForTests::add_max_amount_b(), BalanceForTests::add_min_amount_lp(), @@ -1113,13 +1953,13 @@ fn test_call_add_liquidity_vault_insufficient_balance_2() { #[test] fn test_call_add_liquidity_actual_amount_zero_1() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init_reserve_a_low(), - AccountForTests::vault_a_init_low(), - AccountForTests::vault_b_init_high(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init_reserve_a_low(), + AccountWithMetadataForTests::vault_a_init_low(), + AccountWithMetadataForTests::vault_b_init_high(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a(), BalanceForTests::add_max_amount_b(), @@ -1130,13 +1970,13 @@ fn test_call_add_liquidity_actual_amount_zero_1() { #[test] fn test_call_add_liquidity_actual_amount_zero_2() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init_reserve_b_low(), - AccountForTests::vault_a_init_high(), - AccountForTests::vault_b_init_low(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init_reserve_b_low(), + AccountWithMetadataForTests::vault_a_init_high(), + AccountWithMetadataForTests::vault_b_init_low(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a_low(), BalanceForTests::add_max_amount_b_low(), @@ -1147,13 +1987,13 @@ fn test_call_add_liquidity_actual_amount_zero_2() { #[test] fn test_call_add_liquidity_reserves_zero_1() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init_reserve_a_zero(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init_reserve_a_zero(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a(), BalanceForTests::add_max_amount_b(), @@ -1164,13 +2004,13 @@ fn test_call_add_liquidity_reserves_zero_1() { #[test] fn test_call_add_liquidity_reserves_zero_2() { let _post_states = add_liquidity( - AccountForTests::pool_definition_init_reserve_b_zero(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init_reserve_b_zero(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a(), BalanceForTests::add_max_amount_b(), @@ -1181,13 +2021,13 @@ fn test_call_add_liquidity_reserves_zero_2() { #[test] fn test_call_add_liquidity_payable_lp_zero() { let _post_states = add_liquidity( - AccountForTests::pool_definition_add_zero_lp(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_add_zero_lp(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a_low(), BalanceForTests::add_max_amount_b_low(), @@ -1197,13 +2037,13 @@ fn test_call_add_liquidity_payable_lp_zero() { #[test] fn test_call_add_liquidity_chained_call_successsful() { let (post_states, chained_calls) = add_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::add_min_amount_lp()).unwrap(), BalanceForTests::add_max_amount_a(), BalanceForTests::add_max_amount_b(), @@ -1211,7 +2051,10 @@ fn test_call_add_liquidity_chained_call_successsful() { let pool_post = post_states[0].clone(); - assert!(AccountForTests::pool_definition_add_successful().account == *pool_post.account()); + assert!( + AccountWithMetadataForTests::pool_definition_add_successful().account + == *pool_post.account() + ); let chained_call_lp = chained_calls[0].clone(); let chained_call_b = chained_calls[1].clone(); @@ -1226,13 +2069,13 @@ fn test_call_add_liquidity_chained_call_successsful() { #[test] fn test_call_remove_liquidity_vault_a_omitted() { let _post_states = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_with_wrong_id(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_with_wrong_id(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::remove_amount_lp()).unwrap(), BalanceForTests::remove_min_amount_a(), BalanceForTests::remove_min_amount_b(), @@ -1243,13 +2086,13 @@ fn test_call_remove_liquidity_vault_a_omitted() { #[test] fn test_call_remove_liquidity_vault_b_omitted() { let _post_states = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_with_wrong_id(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_with_wrong_id(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::remove_amount_lp()).unwrap(), BalanceForTests::remove_min_amount_a(), BalanceForTests::remove_min_amount_b(), @@ -1260,13 +2103,13 @@ fn test_call_remove_liquidity_vault_b_omitted() { #[test] fn test_call_remove_liquidity_lp_def_mismatch() { let _post_states = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_with_wrong_id(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_with_wrong_id(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::remove_amount_lp()).unwrap(), BalanceForTests::remove_min_amount_a(), BalanceForTests::remove_min_amount_b(), @@ -1277,14 +2120,15 @@ fn test_call_remove_liquidity_lp_def_mismatch() { #[test] fn test_call_remove_liquidity_insufficient_liquidity_amount() { let _post_states = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_a(), /* different token account than lp to create desired - * error */ + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_a(), /* different token account than lp to + * create desired + * error */ NonZero::new(BalanceForTests::remove_amount_lp()).unwrap(), BalanceForTests::remove_min_amount_a(), BalanceForTests::remove_min_amount_b(), @@ -1297,13 +2141,13 @@ fn test_call_remove_liquidity_insufficient_liquidity_amount() { #[test] fn test_call_remove_liquidity_insufficient_balance_1() { let _post_states = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::remove_amount_lp_1()).unwrap(), BalanceForTests::remove_min_amount_a(), BalanceForTests::remove_min_amount_b(), @@ -1316,13 +2160,13 @@ fn test_call_remove_liquidity_insufficient_balance_1() { #[test] fn test_call_remove_liquidity_insufficient_balance_2() { let _post_states = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::remove_amount_lp()).unwrap(), BalanceForTests::remove_min_amount_a(), BalanceForTests::remove_min_amount_b(), @@ -1333,13 +2177,13 @@ fn test_call_remove_liquidity_insufficient_balance_2() { #[test] fn test_call_remove_liquidity_min_bal_zero_1() { let _post_states = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::remove_amount_lp()).unwrap(), 0, BalanceForTests::remove_min_amount_b(), @@ -1350,13 +2194,13 @@ fn test_call_remove_liquidity_min_bal_zero_1() { #[test] fn test_call_remove_liquidity_min_bal_zero_2() { let _post_states = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::remove_amount_lp()).unwrap(), BalanceForTests::remove_min_amount_a(), 0, @@ -1366,13 +2210,13 @@ fn test_call_remove_liquidity_min_bal_zero_2() { #[test] fn test_call_remove_liquidity_chained_call_successful() { let (post_states, chained_calls) = remove_liquidity( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_init(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_init(), NonZero::new(BalanceForTests::remove_amount_lp()).unwrap(), BalanceForTests::remove_min_amount_a(), BalanceForTests::remove_min_amount_b_low(), @@ -1380,7 +2224,10 @@ fn test_call_remove_liquidity_chained_call_successful() { let pool_post = post_states[0].clone(); - assert!(AccountForTests::pool_definition_remove_successful().account == *pool_post.account()); + assert!( + AccountWithMetadataForTests::pool_definition_remove_successful().account + == *pool_post.account() + ); let chained_call_lp = chained_calls[0].clone(); let chained_call_b = chained_calls[1].clone(); @@ -1395,13 +2242,13 @@ fn test_call_remove_liquidity_chained_call_successful() { #[test] fn test_call_new_definition_with_zero_balance_1() { let _post_states = new_definition( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(0).expect("Balances must be nonzero"), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1412,13 +2259,13 @@ fn test_call_new_definition_with_zero_balance_1() { #[test] fn test_call_new_definition_with_zero_balance_2() { let _post_states = new_definition( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(0).expect("Balances must be nonzero"), AMM_PROGRAM_ID, @@ -1429,13 +2276,13 @@ fn test_call_new_definition_with_zero_balance_2() { #[test] fn test_call_new_definition_same_token_definition() { let _post_states = new_definition( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1446,13 +2293,13 @@ fn test_call_new_definition_same_token_definition() { #[test] fn test_call_new_definition_wrong_liquidity_id() { let _post_states = new_definition( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_with_wrong_id(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_with_wrong_id(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1463,13 +2310,13 @@ fn test_call_new_definition_wrong_liquidity_id() { #[test] fn test_call_new_definition_wrong_pool_id() { let _post_states = new_definition( - AccountForTests::pool_definition_with_wrong_id(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_with_wrong_id(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1480,13 +2327,13 @@ fn test_call_new_definition_wrong_pool_id() { #[test] fn test_call_new_definition_wrong_vault_id_1() { let _post_states = new_definition( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_with_wrong_id(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_with_wrong_id(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1497,13 +2344,13 @@ fn test_call_new_definition_wrong_vault_id_1() { #[test] fn test_call_new_definition_wrong_vault_id_2() { let _post_states = new_definition( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_with_wrong_id(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_with_wrong_id(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1514,13 +2361,13 @@ fn test_call_new_definition_wrong_vault_id_2() { #[test] fn test_call_new_definition_cannot_initialize_active_pool() { let _post_states = new_definition( - AccountForTests::pool_definition_active(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_active(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1531,13 +2378,13 @@ fn test_call_new_definition_cannot_initialize_active_pool() { #[test] fn test_call_new_definition_chained_call_successful() { let (post_states, chained_calls) = new_definition( - AccountForTests::pool_definition_active(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_active(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1545,7 +2392,10 @@ fn test_call_new_definition_chained_call_successful() { let pool_post = post_states[0].clone(); - assert!(AccountForTests::pool_definition_add_successful().account == *pool_post.account()); + assert!( + AccountWithMetadataForTests::pool_definition_add_successful().account + == *pool_post.account() + ); let chained_call_lp = chained_calls[0].clone(); let chained_call_b = chained_calls[1].clone(); @@ -1560,11 +2410,11 @@ fn test_call_new_definition_chained_call_successful() { #[test] fn test_call_swap_incorrect_token_type() { let _post_states = swap( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_a(), BalanceForTests::min_amount_out(), IdForTests::token_lp_definition_id(), @@ -1575,11 +2425,11 @@ fn test_call_swap_incorrect_token_type() { #[test] fn test_call_swap_vault_a_omitted() { let _post_states = swap( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_with_wrong_id(), - AccountForTests::vault_b_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_with_wrong_id(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_a(), BalanceForTests::min_amount_out(), IdForTests::token_a_definition_id(), @@ -1590,11 +2440,11 @@ fn test_call_swap_vault_a_omitted() { #[test] fn test_call_swap_vault_b_omitted() { let _post_states = swap( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_with_wrong_id(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_with_wrong_id(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_a(), BalanceForTests::min_amount_out(), IdForTests::token_a_definition_id(), @@ -1605,11 +2455,11 @@ fn test_call_swap_vault_b_omitted() { #[test] fn test_call_swap_reserves_vault_mismatch_1() { let _post_states = swap( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init_low(), - AccountForTests::vault_b_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init_low(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_a(), BalanceForTests::min_amount_out(), IdForTests::token_a_definition_id(), @@ -1620,11 +2470,11 @@ fn test_call_swap_reserves_vault_mismatch_1() { #[test] fn test_call_swap_reserves_vault_mismatch_2() { let _post_states = swap( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init_low(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init_low(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_a(), BalanceForTests::min_amount_out(), IdForTests::token_a_definition_id(), @@ -1635,11 +2485,11 @@ fn test_call_swap_reserves_vault_mismatch_2() { #[test] fn test_call_swap_ianctive() { let _post_states = swap( - AccountForTests::pool_definition_inactive(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_inactive(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_a(), BalanceForTests::min_amount_out(), IdForTests::token_a_definition_id(), @@ -1650,11 +2500,11 @@ fn test_call_swap_ianctive() { #[test] fn test_call_swap_below_min_out() { let _post_states = swap( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_a(), BalanceForTests::min_amount_out(), IdForTests::token_a_definition_id(), @@ -1664,11 +2514,11 @@ fn test_call_swap_below_min_out() { #[test] fn test_call_swap_chained_call_successful_1() { let (post_states, chained_calls) = swap( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_a(), BalanceForTests::add_max_amount_a_low(), IdForTests::token_a_definition_id(), @@ -1676,7 +2526,9 @@ fn test_call_swap_chained_call_successful_1() { let pool_post = post_states[0].clone(); - assert!(AccountForTests::pool_definition_swap_test_1().account == *pool_post.account()); + assert!( + AccountWithMetadataForTests::pool_definition_swap_test_1().account == *pool_post.account() + ); let chained_call_a = chained_calls[0].clone(); let chained_call_b = chained_calls[1].clone(); @@ -1694,11 +2546,11 @@ fn test_call_swap_chained_call_successful_1() { #[test] fn test_call_swap_chained_call_successful_2() { let (post_states, chained_calls) = swap( - AccountForTests::pool_definition_init(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), + AccountWithMetadataForTests::pool_definition_init(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), BalanceForTests::add_max_amount_b(), BalanceForTests::min_amount_out(), IdForTests::token_b_definition_id(), @@ -1706,7 +2558,9 @@ fn test_call_swap_chained_call_successful_2() { let pool_post = post_states[0].clone(); - assert!(AccountForTests::pool_definition_swap_test_2().account == *pool_post.account()); + assert!( + AccountWithMetadataForTests::pool_definition_swap_test_2().account == *pool_post.account() + ); let chained_call_a = chained_calls[1].clone(); let chained_call_b = chained_calls[0].clone(); @@ -1724,13 +2578,13 @@ fn test_call_swap_chained_call_successful_2() { #[test] fn test_new_definition_lp_asymmetric_amounts() { let (post_states, chained_calls) = new_definition( - AccountForTests::pool_definition_inactive(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_inactive(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(BalanceForTests::vault_a_reserve_init()).unwrap(), NonZero::new(BalanceForTests::vault_b_reserve_init()).unwrap(), AMM_PROGRAM_ID, @@ -1757,13 +2611,13 @@ fn test_new_definition_lp_symmetric_amounts() { assert_eq!(expected_lp, 100); let (post_states, chained_calls) = new_definition( - AccountForTests::pool_definition_inactive(), - AccountForTests::vault_a_init(), - AccountForTests::vault_b_init(), - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_a(), - AccountForTests::user_holding_b(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_definition_inactive(), + AccountWithMetadataForTests::vault_a_init(), + AccountWithMetadataForTests::vault_b_init(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_a(), + AccountWithMetadataForTests::user_holding_b(), + AccountWithMetadataForTests::user_holding_lp_uninit(), NonZero::new(token_a_amount).unwrap(), NonZero::new(token_b_amount).unwrap(), AMM_PROGRAM_ID, @@ -1777,8 +2631,8 @@ fn test_new_definition_lp_symmetric_amounts() { let expected_lp_call = ChainedCall::new( TOKEN_PROGRAM_ID, vec![ - AccountForTests::pool_lp_init(), - AccountForTests::user_holding_lp_uninit(), + AccountWithMetadataForTests::pool_lp_init(), + AccountWithMetadataForTests::user_holding_lp_uninit(), ], &token_core::Instruction::Mint { amount_to_mint: expected_lp, @@ -1790,3 +2644,535 @@ fn test_new_definition_lp_symmetric_amounts() { assert_eq!(chained_call_lp, expected_lp_call); } + +#[cfg(feature = "with-nssa")] +fn state_for_amm_tests() -> V02State { + let initial_data = []; + let mut state = V02State::new_with_genesis_accounts(&initial_data, &[]); + state.force_insert_account( + IdForExeTests::pool_definition_id(), + AccountsForExeTests::pool_definition_init(), + ); + state.force_insert_account( + IdForExeTests::token_a_definition_id(), + AccountsForExeTests::token_a_definition_account(), + ); + state.force_insert_account( + IdForExeTests::token_b_definition_id(), + AccountsForExeTests::token_b_definition_acc(), + ); + state.force_insert_account( + IdForExeTests::token_lp_definition_id(), + AccountsForExeTests::token_lp_definition_acc(), + ); + state.force_insert_account( + IdForExeTests::user_token_a_id(), + AccountsForExeTests::user_token_a_holding(), + ); + state.force_insert_account( + IdForExeTests::user_token_b_id(), + AccountsForExeTests::user_token_b_holding(), + ); + state.force_insert_account( + IdForExeTests::user_token_lp_id(), + AccountsForExeTests::user_token_lp_holding(), + ); + state.force_insert_account( + IdForExeTests::vault_a_id(), + AccountsForExeTests::vault_a_init(), + ); + state.force_insert_account( + IdForExeTests::vault_b_id(), + AccountsForExeTests::vault_b_init(), + ); + + state +} + +#[cfg(feature = "with-nssa")] +fn state_for_amm_tests_with_new_def() -> V02State { + let initial_data = []; + let mut state = V02State::new_with_genesis_accounts(&initial_data, &[]); + state.force_insert_account( + IdForExeTests::token_a_definition_id(), + AccountsForExeTests::token_a_definition_account(), + ); + state.force_insert_account( + IdForExeTests::token_b_definition_id(), + AccountsForExeTests::token_b_definition_acc(), + ); + state.force_insert_account( + IdForExeTests::user_token_a_id(), + AccountsForExeTests::user_token_a_holding(), + ); + state.force_insert_account( + IdForExeTests::user_token_b_id(), + AccountsForExeTests::user_token_b_holding(), + ); + state +} + +#[cfg(feature = "with-nssa")] +#[test] +fn test_simple_amm_remove() { + let mut state = state_for_amm_tests(); + + let instruction = amm_core::Instruction::RemoveLiquidity { + remove_liquidity_amount: BalanceForExeTests::remove_lp(), + min_amount_to_remove_token_a: BalanceForExeTests::remove_min_amount_a(), + min_amount_to_remove_token_b: BalanceForExeTests::remove_min_amount_b(), + }; + + let message = public_transaction::Message::try_new( + Program::amm().id(), + vec![ + IdForExeTests::pool_definition_id(), + IdForExeTests::vault_a_id(), + IdForExeTests::vault_b_id(), + IdForExeTests::token_lp_definition_id(), + IdForExeTests::user_token_a_id(), + IdForExeTests::user_token_b_id(), + IdForExeTests::user_token_lp_id(), + ], + vec![0], + instruction, + ) + .unwrap(); + + let witness_set = public_transaction::WitnessSet::for_message( + &message, + &[&PrivateKeysForTests::user_token_lp_key()], + ); + + let tx = PublicTransaction::new(message, witness_set); + state.transition_from_public_transaction(&tx).unwrap(); + + let pool_post = state.get_account_by_id(IdForExeTests::pool_definition_id()); + let vault_a_post = state.get_account_by_id(IdForExeTests::vault_a_id()); + let vault_b_post = state.get_account_by_id(IdForExeTests::vault_b_id()); + let token_lp_post = state.get_account_by_id(IdForExeTests::token_lp_definition_id()); + let user_token_a_post = state.get_account_by_id(IdForExeTests::user_token_a_id()); + let user_token_b_post = state.get_account_by_id(IdForExeTests::user_token_b_id()); + let user_token_lp_post = state.get_account_by_id(IdForExeTests::user_token_lp_id()); + + let expected_pool = AccountsForExeTests::pool_definition_remove(); + let expected_vault_a = AccountsForExeTests::vault_a_remove(); + let expected_vault_b = AccountsForExeTests::vault_b_remove(); + let expected_token_lp = AccountsForExeTests::token_lp_definition_remove(); + let expected_user_token_a = AccountsForExeTests::user_token_a_holding_remove(); + let expected_user_token_b = AccountsForExeTests::user_token_b_holding_remove(); + let expected_user_token_lp = AccountsForExeTests::user_token_lp_holding_remove(); + + assert_eq!(pool_post, expected_pool); + assert_eq!(vault_a_post, expected_vault_a); + assert_eq!(vault_b_post, expected_vault_b); + assert_eq!(token_lp_post, expected_token_lp); + assert_eq!(user_token_a_post, expected_user_token_a); + assert_eq!(user_token_b_post, expected_user_token_b); + assert_eq!(user_token_lp_post, expected_user_token_lp); +} + +#[cfg(feature = "with-nssa")] +#[test] +fn test_simple_amm_new_definition_inactive_initialized_pool_and_uninit_user_lp() { + let mut state = state_for_amm_tests_with_new_def(); + + // Uninitialized in constructor + state.force_insert_account( + IdForExeTests::vault_a_id(), + AccountsForExeTests::vault_a_init_inactive(), + ); + state.force_insert_account( + IdForExeTests::vault_b_id(), + AccountsForExeTests::vault_b_init_inactive(), + ); + state.force_insert_account( + IdForExeTests::pool_definition_id(), + AccountsForExeTests::pool_definition_inactive(), + ); + state.force_insert_account( + IdForExeTests::token_lp_definition_id(), + AccountsForExeTests::token_lp_definition_init_inactive(), + ); + + let instruction = amm_core::Instruction::NewDefinition { + token_a_amount: BalanceForExeTests::vault_a_balance_init(), + token_b_amount: BalanceForExeTests::vault_b_balance_init(), + amm_program_id: Program::amm().id(), + }; + + let message = public_transaction::Message::try_new( + Program::amm().id(), + vec![ + IdForExeTests::pool_definition_id(), + IdForExeTests::vault_a_id(), + IdForExeTests::vault_b_id(), + IdForExeTests::token_lp_definition_id(), + IdForExeTests::user_token_a_id(), + IdForExeTests::user_token_b_id(), + IdForExeTests::user_token_lp_id(), + ], + vec![0, 0], + instruction, + ) + .unwrap(); + + let witness_set = public_transaction::WitnessSet::for_message( + &message, + &[ + &PrivateKeysForTests::user_token_a_key(), + &PrivateKeysForTests::user_token_b_key(), + ], + ); + + let tx = PublicTransaction::new(message, witness_set); + state.transition_from_public_transaction(&tx).unwrap(); + + let pool_post = state.get_account_by_id(IdForExeTests::pool_definition_id()); + let vault_a_post = state.get_account_by_id(IdForExeTests::vault_a_id()); + let vault_b_post = state.get_account_by_id(IdForExeTests::vault_b_id()); + let token_lp_post = state.get_account_by_id(IdForExeTests::token_lp_definition_id()); + let user_token_a_post = state.get_account_by_id(IdForExeTests::user_token_a_id()); + let user_token_b_post = state.get_account_by_id(IdForExeTests::user_token_b_id()); + let user_token_lp_post = state.get_account_by_id(IdForExeTests::user_token_lp_id()); + + let expected_pool = AccountsForExeTests::pool_definition_new_init(); + let expected_vault_a = AccountsForExeTests::vault_a_init(); + let expected_vault_b = AccountsForExeTests::vault_b_init(); + let expected_token_lp = AccountsForExeTests::token_lp_definition_new_init(); + let expected_user_token_a = AccountsForExeTests::user_token_a_holding_new_init(); + let expected_user_token_b = AccountsForExeTests::user_token_b_holding_new_init(); + let expected_user_token_lp = AccountsForExeTests::user_token_lp_holding_new_init(); + + assert_eq!(pool_post, expected_pool); + assert_eq!(vault_a_post, expected_vault_a); + assert_eq!(vault_b_post, expected_vault_b); + assert_eq!(token_lp_post, expected_token_lp); + assert_eq!(user_token_a_post, expected_user_token_a); + assert_eq!(user_token_b_post, expected_user_token_b); + assert_eq!(user_token_lp_post, expected_user_token_lp); +} + +#[cfg(feature = "with-nssa")] +#[test] +fn test_simple_amm_new_definition_inactive_initialized_pool_init_user_lp() { + let mut state = state_for_amm_tests_with_new_def(); + + // Uninitialized in constructor + state.force_insert_account( + IdForExeTests::vault_a_id(), + AccountsForExeTests::vault_a_init_inactive(), + ); + state.force_insert_account( + IdForExeTests::vault_b_id(), + AccountsForExeTests::vault_b_init_inactive(), + ); + state.force_insert_account( + IdForExeTests::pool_definition_id(), + AccountsForExeTests::pool_definition_inactive(), + ); + state.force_insert_account( + IdForExeTests::token_lp_definition_id(), + AccountsForExeTests::token_lp_definition_init_inactive(), + ); + state.force_insert_account( + IdForExeTests::user_token_lp_id(), + AccountsForExeTests::user_token_lp_holding_init_zero(), + ); + + let instruction = amm_core::Instruction::NewDefinition { + token_a_amount: BalanceForExeTests::vault_a_balance_init(), + token_b_amount: BalanceForExeTests::vault_b_balance_init(), + amm_program_id: Program::amm().id(), + }; + + let message = public_transaction::Message::try_new( + Program::amm().id(), + vec![ + IdForExeTests::pool_definition_id(), + IdForExeTests::vault_a_id(), + IdForExeTests::vault_b_id(), + IdForExeTests::token_lp_definition_id(), + IdForExeTests::user_token_a_id(), + IdForExeTests::user_token_b_id(), + IdForExeTests::user_token_lp_id(), + ], + vec![0, 0], + instruction, + ) + .unwrap(); + + let witness_set = public_transaction::WitnessSet::for_message( + &message, + &[ + &PrivateKeysForTests::user_token_a_key(), + &PrivateKeysForTests::user_token_b_key(), + ], + ); + + let tx = PublicTransaction::new(message, witness_set); + state.transition_from_public_transaction(&tx).unwrap(); + + let pool_post = state.get_account_by_id(IdForExeTests::pool_definition_id()); + let vault_a_post = state.get_account_by_id(IdForExeTests::vault_a_id()); + let vault_b_post = state.get_account_by_id(IdForExeTests::vault_b_id()); + let token_lp_post = state.get_account_by_id(IdForExeTests::token_lp_definition_id()); + let user_token_a_post = state.get_account_by_id(IdForExeTests::user_token_a_id()); + let user_token_b_post = state.get_account_by_id(IdForExeTests::user_token_b_id()); + let user_token_lp_post = state.get_account_by_id(IdForExeTests::user_token_lp_id()); + + let expected_pool = AccountsForExeTests::pool_definition_new_init(); + let expected_vault_a = AccountsForExeTests::vault_a_init(); + let expected_vault_b = AccountsForExeTests::vault_b_init(); + let expected_token_lp = AccountsForExeTests::token_lp_definition_new_init(); + let expected_user_token_a = AccountsForExeTests::user_token_a_holding_new_init(); + let expected_user_token_b = AccountsForExeTests::user_token_b_holding_new_init(); + let expected_user_token_lp = AccountsForExeTests::user_token_lp_holding_new_init(); + + assert_eq!(pool_post, expected_pool); + assert_eq!(vault_a_post, expected_vault_a); + assert_eq!(vault_b_post, expected_vault_b); + assert_eq!(token_lp_post, expected_token_lp); + assert_eq!(user_token_a_post, expected_user_token_a); + assert_eq!(user_token_b_post, expected_user_token_b); + assert_eq!(user_token_lp_post, expected_user_token_lp); +} + +#[cfg(feature = "with-nssa")] +#[test] +fn test_simple_amm_new_definition_uninitialized_pool() { + let mut state = state_for_amm_tests_with_new_def(); + + // Uninitialized in constructor + state.force_insert_account( + IdForExeTests::vault_a_id(), + AccountsForExeTests::vault_a_init_inactive(), + ); + state.force_insert_account( + IdForExeTests::vault_b_id(), + AccountsForExeTests::vault_b_init_inactive(), + ); + + let instruction = amm_core::Instruction::NewDefinition { + token_a_amount: BalanceForExeTests::vault_a_balance_init(), + token_b_amount: BalanceForExeTests::vault_b_balance_init(), + amm_program_id: Program::amm().id(), + }; + + let message = public_transaction::Message::try_new( + Program::amm().id(), + vec![ + IdForExeTests::pool_definition_id(), + IdForExeTests::vault_a_id(), + IdForExeTests::vault_b_id(), + IdForExeTests::token_lp_definition_id(), + IdForExeTests::user_token_a_id(), + IdForExeTests::user_token_b_id(), + IdForExeTests::user_token_lp_id(), + ], + vec![0, 0], + instruction, + ) + .unwrap(); + + let witness_set = public_transaction::WitnessSet::for_message( + &message, + &[ + &PrivateKeysForTests::user_token_a_key(), + &PrivateKeysForTests::user_token_b_key(), + ], + ); + + let tx = PublicTransaction::new(message, witness_set); + state.transition_from_public_transaction(&tx).unwrap(); + + let pool_post = state.get_account_by_id(IdForExeTests::pool_definition_id()); + let vault_a_post = state.get_account_by_id(IdForExeTests::vault_a_id()); + let vault_b_post = state.get_account_by_id(IdForExeTests::vault_b_id()); + let token_lp_post = state.get_account_by_id(IdForExeTests::token_lp_definition_id()); + let user_token_a_post = state.get_account_by_id(IdForExeTests::user_token_a_id()); + let user_token_b_post = state.get_account_by_id(IdForExeTests::user_token_b_id()); + let user_token_lp_post = state.get_account_by_id(IdForExeTests::user_token_lp_id()); + + let expected_pool = AccountsForExeTests::pool_definition_new_init(); + let expected_vault_a = AccountsForExeTests::vault_a_init(); + let expected_vault_b = AccountsForExeTests::vault_b_init(); + let expected_token_lp = AccountsForExeTests::token_lp_definition_new_init(); + let expected_user_token_a = AccountsForExeTests::user_token_a_holding_new_init(); + let expected_user_token_b = AccountsForExeTests::user_token_b_holding_new_init(); + let expected_user_token_lp = AccountsForExeTests::user_token_lp_holding_new_init(); + + assert_eq!(pool_post, expected_pool); + assert_eq!(vault_a_post, expected_vault_a); + assert_eq!(vault_b_post, expected_vault_b); + assert_eq!(token_lp_post, expected_token_lp); + assert_eq!(user_token_a_post, expected_user_token_a); + assert_eq!(user_token_b_post, expected_user_token_b); + assert_eq!(user_token_lp_post, expected_user_token_lp); +} + +#[cfg(feature = "with-nssa")] +#[test] +fn test_simple_amm_add() { + let mut state = state_for_amm_tests(); + + let instruction = amm_core::Instruction::AddLiquidity { + min_amount_liquidity: BalanceForExeTests::add_min_amount_lp(), + max_amount_to_add_token_a: BalanceForExeTests::add_max_amount_a(), + max_amount_to_add_token_b: BalanceForExeTests::add_max_amount_b(), + }; + + let message = public_transaction::Message::try_new( + Program::amm().id(), + vec![ + IdForExeTests::pool_definition_id(), + IdForExeTests::vault_a_id(), + IdForExeTests::vault_b_id(), + IdForExeTests::token_lp_definition_id(), + IdForExeTests::user_token_a_id(), + IdForExeTests::user_token_b_id(), + IdForExeTests::user_token_lp_id(), + ], + vec![0, 0], + instruction, + ) + .unwrap(); + + let witness_set = public_transaction::WitnessSet::for_message( + &message, + &[ + &PrivateKeysForTests::user_token_a_key(), + &PrivateKeysForTests::user_token_b_key(), + ], + ); + + let tx = PublicTransaction::new(message, witness_set); + state.transition_from_public_transaction(&tx).unwrap(); + + let pool_post = state.get_account_by_id(IdForExeTests::pool_definition_id()); + let vault_a_post = state.get_account_by_id(IdForExeTests::vault_a_id()); + let vault_b_post = state.get_account_by_id(IdForExeTests::vault_b_id()); + let token_lp_post = state.get_account_by_id(IdForExeTests::token_lp_definition_id()); + let user_token_a_post = state.get_account_by_id(IdForExeTests::user_token_a_id()); + let user_token_b_post = state.get_account_by_id(IdForExeTests::user_token_b_id()); + let user_token_lp_post = state.get_account_by_id(IdForExeTests::user_token_lp_id()); + + let expected_pool = AccountsForExeTests::pool_definition_add(); + let expected_vault_a = AccountsForExeTests::vault_a_add(); + let expected_vault_b = AccountsForExeTests::vault_b_add(); + let expected_token_lp = AccountsForExeTests::token_lp_definition_add(); + let expected_user_token_a = AccountsForExeTests::user_token_a_holding_add(); + let expected_user_token_b = AccountsForExeTests::user_token_b_holding_add(); + let expected_user_token_lp = AccountsForExeTests::user_token_lp_holding_add(); + + assert_eq!(pool_post, expected_pool); + assert_eq!(vault_a_post, expected_vault_a); + assert_eq!(vault_b_post, expected_vault_b); + assert_eq!(token_lp_post, expected_token_lp); + assert_eq!(user_token_a_post, expected_user_token_a); + assert_eq!(user_token_b_post, expected_user_token_b); + assert_eq!(user_token_lp_post, expected_user_token_lp); +} + +#[cfg(feature = "with-nssa")] +#[test] +fn test_simple_amm_swap_1() { + let mut state = state_for_amm_tests(); + + let instruction = amm_core::Instruction::Swap { + swap_amount_in: BalanceForExeTests::swap_amount_in(), + min_amount_out: BalanceForExeTests::swap_min_amount_out(), + token_definition_id_in: IdForExeTests::token_b_definition_id(), + }; + + let message = public_transaction::Message::try_new( + Program::amm().id(), + vec![ + IdForExeTests::pool_definition_id(), + IdForExeTests::vault_a_id(), + IdForExeTests::vault_b_id(), + IdForExeTests::user_token_a_id(), + IdForExeTests::user_token_b_id(), + ], + vec![0], + instruction, + ) + .unwrap(); + + let witness_set = public_transaction::WitnessSet::for_message( + &message, + &[&PrivateKeysForTests::user_token_b_key()], + ); + + let tx = PublicTransaction::new(message, witness_set); + state.transition_from_public_transaction(&tx).unwrap(); + + let pool_post = state.get_account_by_id(IdForExeTests::pool_definition_id()); + let vault_a_post = state.get_account_by_id(IdForExeTests::vault_a_id()); + let vault_b_post = state.get_account_by_id(IdForExeTests::vault_b_id()); + let user_token_a_post = state.get_account_by_id(IdForExeTests::user_token_a_id()); + let user_token_b_post = state.get_account_by_id(IdForExeTests::user_token_b_id()); + + let expected_pool = AccountsForExeTests::pool_definition_swap_1(); + let expected_vault_a = AccountsForExeTests::vault_a_swap_1(); + let expected_vault_b = AccountsForExeTests::vault_b_swap_1(); + let expected_user_token_a = AccountsForExeTests::user_token_a_holding_swap_1(); + let expected_user_token_b = AccountsForExeTests::user_token_b_holding_swap_1(); + + assert_eq!(pool_post, expected_pool); + assert_eq!(vault_a_post, expected_vault_a); + assert_eq!(vault_b_post, expected_vault_b); + assert_eq!(user_token_a_post, expected_user_token_a); + assert_eq!(user_token_b_post, expected_user_token_b); +} + +#[cfg(feature = "with-nssa")] +#[test] +fn test_simple_amm_swap_2() { + let mut state = state_for_amm_tests(); + + let instruction = amm_core::Instruction::Swap { + swap_amount_in: BalanceForExeTests::swap_amount_in(), + min_amount_out: BalanceForExeTests::swap_min_amount_out(), + token_definition_id_in: IdForExeTests::token_a_definition_id(), + }; + let message = public_transaction::Message::try_new( + Program::amm().id(), + vec![ + IdForExeTests::pool_definition_id(), + IdForExeTests::vault_a_id(), + IdForExeTests::vault_b_id(), + IdForExeTests::user_token_a_id(), + IdForExeTests::user_token_b_id(), + ], + vec![0], + instruction, + ) + .unwrap(); + + let witness_set = public_transaction::WitnessSet::for_message( + &message, + &[&PrivateKeysForTests::user_token_a_key()], + ); + + let tx = PublicTransaction::new(message, witness_set); + state.transition_from_public_transaction(&tx).unwrap(); + + let pool_post = state.get_account_by_id(IdForExeTests::pool_definition_id()); + let vault_a_post = state.get_account_by_id(IdForExeTests::vault_a_id()); + let vault_b_post = state.get_account_by_id(IdForExeTests::vault_b_id()); + let user_token_a_post = state.get_account_by_id(IdForExeTests::user_token_a_id()); + let user_token_b_post = state.get_account_by_id(IdForExeTests::user_token_b_id()); + + let expected_pool = AccountsForExeTests::pool_definition_swap_2(); + let expected_vault_a = AccountsForExeTests::vault_a_swap_2(); + let expected_vault_b = AccountsForExeTests::vault_b_swap_2(); + let expected_user_token_a = AccountsForExeTests::user_token_a_holding_swap_2(); + let expected_user_token_b = AccountsForExeTests::user_token_b_holding_swap_2(); + + assert_eq!(pool_post, expected_pool); + assert_eq!(vault_a_post, expected_vault_a); + assert_eq!(vault_b_post, expected_vault_b); + assert_eq!(user_token_a_post, expected_user_token_a); + assert_eq!(user_token_b_post, expected_user_token_b); +}