diff --git a/Cargo.lock b/Cargo.lock index 3fb6fa62..6faf3b1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -335,6 +335,7 @@ name = "amm_program" version = "0.1.0" dependencies = [ "amm_core", + "nssa", "nssa_core", "token_core", ] @@ -3713,6 +3714,7 @@ dependencies = [ "serde", "serde_json", "storage", + "tempfile", "tokio", "url", ] @@ -5530,7 +5532,6 @@ dependencies = [ name = "nssa" version = "0.1.0" dependencies = [ - "amm_core", "anyhow", "borsh", "env_logger", diff --git a/bedrock_client/src/lib.rs b/bedrock_client/src/lib.rs index 561717a8..fdd14f72 100644 --- a/bedrock_client/src/lib.rs +++ b/bedrock_client/src/lib.rs @@ -31,9 +31,9 @@ impl Default for BackoffConfig { } } -// Simple wrapper -// maybe extend in the future for our purposes -// `Clone` is cheap because `CommonHttpClient` is internally reference counted (`Arc`). +/// Simple wrapper +/// maybe extend in the future for our purposes +/// `Clone` is cheap because `CommonHttpClient` is internally reference counted (`Arc`). #[derive(Clone)] pub struct BedrockClient { http_client: CommonHttpClient, @@ -62,10 +62,22 @@ impl BedrockClient { }) } - pub async fn post_transaction(&self, tx: SignedMantleTx) -> Result<(), Error> { - Retry::spawn(self.backoff_strategy(), || { - self.http_client + pub async fn post_transaction(&self, tx: SignedMantleTx) -> Result, Error> { + Retry::spawn(self.backoff_strategy(), || async { + match self + .http_client .post_transaction(self.node_url.clone(), tx.clone()) + .await + { + Ok(()) => Ok(Ok(())), + Err(err) => match err { + // Retry arm. + // Retrying only reqwest errors: mainly connected to http. + Error::Request(_) => Err(err), + // Returning non-retryable error + Error::Server(_) | Error::Client(_) | Error::Url(_) => Ok(Err(err)), + }, + } }) .await } diff --git a/indexer/core/Cargo.toml b/indexer/core/Cargo.toml index 198cf78e..8129c1ea 100644 --- a/indexer/core/Cargo.toml +++ b/indexer/core/Cargo.toml @@ -25,3 +25,7 @@ url.workspace = true logos-blockchain-core.workspace = true serde_json.workspace = true async-stream.workspace = true + +[dev-dependencies] +tempfile.workspace = true + diff --git a/indexer/core/src/block_store.rs b/indexer/core/src/block_store.rs index 8742184f..db2f855b 100644 --- a/indexer/core/src/block_store.rs +++ b/indexer/core/src/block_store.rs @@ -8,10 +8,12 @@ use common::{ }; use nssa::{Account, AccountId, V02State}; use storage::indexer::RocksDBIO; +use tokio::sync::RwLock; #[derive(Clone)] pub struct IndexerStore { dbio: Arc, + current_state: Arc>, } impl IndexerStore { @@ -25,9 +27,11 @@ impl IndexerStore { initial_state: &V02State, ) -> Result { let dbio = RocksDBIO::open_or_create(location, genesis_block, initial_state)?; + let current_state = dbio.final_state()?; Ok(Self { dbio: Arc::new(dbio), + current_state: Arc::new(RwLock::new(current_state)), }) } @@ -93,22 +97,31 @@ impl IndexerStore { Ok(self.dbio.calculate_state_for_id(block_id)?) } - pub fn final_state(&self) -> Result { + /// Recalculation of final state directly from DB. + /// + /// Used for indexer healthcheck. + pub fn recalculate_final_state(&self) -> Result { Ok(self.dbio.final_state()?) } - pub fn get_account_final(&self, account_id: &AccountId) -> Result { - Ok(self.final_state()?.get_account_by_id(*account_id)) + pub async fn account_current_state(&self, account_id: &AccountId) -> Result { + Ok(self + .current_state + .read() + .await + .get_account_by_id(*account_id)) } - pub fn put_block(&self, mut block: Block, l1_header: HeaderId) -> Result<()> { - let mut final_state = self.dbio.final_state()?; + pub async fn put_block(&self, mut block: Block, l1_header: HeaderId) -> Result<()> { + { + let mut state_guard = self.current_state.write().await; - for transaction in &block.body.transactions { - transaction - .clone() - .transaction_stateless_check()? - .execute_check_on_state(&mut final_state)?; + for transaction in &block.body.transactions { + transaction + .clone() + .transaction_stateless_check()? + .execute_check_on_state(&mut state_guard)?; + } } // ToDo: Currently we are fetching only finalized blocks @@ -119,3 +132,95 @@ impl IndexerStore { Ok(self.dbio.put_block(&block, l1_header.into())?) } } + +#[cfg(test)] +mod tests { + use nssa::{AccountId, PublicKey}; + use tempfile::tempdir; + + use super::*; + + fn genesis_block() -> Block { + common::test_utils::produce_dummy_block(1, None, vec![]) + } + + fn acc1_sign_key() -> nssa::PrivateKey { + nssa::PrivateKey::try_new([1; 32]).unwrap() + } + + fn acc2_sign_key() -> nssa::PrivateKey { + nssa::PrivateKey::try_new([2; 32]).unwrap() + } + + fn acc1() -> AccountId { + AccountId::from(&PublicKey::new_from_private_key(&acc1_sign_key())) + } + + fn acc2() -> AccountId { + AccountId::from(&PublicKey::new_from_private_key(&acc2_sign_key())) + } + + #[test] + fn correct_startup() { + let home = tempdir().unwrap(); + + let storage = IndexerStore::open_db_with_genesis( + home.as_ref(), + &genesis_block(), + &nssa::V02State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]), + ) + .unwrap(); + + let block = storage.get_block_at_id(1).unwrap(); + let final_id = storage.get_last_block_id().unwrap(); + + assert_eq!(block.header.hash, genesis_block().header.hash); + assert_eq!(final_id, 1); + } + + #[tokio::test] + async fn state_transition() { + let home = tempdir().unwrap(); + + let storage = IndexerStore::open_db_with_genesis( + home.as_ref(), + &genesis_block(), + &nssa::V02State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]), + ) + .unwrap(); + + let mut prev_hash = genesis_block().header.hash; + + let from = acc1(); + let to = acc2(); + let sign_key = acc1_sign_key(); + + for i in 2..10 { + let tx = common::test_utils::create_transaction_native_token_transfer( + from, + i - 2, + to, + 10, + &sign_key, + ); + + let next_block = common::test_utils::produce_dummy_block( + u64::try_from(i).unwrap(), + Some(prev_hash), + vec![tx], + ); + prev_hash = next_block.header.hash; + + storage + .put_block(next_block, HeaderId::from([u8::try_from(i).unwrap(); 32])) + .await + .unwrap(); + } + + let acc1_val = storage.account_current_state(&acc1()).await.unwrap(); + let acc2_val = storage.account_current_state(&acc2()).await.unwrap(); + + assert_eq!(acc1_val.balance, 9920); + assert_eq!(acc2_val.balance, 20080); + } +} diff --git a/indexer/core/src/lib.rs b/indexer/core/src/lib.rs index 8f901774..6c96821e 100644 --- a/indexer/core/src/lib.rs +++ b/indexer/core/src/lib.rs @@ -124,8 +124,8 @@ impl IndexerCore { l2_blocks_parsed_ids.sort_unstable(); info!("Parsed {} L2 blocks with ids {:?}", l2_block_vec.len(), l2_blocks_parsed_ids); - for l2_block in l2_block_vec { - self.store.put_block(l2_block.clone(), l1_header)?; + for l2_block in l2_block_vec { + self.store.put_block(l2_block.clone(), l1_header).await?; yield Ok(l2_block); } @@ -158,7 +158,7 @@ impl IndexerCore { info!("Parsed {} L2 blocks with ids {:?}", l2_block_vec.len(), l2_blocks_parsed_ids); for l2_block in l2_block_vec { - self.store.put_block(l2_block.clone(), header)?; + self.store.put_block(l2_block.clone(), header).await?; yield Ok(l2_block); } diff --git a/indexer/service/src/service.rs b/indexer/service/src/service.rs index 2b2e3eca..256ef33d 100644 --- a/indexer/service/src/service.rs +++ b/indexer/service/src/service.rs @@ -74,7 +74,8 @@ impl indexer_service_rpc::RpcServer for IndexerService { Ok(self .indexer .store - .get_account_final(&account_id.into()) + .account_current_state(&account_id.into()) + .await .map_err(db_error)? .into()) } @@ -131,7 +132,11 @@ impl indexer_service_rpc::RpcServer for IndexerService { async fn healthcheck(&self) -> Result<(), ErrorObjectOwned> { // Checking, that indexer can calculate last state - let _ = self.indexer.store.final_state().map_err(db_error)?; + let _ = self + .indexer + .store + .recalculate_final_state() + .map_err(db_error)?; Ok(()) } diff --git a/integration_tests/tests/indexer.rs b/integration_tests/tests/indexer.rs index 5169aacf..0b947135 100644 --- a/integration_tests/tests/indexer.rs +++ b/integration_tests/tests/indexer.rs @@ -1,19 +1,14 @@ #![expect( - clippy::shadow_unrelated, clippy::tests_outside_test_module, reason = "We don't care about these in tests" )] use std::time::Duration; -use anyhow::{Context as _, Result}; +use anyhow::Result; use indexer_service_rpc::RpcClient as _; -use integration_tests::{ - TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, format_private_account_id, - format_public_account_id, verify_commitment_is_in_state, -}; +use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, format_public_account_id}; use log::info; -use nssa::AccountId; use tokio::test; use wallet::cli::{Command, programs::native_token_transfer::AuthTransferSubcommand}; @@ -120,36 +115,6 @@ async fn indexer_state_consistency() -> Result<()> { assert_eq!(acc_1_balance.balance, 9900); assert_eq!(acc_2_balance.balance, 20100); - let from: AccountId = ctx.existing_private_accounts()[0]; - let to: AccountId = ctx.existing_private_accounts()[1]; - - let command = Command::AuthTransfer(AuthTransferSubcommand::Send { - from: format_private_account_id(from), - to: Some(format_private_account_id(to)), - to_npk: None, - to_vpk: None, - amount: 100, - }); - - wallet::cli::execute_subcommand(ctx.wallet_mut(), command).await?; - - info!("Waiting for next block creation"); - tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; - - let new_commitment1 = ctx - .wallet() - .get_private_account_commitment(from) - .context("Failed to get private account commitment for sender")?; - assert!(verify_commitment_is_in_state(new_commitment1, ctx.sequencer_client()).await); - - let new_commitment2 = ctx - .wallet() - .get_private_account_commitment(to) - .context("Failed to get private account commitment for receiver")?; - assert!(verify_commitment_is_in_state(new_commitment2, ctx.sequencer_client()).await); - - info!("Successfully transferred privately to owned account"); - // WAIT info!("Waiting for indexer to parse blocks"); tokio::time::sleep(std::time::Duration::from_millis(L2_TO_L1_TIMEOUT_MILLIS)).await; diff --git a/nssa/Cargo.toml b/nssa/Cargo.toml index d10ba78c..e1b6805f 100644 --- a/nssa/Cargo.toml +++ b/nssa/Cargo.toml @@ -28,7 +28,6 @@ risc0-binfmt = "3.0.2" [dev-dependencies] token_core.workspace = true -amm_core.workspace = true test_program_methods.workspace = true env_logger.workspace = true @@ -38,3 +37,4 @@ test-case = "3.3.1" [features] default = [] prove = ["risc0-zkvm/prove"] +test-utils = [] \ No newline at end of file diff --git a/nssa/core/src/account.rs b/nssa/core/src/account.rs index 5bb17bca..29c967f7 100644 --- a/nssa/core/src/account.rs +++ b/nssa/core/src/account.rs @@ -84,13 +84,15 @@ impl BorshDeserialize for Nonce { } } +pub type Balance = u128; + /// Account to be used both in public and private contexts. #[derive( Default, Clone, Eq, PartialEq, Serialize, Deserialize, BorshSerialize, BorshDeserialize, )] pub struct Account { pub program_owner: ProgramId, - pub balance: u128, + pub balance: Balance, pub data: Data, pub nonce: Nonce, } diff --git a/programs/amm/Cargo.toml b/programs/amm/Cargo.toml index 6b45fc90..449d5dcc 100644 --- a/programs/amm/Cargo.toml +++ b/programs/amm/Cargo.toml @@ -8,6 +8,10 @@ license = { workspace = true } workspace = true [dependencies] +nssa = { workspace = true, optional = true, features = ["test-utils"], default-features = true } nssa_core.workspace = true token_core.workspace = true amm_core.workspace = true + +[features] +nssa = ["dep:nssa"] \ No newline at end of file diff --git a/programs/amm/src/lib.rs b/programs/amm/src/lib.rs index 6142eda6..744f947c 100644 --- a/programs/amm/src/lib.rs +++ b/programs/amm/src/lib.rs @@ -14,4 +14,5 @@ pub mod new_definition; pub mod remove; pub mod swap; +#[cfg(test)] mod tests; diff --git a/programs/amm/src/tests.rs b/programs/amm/src/tests.rs index 0ef2e004..ac2090f5 100644 --- a/programs/amm/src/tests.rs +++ b/programs/amm/src/tests.rs @@ -1,11 +1,13 @@ -#![cfg(test)] - use std::num::NonZero; 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 = "nssa")] +use nssa::{ + PrivateKey, PublicKey, PublicTransaction, V02State, program::Program, public_transaction, +}; use nssa_core::{ account::{Account, AccountId, AccountWithMetadata, Data}, program::{ChainedCall, ProgramId}, @@ -22,7 +24,30 @@ const AMM_PROGRAM_ID: ProgramId = [42; 8]; struct BalanceForTests; struct ChainedCallForTests; struct IdForTests; -struct AccountForTests; +struct AccountWithMetadataForTests; +#[cfg(feature = "nssa")] +struct PrivateKeysForTests; +#[cfg(feature = "nssa")] +struct IdForExeTests; +#[cfg(feature = "nssa")] +struct BalanceForExeTests; +#[cfg(feature = "nssa")] +struct AccountsForExeTests; + +#[cfg(feature = "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 { @@ -160,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(), @@ -172,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, }, @@ -191,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, }, @@ -211,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(), @@ -224,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(), @@ -237,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(), @@ -247,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, }, @@ -263,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(), }, @@ -280,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, }, @@ -297,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(), }, @@ -316,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(), @@ -329,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(), @@ -342,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(), @@ -405,7 +436,7 @@ impl IdForTests { } } -impl AccountForTests { +impl AccountWithMetadataForTests { fn user_holding_a() -> AccountWithMetadata { AccountWithMetadata { account: Account { @@ -977,6 +1008,809 @@ impl AccountForTests { } } +#[cfg(feature = "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 + (Self::vault_a_balance_init() * Self::vault_b_balance_init()).isqrt() + } +} + +#[cfg(feature = "nssa")] +impl IdForExeTests { + fn pool_definition_id() -> AccountId { + amm_core::compute_pool_pda( + Program::amm().id(), + Self::token_a_definition_id(), + Self::token_b_definition_id(), + ) + } + + fn token_lp_definition_id() -> AccountId { + amm_core::compute_liquidity_token_pda(Program::amm().id(), Self::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(), + Self::pool_definition_id(), + Self::token_a_definition_id(), + ) + } + + fn vault_b_id() -> AccountId { + amm_core::compute_vault_pda( + Program::amm().id(), + Self::pool_definition_id(), + Self::token_b_definition_id(), + ) + } +} + +#[cfg(feature = "nssa")] +impl AccountsForExeTests { + fn user_token_a_holding() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + active: true, + }), + nonce: 0, + } + } + + fn token_a_definition_account() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_a_holding_swap_1() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_a_holding_swap_2() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_a_holding_add() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_a_holding_remove() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + active: false, + }), + nonce: 0, + } + } + + fn user_token_a_holding_new_init() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + 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: 0_u128, + active: true, + }), + nonce: 0, + } + } + + fn user_token_lp_holding_init_zero() -> Account { + Account { + program_owner: Program::token().id(), + balance: 0_u128, + data: Data::from(&TokenHolding::Fungible { + definition_id: IdForExeTests::token_lp_definition_id(), + balance: 0, + }), + nonce: 0, + } + } +} + #[test] fn pool_pda_produces_unique_id_for_token_pair() { assert!( @@ -996,13 +1830,13 @@ fn pool_pda_produces_unique_id_for_token_pair() { #[test] fn 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(), @@ -1013,13 +1847,13 @@ fn call_add_liquidity_vault_a_omitted() { #[test] fn 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(), @@ -1030,13 +1864,13 @@ fn call_add_liquidity_vault_b_omitted() { #[test] fn 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(), @@ -1047,13 +1881,13 @@ fn call_add_liquidity_lp_definition_mismatch() { #[test] fn 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(), @@ -1064,13 +1898,13 @@ fn call_add_liquidity_zero_balance_1() { #[test] fn 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(), @@ -1081,13 +1915,13 @@ fn call_add_liquidity_zero_balance_2() { #[test] fn 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(), @@ -1098,13 +1932,13 @@ fn call_add_liquidity_vault_insufficient_balance_1() { #[test] fn 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(), @@ -1115,13 +1949,13 @@ fn call_add_liquidity_vault_insufficient_balance_2() { #[test] fn 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(), @@ -1132,13 +1966,13 @@ fn call_add_liquidity_actual_amount_zero_1() { #[test] fn 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(), @@ -1149,13 +1983,13 @@ fn call_add_liquidity_actual_amount_zero_2() { #[test] fn 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(), @@ -1166,13 +2000,13 @@ fn call_add_liquidity_reserves_zero_1() { #[test] fn 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(), @@ -1183,13 +2017,13 @@ fn call_add_liquidity_reserves_zero_2() { #[test] fn 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(), @@ -1199,13 +2033,13 @@ fn call_add_liquidity_payable_lp_zero() { #[test] fn 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(), @@ -1213,7 +2047,10 @@ fn 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(); @@ -1228,13 +2065,13 @@ fn call_add_liquidity_chained_call_successsful() { #[test] fn 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(), @@ -1245,13 +2082,13 @@ fn call_remove_liquidity_vault_a_omitted() { #[test] fn 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(), @@ -1262,13 +2099,13 @@ fn call_remove_liquidity_vault_b_omitted() { #[test] fn 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(), @@ -1279,14 +2116,15 @@ fn call_remove_liquidity_lp_def_mismatch() { #[test] fn 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(), @@ -1299,13 +2137,13 @@ fn call_remove_liquidity_insufficient_liquidity_amount() { #[test] fn 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(), @@ -1318,13 +2156,13 @@ fn call_remove_liquidity_insufficient_balance_1() { #[test] fn 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(), @@ -1335,13 +2173,13 @@ fn call_remove_liquidity_insufficient_balance_2() { #[test] fn 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(), @@ -1352,13 +2190,13 @@ fn call_remove_liquidity_min_bal_zero_1() { #[test] fn 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, @@ -1368,13 +2206,13 @@ fn call_remove_liquidity_min_bal_zero_2() { #[test] fn 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(), @@ -1382,7 +2220,10 @@ fn 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(); @@ -1397,13 +2238,13 @@ fn call_remove_liquidity_chained_call_successful() { #[test] fn 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, @@ -1414,13 +2255,13 @@ fn call_new_definition_with_zero_balance_1() { #[test] fn 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, @@ -1431,13 +2272,13 @@ fn call_new_definition_with_zero_balance_2() { #[test] fn 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, @@ -1448,13 +2289,13 @@ fn call_new_definition_same_token_definition() { #[test] fn 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, @@ -1465,13 +2306,13 @@ fn call_new_definition_wrong_liquidity_id() { #[test] fn 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, @@ -1482,13 +2323,13 @@ fn call_new_definition_wrong_pool_id() { #[test] fn 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, @@ -1499,13 +2340,13 @@ fn call_new_definition_wrong_vault_id_1() { #[test] fn 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, @@ -1516,13 +2357,13 @@ fn call_new_definition_wrong_vault_id_2() { #[test] fn 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, @@ -1533,13 +2374,13 @@ fn call_new_definition_cannot_initialize_active_pool() { #[test] fn 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, @@ -1547,7 +2388,10 @@ fn 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(); @@ -1562,11 +2406,11 @@ fn call_new_definition_chained_call_successful() { #[test] fn 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(), @@ -1577,11 +2421,11 @@ fn call_swap_incorrect_token_type() { #[test] fn 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(), @@ -1592,11 +2436,11 @@ fn call_swap_vault_a_omitted() { #[test] fn 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(), @@ -1607,11 +2451,11 @@ fn call_swap_vault_b_omitted() { #[test] fn 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(), @@ -1622,11 +2466,11 @@ fn call_swap_reserves_vault_mismatch_1() { #[test] fn 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(), @@ -1637,11 +2481,11 @@ fn call_swap_reserves_vault_mismatch_2() { #[test] fn 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(), @@ -1652,11 +2496,11 @@ fn call_swap_ianctive() { #[test] fn 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(), @@ -1666,11 +2510,11 @@ fn call_swap_below_min_out() { #[test] fn 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(), @@ -1678,7 +2522,9 @@ fn 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(); @@ -1696,11 +2542,11 @@ fn call_swap_chained_call_successful_1() { #[test] fn 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(), @@ -1708,7 +2554,9 @@ fn 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(); @@ -1726,13 +2574,13 @@ fn call_swap_chained_call_successful_2() { #[test] fn 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, @@ -1759,13 +2607,13 @@ fn 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, @@ -1779,8 +2627,8 @@ fn 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, @@ -1792,3 +2640,535 @@ fn new_definition_lp_symmetric_amounts() { assert_eq!(chained_call_lp, expected_lp_call); } + +#[cfg(feature = "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 = "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 = "nssa")] +#[test] +fn 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 = "nssa")] +#[test] +fn 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 = "nssa")] +#[test] +fn 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 = "nssa")] +#[test] +fn 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 = "nssa")] +#[test] +fn 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 = "nssa")] +#[test] +fn 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 = "nssa")] +#[test] +fn 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); +} diff --git a/sequencer_core/src/block_settlement_client.rs b/sequencer_core/src/block_settlement_client.rs index 89b86bb9..2f036b98 100644 --- a/sequencer_core/src/block_settlement_client.rs +++ b/sequencer_core/src/block_settlement_client.rs @@ -102,7 +102,8 @@ impl BlockSettlementClientTrait for BlockSettlementClient { self.client .post_transaction(tx) .await - .context("Failed to post transaction to Bedrock")?; + .context("Failed to post transaction to Bedrock after retries")? + .context("Failed to post transaction to Bedrock with non-retryable error")?; log::debug!("Posted block to Bedrock with parent id {parent_id:?} and msg id: {msg_id:?}");