123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- #![cfg(feature = "test-sbf")]
- mod action;
- use {
- solana_program_test::{processor, tokio, ProgramTest},
- solana_sdk::{
- program_pack::Pack,
- pubkey::Pubkey,
- signature::{Keypair, Signer},
- system_instruction,
- transaction::Transaction,
- },
- spl_token::{
- id, instruction,
- processor::Processor,
- state::{Account, Mint},
- },
- };
- const TRANSFER_AMOUNT: u64 = 1_000_000_000_000_000;
- #[tokio::test]
- async fn initialize_mint() {
- let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
- pt.set_compute_max_units(5_000); // last known 2252
- let (mut banks_client, payer, recent_blockhash) = pt.start().await;
- let owner_key = Pubkey::new_unique();
- let mint = Keypair::new();
- let decimals = 9;
- let rent = banks_client.get_rent().await.unwrap();
- let mint_rent = rent.minimum_balance(Mint::LEN);
- let transaction = Transaction::new_signed_with_payer(
- &[system_instruction::create_account(
- &payer.pubkey(),
- &mint.pubkey(),
- mint_rent,
- Mint::LEN as u64,
- &id(),
- )],
- Some(&payer.pubkey()),
- &[&payer, &mint],
- recent_blockhash,
- );
- banks_client.process_transaction(transaction).await.unwrap();
- let transaction = Transaction::new_signed_with_payer(
- &[
- instruction::initialize_mint(&id(), &mint.pubkey(), &owner_key, None, decimals)
- .unwrap(),
- ],
- Some(&payer.pubkey()),
- &[&payer],
- recent_blockhash,
- );
- banks_client.process_transaction(transaction).await.unwrap();
- }
- #[tokio::test]
- async fn initialize_account() {
- let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
- pt.set_compute_max_units(6_000); // last known 3284
- let (mut banks_client, payer, recent_blockhash) = pt.start().await;
- let owner = Keypair::new();
- let mint = Keypair::new();
- let account = Keypair::new();
- let decimals = 9;
- action::create_mint(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &mint,
- &owner.pubkey(),
- decimals,
- )
- .await
- .unwrap();
- let rent = banks_client.get_rent().await.unwrap();
- let account_rent = rent.minimum_balance(Account::LEN);
- let transaction = Transaction::new_signed_with_payer(
- &[system_instruction::create_account(
- &payer.pubkey(),
- &account.pubkey(),
- account_rent,
- Account::LEN as u64,
- &id(),
- )],
- Some(&payer.pubkey()),
- &[&payer, &account],
- recent_blockhash,
- );
- banks_client.process_transaction(transaction).await.unwrap();
- let transaction = Transaction::new_signed_with_payer(
- &[instruction::initialize_account(
- &id(),
- &account.pubkey(),
- &mint.pubkey(),
- &owner.pubkey(),
- )
- .unwrap()],
- Some(&payer.pubkey()),
- &[&payer],
- recent_blockhash,
- );
- banks_client.process_transaction(transaction).await.unwrap();
- }
- #[tokio::test]
- async fn mint_to() {
- let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
- pt.set_compute_max_units(6_000); // last known 2668
- let (mut banks_client, payer, recent_blockhash) = pt.start().await;
- let owner = Keypair::new();
- let mint = Keypair::new();
- let account = Keypair::new();
- let decimals = 9;
- action::create_mint(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &mint,
- &owner.pubkey(),
- decimals,
- )
- .await
- .unwrap();
- action::create_account(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &account,
- &mint.pubkey(),
- &owner.pubkey(),
- )
- .await
- .unwrap();
- let transaction = Transaction::new_signed_with_payer(
- &[instruction::mint_to(
- &id(),
- &mint.pubkey(),
- &account.pubkey(),
- &owner.pubkey(),
- &[],
- TRANSFER_AMOUNT,
- )
- .unwrap()],
- Some(&payer.pubkey()),
- &[&payer, &owner],
- recent_blockhash,
- );
- banks_client.process_transaction(transaction).await.unwrap();
- }
- #[tokio::test]
- async fn transfer() {
- let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
- pt.set_compute_max_units(7_000); // last known 2972
- let (mut banks_client, payer, recent_blockhash) = pt.start().await;
- let owner = Keypair::new();
- let mint = Keypair::new();
- let source = Keypair::new();
- let destination = Keypair::new();
- let decimals = 9;
- action::create_mint(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &mint,
- &owner.pubkey(),
- decimals,
- )
- .await
- .unwrap();
- action::create_account(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &source,
- &mint.pubkey(),
- &owner.pubkey(),
- )
- .await
- .unwrap();
- action::create_account(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &destination,
- &mint.pubkey(),
- &owner.pubkey(),
- )
- .await
- .unwrap();
- action::mint_to(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &mint.pubkey(),
- &source.pubkey(),
- &owner,
- TRANSFER_AMOUNT,
- )
- .await
- .unwrap();
- action::transfer(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &source.pubkey(),
- &destination.pubkey(),
- &owner,
- TRANSFER_AMOUNT,
- )
- .await
- .unwrap();
- }
- #[tokio::test]
- async fn burn() {
- let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
- pt.set_compute_max_units(6_000); // last known 2655
- let (mut banks_client, payer, recent_blockhash) = pt.start().await;
- let owner = Keypair::new();
- let mint = Keypair::new();
- let account = Keypair::new();
- let decimals = 9;
- action::create_mint(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &mint,
- &owner.pubkey(),
- decimals,
- )
- .await
- .unwrap();
- action::create_account(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &account,
- &mint.pubkey(),
- &owner.pubkey(),
- )
- .await
- .unwrap();
- action::mint_to(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &mint.pubkey(),
- &account.pubkey(),
- &owner,
- TRANSFER_AMOUNT,
- )
- .await
- .unwrap();
- action::burn(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &mint.pubkey(),
- &account.pubkey(),
- &owner,
- TRANSFER_AMOUNT,
- )
- .await
- .unwrap();
- }
- #[tokio::test]
- async fn close_account() {
- let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
- pt.set_compute_max_units(6_000); // last known 1783
- let (mut banks_client, payer, recent_blockhash) = pt.start().await;
- let owner = Keypair::new();
- let mint = Keypair::new();
- let account = Keypair::new();
- let decimals = 9;
- action::create_mint(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &mint,
- &owner.pubkey(),
- decimals,
- )
- .await
- .unwrap();
- action::create_account(
- &mut banks_client,
- &payer,
- recent_blockhash,
- &account,
- &mint.pubkey(),
- &owner.pubkey(),
- )
- .await
- .unwrap();
- let transaction = Transaction::new_signed_with_payer(
- &[instruction::close_account(
- &id(),
- &account.pubkey(),
- &owner.pubkey(),
- &owner.pubkey(),
- &[],
- )
- .unwrap()],
- Some(&payer.pubkey()),
- &[&payer, &owner],
- recent_blockhash,
- );
- banks_client.process_transaction(transaction).await.unwrap();
- }
|