Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 01b945c

Browse files
committed
add test for offchain helper create_transfer_checked_with_fee_instruction_with_extra_metas
1 parent d39ca6a commit 01b945c

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

token/program-2022/src/offchain.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,103 @@ mod tests {
384384

385385
assert_eq!(instruction.accounts, check_metas);
386386
}
387+
388+
#[tokio::test]
389+
async fn test_create_transfer_checked_with_fee_instruction_with_extra_metas() {
390+
let source = Pubkey::new_unique();
391+
let destination = Pubkey::new_unique();
392+
let authority = Pubkey::new_unique();
393+
let amount = 100u64;
394+
let fee = 1u64;
395+
396+
let validate_state_pubkey =
397+
get_extra_account_metas_address(&MINT_PUBKEY, &TRANSFER_HOOK_PROGRAM_ID);
398+
let extra_meta_3_pubkey = Pubkey::find_program_address(
399+
&[
400+
source.as_ref(),
401+
destination.as_ref(),
402+
validate_state_pubkey.as_ref(),
403+
],
404+
&TRANSFER_HOOK_PROGRAM_ID,
405+
)
406+
.0;
407+
let extra_meta_4_pubkey = Pubkey::find_program_address(
408+
&[
409+
amount.to_le_bytes().as_ref(),
410+
destination.as_ref(),
411+
EXTRA_META_1.as_ref(),
412+
extra_meta_3_pubkey.as_ref(),
413+
],
414+
&TRANSFER_HOOK_PROGRAM_ID,
415+
)
416+
.0;
417+
418+
let instruction = create_transfer_checked_with_fee_instruction_with_extra_metas(
419+
&crate::id(),
420+
&source,
421+
&MINT_PUBKEY,
422+
&destination,
423+
&authority,
424+
&[],
425+
amount,
426+
DECIMALS,
427+
fee,
428+
mock_fetch_account_data_fn,
429+
)
430+
.await
431+
.unwrap();
432+
433+
let check_metas = [
434+
AccountMeta::new(source, false),
435+
AccountMeta::new_readonly(MINT_PUBKEY, false),
436+
AccountMeta::new(destination, false),
437+
AccountMeta::new_readonly(authority, true),
438+
AccountMeta::new_readonly(EXTRA_META_1, true),
439+
AccountMeta::new_readonly(EXTRA_META_2, true),
440+
AccountMeta::new(extra_meta_3_pubkey, false),
441+
AccountMeta::new(extra_meta_4_pubkey, false),
442+
AccountMeta::new_readonly(TRANSFER_HOOK_PROGRAM_ID, false),
443+
AccountMeta::new_readonly(validate_state_pubkey, false),
444+
];
445+
446+
assert_eq!(instruction.accounts, check_metas);
447+
448+
// With additional signers
449+
let signer_1 = Pubkey::new_unique();
450+
let signer_2 = Pubkey::new_unique();
451+
let signer_3 = Pubkey::new_unique();
452+
453+
let instruction = create_transfer_checked_with_fee_instruction_with_extra_metas(
454+
&crate::id(),
455+
&source,
456+
&MINT_PUBKEY,
457+
&destination,
458+
&authority,
459+
&[&signer_1, &signer_2, &signer_3],
460+
amount,
461+
DECIMALS,
462+
fee,
463+
mock_fetch_account_data_fn,
464+
)
465+
.await
466+
.unwrap();
467+
468+
let check_metas = [
469+
AccountMeta::new(source, false),
470+
AccountMeta::new_readonly(MINT_PUBKEY, false),
471+
AccountMeta::new(destination, false),
472+
AccountMeta::new_readonly(authority, false), // False because of additional signers
473+
AccountMeta::new_readonly(signer_1, true),
474+
AccountMeta::new_readonly(signer_2, true),
475+
AccountMeta::new_readonly(signer_3, true),
476+
AccountMeta::new_readonly(EXTRA_META_1, true),
477+
AccountMeta::new_readonly(EXTRA_META_2, true),
478+
AccountMeta::new(extra_meta_3_pubkey, false),
479+
AccountMeta::new(extra_meta_4_pubkey, false),
480+
AccountMeta::new_readonly(TRANSFER_HOOK_PROGRAM_ID, false),
481+
AccountMeta::new_readonly(validate_state_pubkey, false),
482+
];
483+
484+
assert_eq!(instruction.accounts, check_metas);
485+
}
387486
}

0 commit comments

Comments
 (0)