123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- mod setup;
- use {
- crate::setup::TOKEN_PROGRAM_ID,
- mollusk_svm::{result::Check, Mollusk},
- pinocchio_token_interface::{
- error::TokenError,
- instruction::TokenInstruction,
- native_mint,
- state::{
- account::Account as TokenAccount, account_state::AccountState, load_mut_unchecked,
- },
- },
- solana_account::Account,
- solana_instruction::{error::InstructionError, AccountMeta, Instruction},
- solana_program_error::ProgramError,
- solana_program_pack::Pack,
- solana_pubkey::Pubkey,
- solana_rent::Rent,
- solana_sdk_ids::bpf_loader_upgradeable,
- };
- fn create_token_account(
- mint: &Pubkey,
- owner: &Pubkey,
- is_native: bool,
- amount: u64,
- program_owner: &Pubkey,
- ) -> Account {
- let space = size_of::<TokenAccount>();
- let mut lamports = Rent::default().minimum_balance(space);
- let mut data: Vec<u8> = vec![0u8; space];
- let token = unsafe { load_mut_unchecked::<TokenAccount>(data.as_mut_slice()).unwrap() };
- token.set_account_state(AccountState::Initialized);
- token.mint = *mint.as_array();
- token.owner = *owner.as_array();
- token.set_amount(amount);
- token.set_native(is_native);
- if is_native {
- token.set_native_amount(lamports);
- lamports = lamports.saturating_add(amount);
- }
- Account {
- lamports,
- data,
- owner: *program_owner,
- executable: false,
- ..Default::default()
- }
- }
- /// Creates a Mollusk instance with the default feature set.
- fn mollusk() -> Mollusk {
- let mut mollusk = Mollusk::default();
- mollusk.add_program(
- &TOKEN_PROGRAM_ID,
- "pinocchio_token_program",
- &bpf_loader_upgradeable::id(),
- );
- mollusk
- }
- fn unwrap_lamports_instruction(
- source: &Pubkey,
- destination: &Pubkey,
- authority: &Pubkey,
- amount: Option<u64>,
- ) -> Result<Instruction, ProgramError> {
- let accounts = vec![
- AccountMeta::new(*source, false),
- AccountMeta::new(*destination, false),
- AccountMeta::new_readonly(*authority, true),
- ];
- // Start with the batch discriminator
- let mut data: Vec<u8> = vec![TokenInstruction::UnwrapLamports as u8];
- if let Some(amount) = amount {
- data.push(1);
- data.extend_from_slice(&amount.to_le_bytes());
- } else {
- data.push(0);
- }
- Ok(Instruction {
- program_id: spl_token_interface::ID,
- data,
- accounts,
- })
- }
- #[test]
- fn unwrap_lamports() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- let destination_account_key = Pubkey::new_unique();
- // native account:
- // - amount: 2_000_000_000
- let source_account_key = Pubkey::new_unique();
- let source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 2_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &authority_key,
- None,
- )
- .unwrap();
- // It should succeed to unwrap 2_000_000_000 lamports.
- let result = mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, Account::default()),
- (authority_key, Account::default()),
- ],
- &[
- Check::success(),
- Check::account(&destination_account_key)
- .lamports(2_000_000_000)
- .build(),
- Check::account(&source_account_key)
- .lamports(Rent::default().minimum_balance(size_of::<TokenAccount>()))
- .build(),
- ],
- );
- // And the remaining amount must be 0.
- let account = result.get_account(&source_account_key);
- assert!(account.is_some());
- let account = account.unwrap();
- let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
- assert_eq!(token_account.amount, 0);
- }
- #[test]
- fn unwrap_lamports_with_amount() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- let destination_account_key = Pubkey::new_unique();
- // native account:
- // - amount: 2_000_000_000
- let source_account_key = Pubkey::new_unique();
- let source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 2_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &authority_key,
- Some(2_000_000_000),
- )
- .unwrap();
- // It should succeed to unwrap 2_000_000_000 lamports.
- let result = mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, Account::default()),
- (authority_key, Account::default()),
- ],
- &[
- Check::success(),
- Check::account(&destination_account_key)
- .lamports(2_000_000_000)
- .build(),
- Check::account(&source_account_key)
- .lamports(Rent::default().minimum_balance(size_of::<TokenAccount>()))
- .build(),
- ],
- );
- // And the remaining amount must be 0.
- let account = result.get_account(&source_account_key);
- assert!(account.is_some());
- let account = account.unwrap();
- let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
- assert_eq!(token_account.amount, 0);
- }
- #[test]
- fn fail_unwrap_lamports_with_insufficient_funds() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- let destination_account_key = Pubkey::new_unique();
- // native account:
- // - amount: 1_000_000_000
- let source_account_key = Pubkey::new_unique();
- let source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 1_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &authority_key,
- Some(2_000_000_000),
- )
- .unwrap();
- // When we try to unwrap 2_000_000_000 lamports, we expect a
- // `TokenError::InsufficientFunds` error.
- mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, Account::default()),
- (authority_key, Account::default()),
- ],
- &[Check::err(ProgramError::Custom(
- TokenError::InsufficientFunds as u32,
- ))],
- );
- }
- #[test]
- fn unwrap_lamports_with_parial_amount() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- let destination_account_key = Pubkey::new_unique();
- // native account:
- // - amount: 2_000_000_000
- let source_account_key = Pubkey::new_unique();
- let source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 2_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &authority_key,
- Some(1_000_000_000),
- )
- .unwrap();
- // It should succeed to unwrap 1_000_000_000 lamports.
- let result = mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, Account::default()),
- (authority_key, Account::default()),
- ],
- &[
- Check::success(),
- Check::account(&destination_account_key)
- .lamports(1_000_000_000)
- .build(),
- Check::account(&source_account_key)
- .lamports(
- Rent::default().minimum_balance(size_of::<TokenAccount>()) + 1_000_000_000,
- )
- .build(),
- ],
- );
- // And the remaining amount must be 1_000_000_000.
- let account = result.get_account(&source_account_key);
- assert!(account.is_some());
- let account = account.unwrap();
- let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
- assert_eq!(token_account.amount, 1_000_000_000);
- }
- #[test]
- fn fail_unwrap_lamports_with_invalid_authority() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- let destination_account_key = Pubkey::new_unique();
- let fake_authority_key = Pubkey::new_unique();
- // native account:
- // - amount: 1_000_000_000
- let source_account_key = Pubkey::new_unique();
- let source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 1_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &fake_authority_key, // <-- wrong authority
- Some(2_000_000_000),
- )
- .unwrap();
- // When we try to unwrap lamports with an invalid authority, we expect a
- // `TokenError::OwnerMismatch` error.
- mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, Account::default()),
- (fake_authority_key, Account::default()),
- ],
- &[Check::err(ProgramError::Custom(
- TokenError::OwnerMismatch as u32,
- ))],
- );
- }
- #[test]
- fn fail_unwrap_lamports_with_non_native_account() {
- let mint = Pubkey::new_unique();
- let authority_key = Pubkey::new_unique();
- let destination_account_key = Pubkey::new_unique();
- // non-native account:
- // - amount: 2_000_000_000
- let source_account_key = Pubkey::new_unique();
- let mut source_account = create_token_account(
- &mint,
- &authority_key,
- false, // <-- non-native account
- 2_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- source_account.lamports += 2_000_000_000;
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &authority_key,
- Some(1_000_000_000),
- )
- .unwrap();
- // When we try to unwrap lamports from a non-native account, we expect a
- // `TokenError::NonNativeNotSupported` error.
- mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, Account::default()),
- (authority_key, Account::default()),
- ],
- &[Check::err(ProgramError::Custom(
- TokenError::NonNativeNotSupported as u32,
- ))],
- );
- }
- #[test]
- fn unwrap_lamports_with_self_transfer() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- // native account:
- // - amount: 2_000_000_000
- let source_account_key = Pubkey::new_unique();
- let source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 2_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &source_account_key, // <-- destination same as source
- &authority_key,
- Some(1_000_000_000),
- )
- .unwrap();
- // It should succeed to unwrap lamports with the same source and destination
- // accounts.
- let result = mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (authority_key, Account::default()),
- ],
- &[
- Check::success(),
- Check::account(&source_account_key)
- .lamports(
- Rent::default().minimum_balance(size_of::<TokenAccount>()) + 2_000_000_000,
- )
- .build(),
- ],
- );
- let account = result.get_account(&source_account_key);
- assert!(account.is_some());
- let account = account.unwrap();
- let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
- assert_eq!(token_account.amount, 2_000_000_000);
- }
- #[test]
- fn fail_unwrap_lamports_with_invalid_native_account() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- let destination_account_key = Pubkey::new_unique();
- let invalid_program_owner = Pubkey::new_unique();
- // native account:
- // - amount: 2_000_000_000
- let source_account_key = Pubkey::new_unique();
- let mut source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 2_000_000_000,
- &invalid_program_owner, // <-- invalid program owner
- );
- source_account.lamports += 2_000_000_000;
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &authority_key,
- Some(1_000_000_000),
- )
- .unwrap();
- // When we try to unwrap lamports with an invalid native account, we expect
- // a `InstructionError::ExternalAccountDataModified` error.
- mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, Account::default()),
- (authority_key, Account::default()),
- ],
- &[Check::instruction_err(
- InstructionError::ExternalAccountDataModified,
- )],
- );
- }
- #[test]
- fn unwrap_lamports_to_native_account() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- // native account:
- // - amount: 2_000_000_000
- let source_account_key = Pubkey::new_unique();
- let source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 2_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- // destination native account:
- // - amount: 0
- let destination_account_key = Pubkey::new_unique();
- let destination_account =
- create_token_account(&native_mint, &authority_key, true, 0, &TOKEN_PROGRAM_ID);
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &authority_key,
- None,
- )
- .unwrap();
- // It should succeed to unwrap 2_000_000_000 lamports.
- let result = mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, destination_account),
- (authority_key, Account::default()),
- ],
- &[
- Check::success(),
- Check::account(&destination_account_key)
- .lamports(
- Rent::default().minimum_balance(size_of::<TokenAccount>()) + 2_000_000_000,
- )
- .build(),
- Check::account(&source_account_key)
- .lamports(Rent::default().minimum_balance(size_of::<TokenAccount>()))
- .build(),
- ],
- );
- // And the remaining amount on the source account must be 0.
- let account = result.get_account(&source_account_key);
- assert!(account.is_some());
- let account = account.unwrap();
- let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
- assert_eq!(token_account.amount, 0);
- // And the amount on the destination account must be 0 since we transferred
- // lamports directly to the account.
- let account = result.get_account(&destination_account_key);
- assert!(account.is_some());
- let account = account.unwrap();
- let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
- assert_eq!(token_account.amount, 0);
- }
- #[test]
- fn unwrap_lamports_to_token_account() {
- let native_mint = Pubkey::new_from_array(native_mint::ID);
- let authority_key = Pubkey::new_unique();
- let non_native_mint = Pubkey::new_unique();
- // native account:
- // - amount: 2_000_000_000
- let source_account_key = Pubkey::new_unique();
- let source_account = create_token_account(
- &native_mint,
- &authority_key,
- true,
- 2_000_000_000,
- &TOKEN_PROGRAM_ID,
- );
- // destination non-native account:
- // - amount: 0
- let destination_account_key = Pubkey::new_unique();
- let destination_account = create_token_account(
- &non_native_mint,
- &authority_key,
- false,
- 0,
- &TOKEN_PROGRAM_ID,
- );
- let instruction = unwrap_lamports_instruction(
- &source_account_key,
- &destination_account_key,
- &authority_key,
- None,
- )
- .unwrap();
- // It should succeed to unwrap 2_000_000_000 lamports.
- let result = mollusk().process_and_validate_instruction(
- &instruction,
- &[
- (source_account_key, source_account),
- (destination_account_key, destination_account),
- (authority_key, Account::default()),
- ],
- &[
- Check::success(),
- Check::account(&destination_account_key)
- .lamports(
- Rent::default().minimum_balance(size_of::<TokenAccount>()) + 2_000_000_000,
- )
- .build(),
- Check::account(&source_account_key)
- .lamports(Rent::default().minimum_balance(size_of::<TokenAccount>()))
- .build(),
- ],
- );
- // And the remaining amount on the source account must be 0.
- let account = result.get_account(&source_account_key);
- assert!(account.is_some());
- let account = account.unwrap();
- let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
- assert_eq!(token_account.amount, 0);
- // And the amount on the destination account must be 0 since we transferred
- // lamports directly to the account.
- let account = result.get_account(&destination_account_key);
- assert!(account.is_some());
- let account = account.unwrap();
- let token_account = spl_token_interface::state::Account::unpack(&account.data).unwrap();
- assert_eq!(token_account.amount, 0);
- }
|