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

Commit 56d04fd

Browse files
committed
add test success_transfers_with_fee_using_onchain_helper
1 parent e832fe3 commit 56d04fd

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

token/program-2022-test/tests/transfer_hook.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,150 @@ async fn success_transfers_using_onchain_helper() {
965965
.unwrap();
966966
}
967967

968+
#[tokio::test]
969+
async fn success_transfers_with_fee_using_onchain_helper() {
970+
let authority = Pubkey::new_unique();
971+
let program_id = Pubkey::new_unique();
972+
let mint_a_keypair = Keypair::new();
973+
let mint_a = mint_a_keypair.pubkey();
974+
let mint_b_keypair = Keypair::new();
975+
let mint_b = mint_b_keypair.pubkey();
976+
let amount = 10;
977+
978+
let transfer_fee_config_authority = Keypair::new();
979+
let withdraw_withheld_authority = Keypair::new();
980+
let transfer_fee_basis_points = TEST_FEE_BASIS_POINTS;
981+
let maximum_fee = TEST_MAXIMUM_FEE;
982+
983+
let swap_program_id = Pubkey::new_unique();
984+
let mut program_test = setup_program_test(&program_id);
985+
program_test.add_program(
986+
"my_swap",
987+
swap_program_id,
988+
processor!(process_instruction_swap),
989+
);
990+
add_validation_account(&mut program_test, &mint_a, &program_id);
991+
add_validation_account(&mut program_test, &mint_b, &program_id);
992+
993+
let context = program_test.start_with_context().await;
994+
let context = Arc::new(tokio::sync::Mutex::new(context));
995+
let mut context_a = TestContext {
996+
context: context.clone(),
997+
token_context: None,
998+
};
999+
context_a
1000+
.init_token_with_mint_keypair_and_freeze_authority(
1001+
mint_a_keypair,
1002+
vec![
1003+
ExtensionInitializationParams::TransferHook {
1004+
authority: Some(authority),
1005+
program_id: Some(program_id),
1006+
},
1007+
ExtensionInitializationParams::TransferFeeConfig {
1008+
transfer_fee_config_authority: transfer_fee_config_authority.pubkey().into(),
1009+
withdraw_withheld_authority: withdraw_withheld_authority.pubkey().into(),
1010+
transfer_fee_basis_points,
1011+
maximum_fee,
1012+
},
1013+
],
1014+
None,
1015+
)
1016+
.await
1017+
.unwrap();
1018+
let token_a_context = context_a.token_context.unwrap();
1019+
let (source_a_account, destination_a_account) =
1020+
setup_accounts(&token_a_context, Keypair::new(), Keypair::new(), amount).await;
1021+
let authority_a = token_a_context.alice;
1022+
let token_a = token_a_context.token;
1023+
let mut context_b = TestContext {
1024+
context,
1025+
token_context: None,
1026+
};
1027+
context_b
1028+
.init_token_with_mint_keypair_and_freeze_authority(
1029+
mint_b_keypair,
1030+
vec![
1031+
ExtensionInitializationParams::TransferHook {
1032+
authority: Some(authority),
1033+
program_id: Some(program_id),
1034+
},
1035+
ExtensionInitializationParams::TransferFeeConfig {
1036+
transfer_fee_config_authority: transfer_fee_config_authority.pubkey().into(),
1037+
withdraw_withheld_authority: withdraw_withheld_authority.pubkey().into(),
1038+
transfer_fee_basis_points,
1039+
maximum_fee,
1040+
},
1041+
],
1042+
None,
1043+
)
1044+
.await
1045+
.unwrap();
1046+
let token_b_context = context_b.token_context.unwrap();
1047+
let (source_b_account, destination_b_account) =
1048+
setup_accounts(&token_b_context, Keypair::new(), Keypair::new(), amount).await;
1049+
let authority_b = token_b_context.alice;
1050+
let account_metas = vec![
1051+
AccountMeta::new(source_a_account, false),
1052+
AccountMeta::new_readonly(mint_a, false),
1053+
AccountMeta::new(destination_a_account, false),
1054+
AccountMeta::new_readonly(authority_a.pubkey(), true),
1055+
AccountMeta::new_readonly(spl_token_2022::id(), false),
1056+
AccountMeta::new(source_b_account, false),
1057+
AccountMeta::new_readonly(mint_b, false),
1058+
AccountMeta::new(destination_b_account, false),
1059+
AccountMeta::new_readonly(authority_b.pubkey(), true),
1060+
AccountMeta::new_readonly(spl_token_2022::id(), false),
1061+
];
1062+
1063+
let mut instruction = Instruction::new_with_bytes(swap_program_id, &[], account_metas);
1064+
1065+
add_extra_account_metas_for_execute(
1066+
&mut instruction,
1067+
&program_id,
1068+
&source_a_account,
1069+
&mint_a,
1070+
&destination_a_account,
1071+
&authority_a.pubkey(),
1072+
amount,
1073+
|address| {
1074+
token_a.get_account(address).map_ok_or_else(
1075+
|e| match e {
1076+
TokenClientError::AccountNotFound => Ok(None),
1077+
_ => Err(offchain::AccountFetchError::from(e)),
1078+
},
1079+
|acc| Ok(Some(acc.data)),
1080+
)
1081+
},
1082+
)
1083+
.await
1084+
.unwrap();
1085+
add_extra_account_metas_for_execute(
1086+
&mut instruction,
1087+
&program_id,
1088+
&source_b_account,
1089+
&mint_b,
1090+
&destination_b_account,
1091+
&authority_b.pubkey(),
1092+
amount,
1093+
|address| {
1094+
token_a.get_account(address).map_ok_or_else(
1095+
|e| match e {
1096+
TokenClientError::AccountNotFound => Ok(None),
1097+
_ => Err(offchain::AccountFetchError::from(e)),
1098+
},
1099+
|acc| Ok(Some(acc.data)),
1100+
)
1101+
},
1102+
)
1103+
.await
1104+
.unwrap();
1105+
1106+
token_a
1107+
.process_ixs(&[instruction], &[&authority_a, &authority_b])
1108+
.await
1109+
.unwrap();
1110+
}
1111+
9681112
#[tokio::test]
9691113
async fn success_confidential_transfer() {
9701114
let authority = Keypair::new();

0 commit comments

Comments
 (0)