From 16b441edb824a0ce751ddc0058e4cd0c2013e59d Mon Sep 17 00:00:00 2001 From: Jon Cinque Date: Wed, 10 Aug 2022 13:41:17 +0200 Subject: [PATCH] Step 1: Update all instruction interfaces --- token-swap/program/src/instruction.rs | 58 +++++++++++++++++++-------- token-swap/program/src/processor.rs | 32 +++++++++++++++ 2 files changed, 73 insertions(+), 17 deletions(-) diff --git a/token-swap/program/src/instruction.rs b/token-swap/program/src/instruction.rs index c8803df1..dd8c520c 100644 --- a/token-swap/program/src/instruction.rs +++ b/token-swap/program/src/instruction.rs @@ -105,7 +105,7 @@ pub enum SwapInstruction { /// Must be empty, not owned by swap authority /// 6. `[writable]` Pool Token Account to deposit the initial pool token /// supply. Must be empty, not owned by swap authority. - /// 7. `[]` Token program id + /// 7. `[]` Pool Token program id Initialize(Initialize), /// Swap the tokens in the pool. @@ -119,8 +119,10 @@ pub enum SwapInstruction { /// 6. `[writable]` token_(A|B) DESTINATION Account assigned to USER as the owner. /// 7. `[writable]` Pool token mint, to generate trading fees /// 8. `[writable]` Fee account, to receive trading fees - /// 9. `[]` Token program id - /// 10. `[optional, writable]` Host fee account to receive additional trading fees + /// 9. `[]` Token (A|B) SOURCE program id + /// 10. `[]` Token (A|B) DESTINATION program id + /// 11. `[]` Pool Token program id + /// 12. `[optional, writable]` Host fee account to receive additional trading fees Swap(Swap), /// Deposit both types of tokens into the pool. The output is a "pool" @@ -136,7 +138,9 @@ pub enum SwapInstruction { /// 6. `[writable]` token_b Base Account to deposit into. /// 7. `[writable]` Pool MINT account, swap authority is the owner. /// 8. `[writable]` Pool Account to deposit the generated tokens, user is the owner. - /// 9. `[]` Token program id + /// 9. `[]` Token A program id + /// 10. `[]` Token B program id + /// 11. `[]` Pool Token program id DepositAllTokenTypes(DepositAllTokenTypes), /// Withdraw both types of tokens from the pool at the current ratio, given @@ -153,7 +157,9 @@ pub enum SwapInstruction { /// 7. `[writable]` token_a user Account to credit. /// 8. `[writable]` token_b user Account to credit. /// 9. `[writable]` Fee account, to receive withdrawal fees - /// 10. `[]` Token program id + /// 10. `[]` Pool Token program id + /// 11. `[]` Token A program id + /// 12. `[]` Token B program id WithdrawAllTokenTypes(WithdrawAllTokenTypes), /// Deposit one type of tokens into the pool. The output is a "pool" token @@ -168,7 +174,8 @@ pub enum SwapInstruction { /// 5. `[writable]` token_b Swap Account, may deposit INTO. /// 6. `[writable]` Pool MINT account, swap authority is the owner. /// 7. `[writable]` Pool Account to deposit the generated tokens, user is the owner. - /// 8. `[]` Token program id + /// 8. `[]` Token (A|B) SOURCE program id + /// 9. `[]` Pool Token program id DepositSingleTokenTypeExactAmountIn(DepositSingleTokenTypeExactAmountIn), /// Withdraw one token type from the pool at the current ratio given the @@ -183,7 +190,8 @@ pub enum SwapInstruction { /// 6. `[writable]` token_b Swap Account to potentially withdraw from. /// 7. `[writable]` token_(A|B) User Account to credit /// 8. `[writable]` Fee account, to receive withdrawal fees - /// 9. `[]` Token program id + /// 9. `[]` Pool Token program id + /// 10. `[]` Token (A|B) DESTINATION program id WithdrawSingleTokenTypeExactAmountOut(WithdrawSingleTokenTypeExactAmountOut), } @@ -366,7 +374,9 @@ pub fn initialize( /// Creates a 'deposit_all_token_types' instruction. pub fn deposit_all_token_types( program_id: &Pubkey, - token_program_id: &Pubkey, + token_a_program_id: &Pubkey, + token_b_program_id: &Pubkey, + pool_token_program_id: &Pubkey, swap_pubkey: &Pubkey, authority_pubkey: &Pubkey, user_transfer_authority_pubkey: &Pubkey, @@ -390,7 +400,9 @@ pub fn deposit_all_token_types( AccountMeta::new(*swap_token_b_pubkey, false), AccountMeta::new(*pool_mint_pubkey, false), AccountMeta::new(*destination_pubkey, false), - AccountMeta::new_readonly(*token_program_id, false), + AccountMeta::new_readonly(*token_a_program_id, false), + AccountMeta::new_readonly(*token_b_program_id, false), + AccountMeta::new_readonly(*pool_token_program_id, false), ]; Ok(Instruction { @@ -403,7 +415,9 @@ pub fn deposit_all_token_types( /// Creates a 'withdraw_all_token_types' instruction. pub fn withdraw_all_token_types( program_id: &Pubkey, - token_program_id: &Pubkey, + pool_token_program_id: &Pubkey, + token_a_program_id: &Pubkey, + token_b_program_id: &Pubkey, swap_pubkey: &Pubkey, authority_pubkey: &Pubkey, user_transfer_authority_pubkey: &Pubkey, @@ -429,7 +443,9 @@ pub fn withdraw_all_token_types( AccountMeta::new(*destination_token_a_pubkey, false), AccountMeta::new(*destination_token_b_pubkey, false), AccountMeta::new(*fee_account_pubkey, false), - AccountMeta::new_readonly(*token_program_id, false), + AccountMeta::new_readonly(*pool_token_program_id, false), + AccountMeta::new_readonly(*token_a_program_id, false), + AccountMeta::new_readonly(*token_b_program_id, false), ]; Ok(Instruction { @@ -442,7 +458,8 @@ pub fn withdraw_all_token_types( /// Creates a 'deposit_single_token_type_exact_amount_in' instruction. pub fn deposit_single_token_type_exact_amount_in( program_id: &Pubkey, - token_program_id: &Pubkey, + source_token_program_id: &Pubkey, + pool_token_program_id: &Pubkey, swap_pubkey: &Pubkey, authority_pubkey: &Pubkey, user_transfer_authority_pubkey: &Pubkey, @@ -464,7 +481,8 @@ pub fn deposit_single_token_type_exact_amount_in( AccountMeta::new(*swap_token_b_pubkey, false), AccountMeta::new(*pool_mint_pubkey, false), AccountMeta::new(*destination_pubkey, false), - AccountMeta::new_readonly(*token_program_id, false), + AccountMeta::new_readonly(*source_token_program_id, false), + AccountMeta::new_readonly(*pool_token_program_id, false), ]; Ok(Instruction { @@ -477,7 +495,8 @@ pub fn deposit_single_token_type_exact_amount_in( /// Creates a 'withdraw_single_token_type_exact_amount_out' instruction. pub fn withdraw_single_token_type_exact_amount_out( program_id: &Pubkey, - token_program_id: &Pubkey, + pool_token_program_id: &Pubkey, + destination_token_program_id: &Pubkey, swap_pubkey: &Pubkey, authority_pubkey: &Pubkey, user_transfer_authority_pubkey: &Pubkey, @@ -501,7 +520,8 @@ pub fn withdraw_single_token_type_exact_amount_out( AccountMeta::new(*swap_token_b_pubkey, false), AccountMeta::new(*destination_pubkey, false), AccountMeta::new(*fee_account_pubkey, false), - AccountMeta::new_readonly(*token_program_id, false), + AccountMeta::new_readonly(*pool_token_program_id, false), + AccountMeta::new_readonly(*destination_token_program_id, false), ]; Ok(Instruction { @@ -514,7 +534,9 @@ pub fn withdraw_single_token_type_exact_amount_out( /// Creates a 'swap' instruction. pub fn swap( program_id: &Pubkey, - token_program_id: &Pubkey, + source_token_program_id: &Pubkey, + destination_token_program_id: &Pubkey, + pool_token_program_id: &Pubkey, swap_pubkey: &Pubkey, authority_pubkey: &Pubkey, user_transfer_authority_pubkey: &Pubkey, @@ -539,7 +561,9 @@ pub fn swap( AccountMeta::new(*destination_pubkey, false), AccountMeta::new(*pool_mint_pubkey, false), AccountMeta::new(*pool_fee_pubkey, false), - AccountMeta::new_readonly(*token_program_id, false), + AccountMeta::new_readonly(*source_token_program_id, false), + AccountMeta::new_readonly(*destination_token_program_id, false), + AccountMeta::new_readonly(*pool_token_program_id, false), ]; if let Some(host_fee_pubkey) = host_fee_pubkey { accounts.push(AccountMeta::new(*host_fee_pubkey, false)); diff --git a/token-swap/program/src/processor.rs b/token-swap/program/src/processor.rs index 9a4d3536..f4500d04 100644 --- a/token-swap/program/src/processor.rs +++ b/token-swap/program/src/processor.rs @@ -1440,6 +1440,8 @@ mod tests { swap( &SWAP_PROGRAM_ID, &self.token_program_id, + &self.token_program_id, + &self.token_program_id, &self.swap_key, &self.authority_key, &user_transfer_key, @@ -1531,6 +1533,8 @@ mod tests { deposit_all_token_types( &SWAP_PROGRAM_ID, &self.token_program_id, + &self.token_program_id, + &self.token_program_id, &self.swap_key, &self.authority_key, &user_transfer_authority, @@ -1601,6 +1605,8 @@ mod tests { withdraw_all_token_types( &SWAP_PROGRAM_ID, &self.token_program_id, + &self.token_program_id, + &self.token_program_id, &self.swap_key, &self.authority_key, &user_transfer_authority_key, @@ -1668,6 +1674,7 @@ mod tests { deposit_single_token_type_exact_amount_in( &SWAP_PROGRAM_ID, &self.token_program_id, + &self.token_program_id, &self.swap_key, &self.authority_key, &user_transfer_authority_key, @@ -1731,6 +1738,7 @@ mod tests { withdraw_single_token_type_exact_amount_out( &SWAP_PROGRAM_ID, &self.token_program_id, + &self.token_program_id, &self.swap_key, &self.authority_key, &user_transfer_authority_key, @@ -3223,6 +3231,8 @@ mod tests { deposit_all_token_types( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &user_transfer_authority_key, @@ -3272,6 +3282,8 @@ mod tests { deposit_all_token_types( &SWAP_PROGRAM_ID, &wrong_key, + &wrong_key, + &wrong_key, &accounts.swap_key, &accounts.authority_key, &accounts.authority_key, @@ -3888,6 +3900,8 @@ mod tests { withdraw_all_token_types( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &user_transfer_authority_key, @@ -3945,6 +3959,8 @@ mod tests { withdraw_all_token_types( &SWAP_PROGRAM_ID, &wrong_key, + &wrong_key, + &wrong_key, &accounts.swap_key, &accounts.authority_key, &accounts.authority_key, @@ -4585,6 +4601,7 @@ mod tests { deposit_single_token_type_exact_amount_in( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &user_transfer_authority_key, @@ -4631,6 +4648,7 @@ mod tests { deposit_single_token_type_exact_amount_in( &SWAP_PROGRAM_ID, &wrong_key, + &wrong_key, &accounts.swap_key, &accounts.authority_key, &accounts.authority_key, @@ -5158,6 +5176,7 @@ mod tests { withdraw_single_token_type_exact_amount_out( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &user_transfer_authority_key, @@ -5212,6 +5231,7 @@ mod tests { withdraw_single_token_type_exact_amount_out( &SWAP_PROGRAM_ID, &wrong_key, + &wrong_key, &accounts.swap_key, &accounts.authority_key, &accounts.authority_key, @@ -5948,6 +5968,8 @@ mod tests { swap( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &accounts.authority_key, @@ -6149,6 +6171,8 @@ mod tests { swap( &SWAP_PROGRAM_ID, &wrong_program_id, + &wrong_program_id, + &wrong_program_id, &accounts.swap_key, &accounts.authority_key, &accounts.authority_key, @@ -6224,6 +6248,8 @@ mod tests { swap( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &user_transfer_key, @@ -6393,6 +6419,8 @@ mod tests { swap( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &user_transfer_key, @@ -6551,6 +6579,8 @@ mod tests { swap( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &accounts.authority_key, @@ -6625,6 +6655,8 @@ mod tests { swap( &SWAP_PROGRAM_ID, &token_program_id, + &token_program_id, + &token_program_id, &accounts.swap_key, &accounts.authority_key, &accounts.authority_key,