-
Notifications
You must be signed in to change notification settings - Fork 247
feat (PRO-254): Add Token 2022 integration tests #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d88f1ce
feat (PRO-254): Add Token 2022 integration tests
dev-jodee a919665
Fix broken jupiter test
dev-jodee c670aa2
JUP tst fix
dev-jodee f13eeac
jupiter fix2
dev-jodee 46ab29f
Added transfer fee tests with payment amount bounds
dev-jodee a17f69d
Jup 4
dev-jodee 7e8188d
Update coverage badge [skip ci]
invalid-email-address File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| use anyhow::Result; | ||
| use solana_client::nonblocking::rpc_client::RpcClient; | ||
| use solana_sdk::{ | ||
| pubkey::Pubkey, | ||
| signature::{Keypair, Signer}, | ||
| transaction::Transaction, | ||
| }; | ||
| use spl_token_2022::{ | ||
| extension::{interest_bearing_mint::instruction::initialize, ExtensionType}, | ||
| instruction as token_2022_instruction, | ||
| state::{Account as Token2022Account, Mint as Token2022Mint}, | ||
| }; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::common::USDCMintTestHelper; | ||
|
|
||
| /// Helper functions for creating Token 2022 accounts with specific extensions for testing | ||
| pub struct ExtensionHelpers; | ||
|
|
||
| impl ExtensionHelpers { | ||
| /// Create a mint with InterestBearingConfig extension | ||
| pub async fn create_mint_with_interest_bearing( | ||
| rpc_client: &Arc<RpcClient>, | ||
| payer: &Keypair, | ||
| mint_keypair: &Keypair, | ||
| ) -> Result<()> { | ||
| if (rpc_client.get_account(&mint_keypair.pubkey()).await).is_ok() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let decimals = USDCMintTestHelper::get_test_usdc_mint_decimals(); | ||
|
|
||
| let space = ExtensionType::try_calculate_account_len::<Token2022Mint>(&[ | ||
| ExtensionType::InterestBearingConfig, | ||
| ])?; | ||
|
|
||
| let rent = rpc_client.get_minimum_balance_for_rent_exemption(space).await?; | ||
|
|
||
| let create_account_instruction = solana_sdk::system_instruction::create_account( | ||
| &payer.pubkey(), | ||
| &mint_keypair.pubkey(), | ||
| rent, | ||
| space as u64, | ||
| &spl_token_2022::id(), | ||
| ); | ||
|
|
||
| let initialize_interest_bearing_instruction = | ||
| initialize(&spl_token_2022::id(), &mint_keypair.pubkey(), Some(payer.pubkey()), 10)?; | ||
|
|
||
| let initialize_mint_instruction = token_2022_instruction::initialize_mint2( | ||
| &spl_token_2022::id(), | ||
| &mint_keypair.pubkey(), | ||
| &payer.pubkey(), | ||
| Some(&payer.pubkey()), | ||
| decimals, | ||
| )?; | ||
|
|
||
| let recent_blockhash = rpc_client.get_latest_blockhash().await?; | ||
|
|
||
| let transaction = Transaction::new_signed_with_payer( | ||
| &[ | ||
| create_account_instruction, | ||
| initialize_interest_bearing_instruction, | ||
| initialize_mint_instruction, | ||
| ], | ||
| Some(&payer.pubkey()), | ||
| &[payer, mint_keypair], | ||
| recent_blockhash, | ||
| ); | ||
|
|
||
| rpc_client.send_and_confirm_transaction(&transaction).await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Create a manual token account with MemoTransfer extension | ||
| pub async fn create_token_account_with_memo_transfer( | ||
| rpc_client: &Arc<RpcClient>, | ||
| payer: &Keypair, | ||
| token_account_keypair: &Keypair, | ||
| mint: &Pubkey, | ||
| owner: &Keypair, | ||
| ) -> Result<()> { | ||
| if (rpc_client.get_account(&token_account_keypair.pubkey()).await).is_ok() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| // Calculate space for token accounts with MemoTransfer extension | ||
| // Also include TransferFeeAmount if the mint has TransferFeeConfig | ||
| // (The USDC mint 2022 has TransferFeeConfig, so we need to account for it) | ||
| let account_space = ExtensionType::try_calculate_account_len::<Token2022Account>(&[ | ||
| ExtensionType::MemoTransfer, | ||
| ExtensionType::TransferFeeAmount, | ||
| ])?; | ||
| let rent = rpc_client.get_minimum_balance_for_rent_exemption(account_space).await?; | ||
|
|
||
| let create_account_instruction = solana_sdk::system_instruction::create_account( | ||
| &payer.pubkey(), | ||
| &token_account_keypair.pubkey(), | ||
| rent, | ||
| account_space as u64, | ||
| &spl_token_2022::id(), | ||
| ); | ||
|
|
||
| // Initialize MemoTransfer account extension (requires memo for transfers) | ||
| let initialize_memo_transfer_instruction = | ||
| spl_token_2022::extension::memo_transfer::instruction::enable_required_transfer_memos( | ||
| &spl_token_2022::id(), | ||
| &token_account_keypair.pubkey(), | ||
| &owner.pubkey(), | ||
| &[&owner.pubkey()], | ||
| )?; | ||
|
|
||
| let initialize_account_instruction = token_2022_instruction::initialize_account3( | ||
| &spl_token_2022::id(), | ||
| &token_account_keypair.pubkey(), | ||
| mint, | ||
| &owner.pubkey(), | ||
| )?; | ||
|
|
||
| let recent_blockhash = rpc_client.get_latest_blockhash().await?; | ||
| let transaction = Transaction::new_signed_with_payer( | ||
| &[ | ||
| create_account_instruction, | ||
| initialize_account_instruction, | ||
| initialize_memo_transfer_instruction, | ||
| ], | ||
| Some(&payer.pubkey()), | ||
| &[payer, token_account_keypair, owner], | ||
| recent_blockhash, | ||
| ); | ||
|
|
||
| rpc_client.send_and_confirm_transaction(&transaction).await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub async fn mint_tokens_to_account( | ||
| rpc_client: &Arc<RpcClient>, | ||
| payer: &Keypair, | ||
| mint: &Pubkey, | ||
| token_account: &Pubkey, | ||
| mint_authority: &Keypair, | ||
| amount: Option<u64>, | ||
| ) -> Result<()> { | ||
| let amount = amount.unwrap_or_else(|| { | ||
| 1_000_000 * 10_u64.pow(USDCMintTestHelper::get_test_usdc_mint_decimals() as u32) | ||
| }); | ||
|
|
||
| let instruction = token_2022_instruction::mint_to( | ||
| &spl_token_2022::id(), | ||
| mint, | ||
| token_account, | ||
| &mint_authority.pubkey(), | ||
| &[], | ||
| amount, | ||
| )?; | ||
|
|
||
| let recent_blockhash = rpc_client.get_latest_blockhash().await?; | ||
| let transaction = Transaction::new_signed_with_payer( | ||
| &[instruction], | ||
| Some(&payer.pubkey()), | ||
| &[payer, mint_authority], | ||
| recent_blockhash, | ||
| ); | ||
|
|
||
| rpc_client.send_and_confirm_transaction(&transaction).await?; | ||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [78,63,83,18,2,4,230,26,94,142,178,78,179,23,4,195,116,211,25,214,9,31,129,226,122,194,246,64,215,104,16,86,146,214,141,14,252,8,190,24,86,19,255,157,82,235,44,165,148,6,30,152,175,62,180,176,51,161,19,28,22,83,141,46] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [88,238,182,173,120,156,180,249,148,252,218,221,159,84,55,159,191,243,121,129,173,14,168,89,104,182,108,47,177,47,225,222,120,20,240,168,32,222,220,197,136,143,185,82,29,124,70,158,138,58,140,15,125,198,38,156,255,86,143,167,1,165,96,195] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you want to add additional tests for other methods than
signTransactionIfPaid?