| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465 |
- // Mark this test as SBF-only due to current `ProgramTest` limitations when
- // CPIing into the system program
- #![cfg(feature = "test-sbf")]
- use {
- solana_program_test::{processor, tokio, ProgramTest},
- solana_sdk::{
- account::Account as SolanaAccount,
- account_info::AccountInfo,
- entrypoint::ProgramResult,
- instruction::{AccountMeta, InstructionError},
- program_error::ProgramError,
- program_option::COption,
- pubkey::Pubkey,
- signature::Signer,
- signer::keypair::Keypair,
- sysvar,
- transaction::{Transaction, TransactionError},
- },
- solana_system_interface::instruction as system_instruction,
- spl_tlv_account_resolution::{
- account::ExtraAccountMeta, error::AccountResolutionError, seeds::Seed,
- state::ExtraAccountMetaList,
- },
- spl_token_2022::{
- extension::{
- transfer_hook::TransferHookAccount, BaseStateWithExtensionsMut, ExtensionType,
- StateWithExtensionsMut,
- },
- state::{Account, AccountState, Mint},
- },
- spl_transfer_hook_interface::{
- error::TransferHookError,
- get_extra_account_metas_address,
- instruction::{
- execute_with_extra_account_metas, initialize_extra_account_meta_list,
- update_extra_account_meta_list,
- },
- onchain,
- },
- };
- fn setup(program_id: &Pubkey) -> ProgramTest {
- let mut program_test = ProgramTest::new(
- "spl_transfer_hook_example",
- *program_id,
- processor!(spl_transfer_hook_example::processor::process),
- );
- program_test.prefer_bpf(false); // simplicity in the build
- program_test.add_program(
- "spl_token_2022",
- spl_token_2022::id(),
- processor!(spl_token_2022::processor::Processor::process),
- );
- program_test
- }
- #[allow(clippy::too_many_arguments)]
- fn setup_token_accounts(
- program_test: &mut ProgramTest,
- program_id: &Pubkey,
- mint_address: &Pubkey,
- mint_authority: &Pubkey,
- source: &Pubkey,
- destination: &Pubkey,
- owner: &Pubkey,
- decimals: u8,
- transferring: bool,
- ) {
- // add mint, source, and destination accounts by hand to always force
- // the "transferring" flag to true
- let mint_size = ExtensionType::try_calculate_account_len::<Mint>(&[]).unwrap();
- let mut mint_data = vec![0; mint_size];
- let mut state = StateWithExtensionsMut::<Mint>::unpack_uninitialized(&mut mint_data).unwrap();
- let token_amount = 1_000_000_000_000;
- state.base = Mint {
- mint_authority: COption::Some(*mint_authority),
- supply: token_amount,
- decimals,
- is_initialized: true,
- freeze_authority: COption::None,
- };
- state.pack_base();
- program_test.add_account(
- *mint_address,
- SolanaAccount {
- lamports: 1_000_000_000,
- data: mint_data,
- owner: *program_id,
- ..SolanaAccount::default()
- },
- );
- let account_size =
- ExtensionType::try_calculate_account_len::<Account>(&[ExtensionType::TransferHookAccount])
- .unwrap();
- let mut account_data = vec![0; account_size];
- let mut state =
- StateWithExtensionsMut::<Account>::unpack_uninitialized(&mut account_data).unwrap();
- let extension = state.init_extension::<TransferHookAccount>(true).unwrap();
- extension.transferring = transferring.into();
- let token_amount = 1_000_000_000_000;
- state.base = Account {
- mint: *mint_address,
- owner: *owner,
- amount: token_amount,
- delegate: COption::None,
- state: AccountState::Initialized,
- is_native: COption::None,
- delegated_amount: 0,
- close_authority: COption::None,
- };
- state.pack_base();
- state.init_account_type().unwrap();
- program_test.add_account(
- *source,
- SolanaAccount {
- lamports: 1_000_000_000,
- data: account_data.clone(),
- owner: *program_id,
- ..SolanaAccount::default()
- },
- );
- program_test.add_account(
- *destination,
- SolanaAccount {
- lamports: 1_000_000_000,
- data: account_data,
- owner: *program_id,
- ..SolanaAccount::default()
- },
- );
- }
- #[tokio::test]
- async fn success_execute() {
- let program_id = Pubkey::new_unique();
- let mut program_test = setup(&program_id);
- let token_program_id = spl_token_2022::id();
- let wallet = Keypair::new();
- let mint_address = spl_transfer_hook_example::mint::id();
- let mint_authority = Keypair::new();
- let mint_authority_pubkey = mint_authority.pubkey();
- let source = Pubkey::new_unique();
- let destination = Pubkey::new_unique();
- let decimals = 2;
- let amount = 0u64;
- setup_token_accounts(
- &mut program_test,
- &token_program_id,
- &mint_address,
- &mint_authority_pubkey,
- &source,
- &destination,
- &wallet.pubkey(),
- decimals,
- true,
- );
- let extra_account_metas_address = get_extra_account_metas_address(&mint_address, &program_id);
- let writable_pubkey = Pubkey::new_unique();
- let init_extra_account_metas = [
- ExtraAccountMeta::new_with_pubkey(&sysvar::instructions::id(), false, false).unwrap(),
- ExtraAccountMeta::new_with_pubkey(&mint_authority_pubkey, true, false).unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::Literal {
- bytes: b"seed-prefix".to_vec(),
- },
- Seed::AccountKey { index: 0 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::InstructionData {
- index: 8, // After instruction discriminator
- length: 8, // `u64` (amount)
- },
- Seed::AccountKey { index: 2 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_pubkey(&writable_pubkey, false, true).unwrap(),
- ];
- let extra_pda_1 = Pubkey::find_program_address(
- &[
- b"seed-prefix", // Literal prefix
- source.as_ref(), // Account at index 0
- ],
- &program_id,
- )
- .0;
- let extra_pda_2 = Pubkey::find_program_address(
- &[
- &amount.to_le_bytes(), // Instruction data bytes 8 to 16
- destination.as_ref(), // Account at index 2
- ],
- &program_id,
- )
- .0;
- let extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(extra_pda_1, false),
- AccountMeta::new(extra_pda_2, false),
- AccountMeta::new(writable_pubkey, false),
- ];
- let context = program_test.start_with_context().await;
- let rent = context.banks_client.get_rent().await.unwrap();
- let rent_lamports = rent
- .minimum_balance(ExtraAccountMetaList::size_of(init_extra_account_metas.len()).unwrap());
- let transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas_address,
- rent_lamports,
- ),
- initialize_extra_account_meta_list(
- &program_id,
- &extra_account_metas_address,
- &mint_address,
- &mint_authority_pubkey,
- &init_extra_account_metas,
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap();
- // fail with missing account
- {
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas[..2],
- amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // fail with wrong account
- {
- let extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(extra_pda_1, false),
- AccountMeta::new(extra_pda_2, false),
- AccountMeta::new(Pubkey::new_unique(), false),
- ];
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // fail with wrong PDA
- let wrong_pda_2 = Pubkey::find_program_address(
- &[
- &99u64.to_le_bytes(), // Wrong data
- destination.as_ref(),
- ],
- &program_id,
- )
- .0;
- {
- let extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(extra_pda_1, false),
- AccountMeta::new(wrong_pda_2, false),
- AccountMeta::new(writable_pubkey, false),
- ];
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // fail with not signer
- {
- let extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, false),
- AccountMeta::new(extra_pda_1, false),
- AccountMeta::new(extra_pda_2, false),
- AccountMeta::new(writable_pubkey, false),
- ];
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // success with correct params
- {
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap();
- }
- }
- #[tokio::test]
- async fn fail_incorrect_derivation() {
- let program_id = Pubkey::new_unique();
- let mut program_test = setup(&program_id);
- let token_program_id = spl_token_2022::id();
- let wallet = Keypair::new();
- let mint_address = spl_transfer_hook_example::mint::id();
- let mint_authority = Keypair::new();
- let mint_authority_pubkey = mint_authority.pubkey();
- let source = Pubkey::new_unique();
- let destination = Pubkey::new_unique();
- let decimals = 2;
- setup_token_accounts(
- &mut program_test,
- &token_program_id,
- &mint_address,
- &mint_authority_pubkey,
- &source,
- &destination,
- &wallet.pubkey(),
- decimals,
- true,
- );
- // wrong derivation
- let extra_account_metas = get_extra_account_metas_address(&program_id, &mint_address);
- let context = program_test.start_with_context().await;
- let rent = context.banks_client.get_rent().await.unwrap();
- let rent_lamports = rent.minimum_balance(ExtraAccountMetaList::size_of(0).unwrap());
- let transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas,
- rent_lamports,
- ),
- initialize_extra_account_meta_list(
- &program_id,
- &extra_account_metas,
- &mint_address,
- &mint_authority_pubkey,
- &[],
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(1, InstructionError::InvalidSeeds)
- );
- }
- #[tokio::test]
- async fn fail_incorrect_mint() {
- let program_id = Pubkey::new_unique();
- let mut program_test = setup(&program_id);
- let token_program_id = spl_token_2022::id();
- let wallet = Keypair::new();
- // wrong mint, only `spl_transfer_hook_example::mint::id()` allowed
- let mint_address = Pubkey::new_unique();
- let mint_authority = Keypair::new();
- let mint_authority_pubkey = mint_authority.pubkey();
- let source = Pubkey::new_unique();
- let destination = Pubkey::new_unique();
- let decimals = 2;
- setup_token_accounts(
- &mut program_test,
- &token_program_id,
- &mint_address,
- &mint_authority_pubkey,
- &source,
- &destination,
- &wallet.pubkey(),
- decimals,
- true,
- );
- let extra_account_metas = get_extra_account_metas_address(&mint_address, &program_id);
- let context = program_test.start_with_context().await;
- let rent = context.banks_client.get_rent().await.unwrap();
- let rent_lamports = rent.minimum_balance(ExtraAccountMetaList::size_of(0).unwrap());
- let transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas,
- rent_lamports,
- ),
- initialize_extra_account_meta_list(
- &program_id,
- &extra_account_metas,
- &mint_address,
- &mint_authority_pubkey,
- &[],
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(1, InstructionError::InvalidArgument)
- );
- }
- /// Test program to CPI into default transfer-hook-interface program
- pub fn process_instruction(
- _program_id: &Pubkey,
- accounts: &[AccountInfo],
- input: &[u8],
- ) -> ProgramResult {
- let amount = input
- .get(8..16)
- .and_then(|slice| slice.try_into().ok())
- .map(u64::from_le_bytes)
- .ok_or(ProgramError::InvalidInstructionData)?;
- onchain::invoke_execute(
- accounts[0].key,
- accounts[1].clone(),
- accounts[2].clone(),
- accounts[3].clone(),
- accounts[4].clone(),
- &accounts[5..],
- amount,
- )
- }
- #[tokio::test]
- async fn success_on_chain_invoke() {
- let hook_program_id = Pubkey::new_unique();
- let mut program_test = setup(&hook_program_id);
- let program_id = Pubkey::new_unique();
- program_test.add_program(
- "test_cpi_program",
- program_id,
- processor!(process_instruction),
- );
- let token_program_id = spl_token_2022::id();
- let wallet = Keypair::new();
- let mint_address = spl_transfer_hook_example::mint::id();
- let mint_authority = Keypair::new();
- let mint_authority_pubkey = mint_authority.pubkey();
- let source = Pubkey::new_unique();
- let destination = Pubkey::new_unique();
- let decimals = 2;
- let amount = 0u64;
- setup_token_accounts(
- &mut program_test,
- &token_program_id,
- &mint_address,
- &mint_authority_pubkey,
- &source,
- &destination,
- &wallet.pubkey(),
- decimals,
- true,
- );
- let extra_account_metas_address =
- get_extra_account_metas_address(&mint_address, &hook_program_id);
- let writable_pubkey = Pubkey::new_unique();
- let init_extra_account_metas = [
- ExtraAccountMeta::new_with_pubkey(&sysvar::instructions::id(), false, false).unwrap(),
- ExtraAccountMeta::new_with_pubkey(&mint_authority_pubkey, true, false).unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::Literal {
- bytes: b"seed-prefix".to_vec(),
- },
- Seed::AccountKey { index: 0 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::InstructionData {
- index: 8, // After instruction discriminator
- length: 8, // `u64` (amount)
- },
- Seed::AccountKey { index: 2 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_pubkey(&writable_pubkey, false, true).unwrap(),
- ];
- let extra_pda_1 = Pubkey::find_program_address(
- &[
- b"seed-prefix", // Literal prefix
- source.as_ref(), // Account at index 0
- ],
- &hook_program_id,
- )
- .0;
- let extra_pda_2 = Pubkey::find_program_address(
- &[
- &amount.to_le_bytes(), // Instruction data bytes 8 to 16
- destination.as_ref(), // Account at index 2
- ],
- &hook_program_id,
- )
- .0;
- let extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(extra_pda_1, false),
- AccountMeta::new(extra_pda_2, false),
- AccountMeta::new(writable_pubkey, false),
- ];
- let context = program_test.start_with_context().await;
- let rent = context.banks_client.get_rent().await.unwrap();
- let rent_lamports = rent
- .minimum_balance(ExtraAccountMetaList::size_of(init_extra_account_metas.len()).unwrap());
- let transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas_address,
- rent_lamports,
- ),
- initialize_extra_account_meta_list(
- &hook_program_id,
- &extra_account_metas_address,
- &mint_address,
- &mint_authority_pubkey,
- &init_extra_account_metas,
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap();
- // easier to hack this up!
- let mut test_instruction = execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- amount,
- );
- test_instruction
- .accounts
- .insert(0, AccountMeta::new_readonly(hook_program_id, false));
- let transaction = Transaction::new_signed_with_payer(
- &[test_instruction],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap();
- }
- #[tokio::test]
- async fn fail_without_transferring_flag() {
- let program_id = Pubkey::new_unique();
- let mut program_test = setup(&program_id);
- let token_program_id = spl_token_2022::id();
- let wallet = Keypair::new();
- let mint_address = spl_transfer_hook_example::mint::id();
- let mint_authority = Keypair::new();
- let mint_authority_pubkey = mint_authority.pubkey();
- let source = Pubkey::new_unique();
- let destination = Pubkey::new_unique();
- let decimals = 2;
- setup_token_accounts(
- &mut program_test,
- &token_program_id,
- &mint_address,
- &mint_authority_pubkey,
- &source,
- &destination,
- &wallet.pubkey(),
- decimals,
- false,
- );
- let extra_account_metas_address = get_extra_account_metas_address(&mint_address, &program_id);
- let extra_account_metas = [];
- let init_extra_account_metas = [];
- let context = program_test.start_with_context().await;
- let rent = context.banks_client.get_rent().await.unwrap();
- let rent_lamports = rent
- .minimum_balance(ExtraAccountMetaList::size_of(init_extra_account_metas.len()).unwrap());
- let transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas_address,
- rent_lamports,
- ),
- initialize_extra_account_meta_list(
- &program_id,
- &extra_account_metas_address,
- &mint_address,
- &mint_authority_pubkey,
- &init_extra_account_metas,
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap();
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- 0,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(TransferHookError::ProgramCalledOutsideOfTransfer as u32)
- )
- );
- }
- #[tokio::test]
- async fn success_on_chain_invoke_with_updated_extra_account_metas() {
- let hook_program_id = Pubkey::new_unique();
- let mut program_test = setup(&hook_program_id);
- let program_id = Pubkey::new_unique();
- program_test.add_program(
- "test_cpi_program",
- program_id,
- processor!(process_instruction),
- );
- let token_program_id = spl_token_2022::id();
- let wallet = Keypair::new();
- let mint_address = spl_transfer_hook_example::mint::id();
- let mint_authority = Keypair::new();
- let mint_authority_pubkey = mint_authority.pubkey();
- let source = Pubkey::new_unique();
- let destination = Pubkey::new_unique();
- let decimals = 2;
- let amount = 0u64;
- setup_token_accounts(
- &mut program_test,
- &token_program_id,
- &mint_address,
- &mint_authority_pubkey,
- &source,
- &destination,
- &wallet.pubkey(),
- decimals,
- true,
- );
- let extra_account_metas_address =
- get_extra_account_metas_address(&mint_address, &hook_program_id);
- let writable_pubkey = Pubkey::new_unique();
- // Create an initial account metas list
- let init_extra_account_metas = [
- ExtraAccountMeta::new_with_pubkey(&sysvar::instructions::id(), false, false).unwrap(),
- ExtraAccountMeta::new_with_pubkey(&mint_authority_pubkey, true, false).unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::Literal {
- bytes: b"init-seed-prefix".to_vec(),
- },
- Seed::AccountKey { index: 0 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::InstructionData {
- index: 8, // After instruction discriminator
- length: 8, // `u64` (amount)
- },
- Seed::AccountKey { index: 2 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_pubkey(&writable_pubkey, false, true).unwrap(),
- ];
- let context = program_test.start_with_context().await;
- let rent = context.banks_client.get_rent().await.unwrap();
- let rent_lamports = rent
- .minimum_balance(ExtraAccountMetaList::size_of(init_extra_account_metas.len()).unwrap());
- let init_transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas_address,
- rent_lamports,
- ),
- initialize_extra_account_meta_list(
- &hook_program_id,
- &extra_account_metas_address,
- &mint_address,
- &mint_authority_pubkey,
- &init_extra_account_metas,
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(init_transaction)
- .await
- .unwrap();
- // Create an updated account metas list
- let updated_extra_account_metas = [
- ExtraAccountMeta::new_with_pubkey(&sysvar::instructions::id(), false, false).unwrap(),
- ExtraAccountMeta::new_with_pubkey(&mint_authority_pubkey, true, false).unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::Literal {
- bytes: b"updated-seed-prefix".to_vec(),
- },
- Seed::AccountKey { index: 0 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::InstructionData {
- index: 8, // After instruction discriminator
- length: 8, // `u64` (amount)
- },
- Seed::AccountKey { index: 2 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_pubkey(&writable_pubkey, false, true).unwrap(),
- ];
- let rent = context.banks_client.get_rent().await.unwrap();
- let rent_lamports = rent
- .minimum_balance(ExtraAccountMetaList::size_of(updated_extra_account_metas.len()).unwrap());
- let update_transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas_address,
- rent_lamports,
- ),
- update_extra_account_meta_list(
- &hook_program_id,
- &extra_account_metas_address,
- &mint_address,
- &mint_authority_pubkey,
- &updated_extra_account_metas,
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(update_transaction)
- .await
- .unwrap();
- let updated_extra_pda_1 = Pubkey::find_program_address(
- &[
- b"updated-seed-prefix", // Literal prefix
- source.as_ref(), // Account at index 0
- ],
- &hook_program_id,
- )
- .0;
- let extra_pda_2 = Pubkey::find_program_address(
- &[
- &amount.to_le_bytes(), // Instruction data bytes 8 to 16
- destination.as_ref(), // Account at index 2
- ],
- &hook_program_id,
- )
- .0;
- let test_updated_extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(updated_extra_pda_1, false),
- AccountMeta::new(extra_pda_2, false),
- AccountMeta::new(writable_pubkey, false),
- ];
- // Use updated account metas list
- let mut test_instruction = execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &test_updated_extra_account_metas,
- amount,
- );
- test_instruction
- .accounts
- .insert(0, AccountMeta::new_readonly(hook_program_id, false));
- let transaction = Transaction::new_signed_with_payer(
- &[test_instruction],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap();
- }
- #[tokio::test]
- async fn success_execute_with_updated_extra_account_metas() {
- let program_id = Pubkey::new_unique();
- let mut program_test = setup(&program_id);
- let token_program_id = spl_token_2022::id();
- let wallet = Keypair::new();
- let mint_address = spl_transfer_hook_example::mint::id();
- let mint_authority = Keypair::new();
- let mint_authority_pubkey = mint_authority.pubkey();
- let source = Pubkey::new_unique();
- let destination = Pubkey::new_unique();
- let decimals = 2;
- let amount = 0u64;
- setup_token_accounts(
- &mut program_test,
- &token_program_id,
- &mint_address,
- &mint_authority_pubkey,
- &source,
- &destination,
- &wallet.pubkey(),
- decimals,
- true,
- );
- let extra_account_metas_address = get_extra_account_metas_address(&mint_address, &program_id);
- let writable_pubkey = Pubkey::new_unique();
- let init_extra_account_metas = [
- ExtraAccountMeta::new_with_pubkey(&sysvar::instructions::id(), false, false).unwrap(),
- ExtraAccountMeta::new_with_pubkey(&mint_authority_pubkey, true, false).unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::Literal {
- bytes: b"seed-prefix".to_vec(),
- },
- Seed::AccountKey { index: 0 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::InstructionData {
- index: 8, // After instruction discriminator
- length: 8, // `u64` (amount)
- },
- Seed::AccountKey { index: 2 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_pubkey(&writable_pubkey, false, true).unwrap(),
- ];
- let extra_pda_1 = Pubkey::find_program_address(
- &[
- b"seed-prefix", // Literal prefix
- source.as_ref(), // Account at index 0
- ],
- &program_id,
- )
- .0;
- let extra_pda_2 = Pubkey::find_program_address(
- &[
- &amount.to_le_bytes(), // Instruction data bytes 8 to 16
- destination.as_ref(), // Account at index 2
- ],
- &program_id,
- )
- .0;
- let init_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(extra_pda_1, false),
- AccountMeta::new(extra_pda_2, false),
- AccountMeta::new(writable_pubkey, false),
- ];
- let context = program_test.start_with_context().await;
- let rent = context.banks_client.get_rent().await.unwrap();
- let rent_lamports = rent
- .minimum_balance(ExtraAccountMetaList::size_of(init_extra_account_metas.len()).unwrap());
- let transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas_address,
- rent_lamports,
- ),
- initialize_extra_account_meta_list(
- &program_id,
- &extra_account_metas_address,
- &mint_address,
- &mint_authority_pubkey,
- &init_extra_account_metas,
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap();
- let updated_amount = 1u64;
- let updated_writable_pubkey = Pubkey::new_unique();
- // Create updated extra account metas
- let updated_extra_account_metas = [
- ExtraAccountMeta::new_with_pubkey(&sysvar::instructions::id(), false, false).unwrap(),
- ExtraAccountMeta::new_with_pubkey(&mint_authority_pubkey, true, false).unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::Literal {
- bytes: b"updated-seed-prefix".to_vec(),
- },
- Seed::AccountKey { index: 0 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::InstructionData {
- index: 8, // After instruction discriminator
- length: 8, // `u64` (amount)
- },
- Seed::AccountKey { index: 2 },
- ],
- false,
- true,
- )
- .unwrap(),
- ExtraAccountMeta::new_with_pubkey(&updated_writable_pubkey, false, true).unwrap(),
- ExtraAccountMeta::new_with_seeds(
- &[
- Seed::Literal {
- bytes: b"new-seed-prefix".to_vec(),
- },
- Seed::AccountKey { index: 0 },
- ],
- false,
- true,
- )
- .unwrap(),
- ];
- let updated_extra_pda_1 = Pubkey::find_program_address(
- &[
- b"updated-seed-prefix", // Literal prefix
- source.as_ref(), // Account at index 0
- ],
- &program_id,
- )
- .0;
- let updated_extra_pda_2 = Pubkey::find_program_address(
- &[
- &updated_amount.to_le_bytes(), // Instruction data bytes 8 to 16
- destination.as_ref(), // Account at index 2
- ],
- &program_id,
- )
- .0;
- // add another PDA
- let new_extra_pda = Pubkey::find_program_address(
- &[
- b"new-seed-prefix", // Literal prefix
- source.as_ref(), // Account at index 0
- ],
- &program_id,
- )
- .0;
- let updated_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(updated_extra_pda_1, false),
- AccountMeta::new(updated_extra_pda_2, false),
- AccountMeta::new(updated_writable_pubkey, false),
- AccountMeta::new(new_extra_pda, false),
- ];
- let update_transaction = Transaction::new_signed_with_payer(
- &[
- system_instruction::transfer(
- &context.payer.pubkey(),
- &extra_account_metas_address,
- rent_lamports,
- ),
- update_extra_account_meta_list(
- &program_id,
- &extra_account_metas_address,
- &mint_address,
- &mint_authority_pubkey,
- &updated_extra_account_metas,
- ),
- ],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(update_transaction)
- .await
- .unwrap();
- // fail with initial account metas list
- {
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &init_account_metas,
- updated_amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // fail with missing account
- {
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &updated_account_metas[..2],
- updated_amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // fail with wrong account
- {
- let extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(updated_extra_pda_1, false),
- AccountMeta::new(updated_extra_pda_2, false),
- AccountMeta::new(Pubkey::new_unique(), false),
- ];
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- updated_amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // fail with wrong PDA
- let wrong_pda_2 = Pubkey::find_program_address(
- &[
- &99u64.to_le_bytes(), // Wrong data
- destination.as_ref(),
- ],
- &program_id,
- )
- .0;
- {
- let extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, true),
- AccountMeta::new(updated_extra_pda_1, false),
- AccountMeta::new(wrong_pda_2, false),
- AccountMeta::new(writable_pubkey, false),
- ];
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- updated_amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // fail with not signer
- {
- let extra_account_metas = [
- AccountMeta::new_readonly(sysvar::instructions::id(), false),
- AccountMeta::new_readonly(mint_authority_pubkey, false),
- AccountMeta::new(updated_extra_pda_1, false),
- AccountMeta::new(updated_extra_pda_2, false),
- AccountMeta::new(writable_pubkey, false),
- ];
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &extra_account_metas,
- updated_amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer],
- context.last_blockhash,
- );
- let error = context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap_err()
- .unwrap();
- assert_eq!(
- error,
- TransactionError::InstructionError(
- 0,
- InstructionError::Custom(AccountResolutionError::IncorrectAccount as u32),
- )
- );
- }
- // success with correct params
- {
- let transaction = Transaction::new_signed_with_payer(
- &[execute_with_extra_account_metas(
- &program_id,
- &source,
- &mint_address,
- &destination,
- &wallet.pubkey(),
- &extra_account_metas_address,
- &updated_account_metas,
- updated_amount,
- )],
- Some(&context.payer.pubkey()),
- &[&context.payer, &mint_authority],
- context.last_blockhash,
- );
- context
- .banks_client
- .process_transaction(transaction)
- .await
- .unwrap();
- }
- }
|