From 853d4d6b59a375ad9ae55140bfb17a11c41ff7ea Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Tue, 18 Nov 2025 14:43:40 -0500 Subject: [PATCH] Tests written --- nssa/program_methods/guest/src/bin/amm.rs | 1674 ++++++++++++++++++++- 1 file changed, 1634 insertions(+), 40 deletions(-) diff --git a/nssa/program_methods/guest/src/bin/amm.rs b/nssa/program_methods/guest/src/bin/amm.rs index ba48dc0..8625429 100644 --- a/nssa/program_methods/guest/src/bin/amm.rs +++ b/nssa/program_methods/guest/src/bin/amm.rs @@ -21,7 +21,7 @@ use bytemuck; // * An instruction data byte string of length 23, indicating the total supply with the following layout // [0x01 || amount (little-endian 16 bytes) || 0x00 || 0x00 || 0x00 || 0x00 || 0x00 || 0x00]. -const POOL_DEFINITION_DATA_SIZE: usize = 176; +const POOL_DEFINITION_DATA_SIZE: usize = 240; struct PoolDefinition{ definition_token_a_id: AccountId, @@ -64,10 +64,9 @@ impl PoolDefinition { let vault_b_addr = AccountId::new(data[96..128].try_into().unwrap()); let liquidity_pool_id = AccountId::new(data[128..160].try_into().unwrap()); let liquidity_pool_cap = u128::from_le_bytes(data[160..176].try_into().unwrap()); - let reserve_a = u128::from_le_bytes(data[176..].try_into().unwrap()); + let reserve_a = u128::from_le_bytes(data[176..192].try_into().unwrap()); let reserve_b = u128::from_le_bytes(data[192..208].try_into().unwrap()); - let token_program_id : &[u32] = bytemuck::cast_slice(&data[208..]); let token_program_id : ProgramId = token_program_id[0..8].try_into().unwrap(); Some(Self { @@ -142,7 +141,7 @@ fn new_definition( //2 accounts for funding tokens //initial funder's LP account if pre_states.len() != 7 { - panic!("Invalid number of input account") + panic!("Invalid number of input accounts") } if balance_in.len() != 2 { @@ -157,25 +156,26 @@ fn new_definition( let user_b = &pre_states[5]; let user_lp = &pre_states[6]; - if pool.account != Account::default() || !pool.is_authorized { - panic!("TODO-1"); + if pool.account == Account::default() || !pool.is_authorized { + panic!("Pool account is uninitiated or not authorized"); } // TODO: temporary band-aid to prevent vault's from being // owned by the amm program. if vault_a.account == Account::default() || vault_b.account == Account::default() { - panic!("Vault accounts must be initialized first; issue to be fixed") + panic!("Vault accounts uninitialized") } if pool_lp.account == Account::default() { - panic!("Pool LP must be initialized first; issue to be fixed") + panic!("Pool LP must be initialized first") } let amount_a = balance_in[0]; let amount_b = balance_in[1]; // Prevents pool constant coefficient (k) from being 0. - assert!(amount_a > 0); - assert!(amount_b > 0); + if amount_a == 0 || amount_b == 0 { + panic!("Balances must be nonzero") + } // Verify token_a and token_b are different let definition_token_a_id = TokenHolding::parse(&vault_a.account.data).unwrap().definition_id; @@ -270,12 +270,10 @@ fn main() { write_nssa_outputs_with_chained_call(pre_states, post_states, chained_call); } 1 => { - let intent = SwapIntent { - token_id: AccountId::new(instruction[1..33].try_into().unwrap()), - amount: u128::from_le_bytes(instruction[33..49].try_into().unwrap()), - }; + let token_id = AccountId::new(instruction[1..33].try_into().unwrap()); + let amount = u128::from_le_bytes(instruction[33..49].try_into().unwrap()); - let (post_states, chained_call) = swap(&pre_states, &intent); + let (post_states, chained_call) = swap(&pre_states, amount, token_id); write_nssa_outputs_with_chained_call(pre_states, post_states, chained_call); } @@ -296,14 +294,10 @@ fn main() { }; } -struct SwapIntent { - token_id: AccountId, - amount: u128, -} - fn swap( pre_states: &[AccountWithMetadata], - intent: &SwapIntent, + amount: u128, + token_id: AccountId, ) -> (Vec, Vec) { if pre_states.len() != 5 { @@ -327,7 +321,7 @@ fn swap( } else if vault2.account_id == pool_def_data.definition_token_a_id { vault_a = vault2.clone(); } else { - panic!("Vault A was no provided"); + panic!("Vault A was not provided"); } if vault1.account_id == pool_def_data.definition_token_b_id { @@ -335,21 +329,21 @@ fn swap( } else if vault2.account_id == pool_def_data.definition_token_b_id { vault_b = vault2.clone(); } else { - panic!("Vault B was no provided"); + panic!("Vault B was not provided"); } // 1. Identify swap direction (a -> b or b -> a) let mut deposit_a = 0; let mut deposit_b = 0; let a_to_b; - if intent.token_id == pool_def_data.definition_token_a_id { - deposit_a = intent.amount; + if token_id == pool_def_data.definition_token_a_id { + deposit_a = amount; a_to_b = true; - } else if intent.token_id == pool_def_data.definition_token_b_id { - deposit_b = intent.amount; + } else if token_id == pool_def_data.definition_token_b_id { + deposit_b = amount; a_to_b = false; } else { - panic!("Intent address is not a token type for the pool"); + panic!("Address is not a token type for the pool"); } // 2. fetch pool reserves @@ -462,12 +456,19 @@ fn add_liquidity(pre_states: &[AccountWithMetadata], let pool_def_data = PoolDefinition::parse(&pool.account.data).unwrap(); + if max_balance_in.len() != 2 { + panic!("Invalid number of input balances"); + } + let max_amount_a = max_balance_in[0]; + let max_amount_b = max_balance_in[1]; + + if vault1.account_id == pool_def_data.definition_token_a_id { vault_a = vault1.clone(); } else if vault2.account_id == pool_def_data.definition_token_a_id { vault_a = vault2.clone(); } else { - panic!("Vault A was no provided"); + panic!("Vault A was not provided"); } if vault1.account_id == pool_def_data.definition_token_b_id { @@ -475,17 +476,9 @@ fn add_liquidity(pre_states: &[AccountWithMetadata], } else if vault2.account_id == pool_def_data.definition_token_b_id { vault_b = vault2.clone(); } else { - panic!("Vault B was no provided"); + panic!("Vault B was not provided"); } - - if max_balance_in.len() != 2 { - panic!("Invalid number of input balances"); - } - - let max_amount_a = max_balance_in[0]; - let max_amount_b = max_balance_in[1]; - // 2. Determine deposit amounts let mut actual_amount_a = 0; let mut actual_amount_b = 0; @@ -502,6 +495,7 @@ fn add_liquidity(pre_states: &[AccountWithMetadata], // 3. Validate amounts + assert!(max_amount_a >= actual_amount_a && max_amount_b >= actual_amount_b); assert!(user_a.account.balance >= actual_amount_a && actual_amount_a > 0); assert!(user_b.account.balance >= actual_amount_b && actual_amount_b > 0); @@ -593,7 +587,7 @@ fn remove_liquidity(pre_states: &[AccountWithMetadata]) -> (Vec, Vec (Vec, Vec (Vec, Vec