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

[token-cli] Add support for transfer-hook account resolution for transfers with the transfer-fee extension. #7171

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add cli test transfer_hook_with_transfer_fee
  • Loading branch information
tonton-sol committed Aug 23, 2024
commit ebc47d2387d9588f5206e3e35e36aa169df1d732
102 changes: 102 additions & 0 deletions token/cli/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ async fn main() {
async_trial!(group_pointer, test_validator, payer),
async_trial!(group_member_pointer, test_validator, payer),
async_trial!(transfer_hook, test_validator, payer),
async_trial!(transfer_hook_with_transfer_fee, test_validator, payer),
async_trial!(metadata, test_validator, payer),
async_trial!(group, test_validator, payer),
async_trial!(confidential_transfer_with_fee, test_validator, payer),
Expand Down Expand Up @@ -3872,6 +3873,107 @@ async fn transfer_hook(test_validator: &TestValidator, payer: &Keypair) {
assert_eq!(extension.program_id, None.try_into().unwrap());
}

async fn transfer_hook_with_transfer_fee(test_validator: &TestValidator, payer: &Keypair) {
let program_id = spl_token_2022::id();
let mut config = test_config_with_default_signer(test_validator, payer, &program_id);
let transfer_hook_program_id = Pubkey::new_unique();

let transfer_fee_basis_points = 100;
let maximum_fee: u64 = 10_000_000_000;

let result = process_test_command(
&config,
payer,
&[
"spl-token",
CommandName::CreateToken.into(),
"--program-id",
&program_id.to_string(),
"--transfer-hook",
&transfer_hook_program_id.to_string(),
"--transfer-fee",
&transfer_fee_basis_points.to_string(),
&maximum_fee.to_string(),
],
)
.await;

// Check that the transfer-hook extension is correctly configured
let value: serde_json::Value = serde_json::from_str(&result.unwrap()).unwrap();
let mint = Pubkey::from_str(value["commandOutput"]["address"].as_str().unwrap()).unwrap();
let account = config.rpc_client.get_account(&mint).await.unwrap();
let mint_state = StateWithExtensionsOwned::<Mint>::unpack(account.data).unwrap();
let extension = mint_state.get_extension::<TransferHook>().unwrap();
assert_eq!(
extension.program_id,
Some(transfer_hook_program_id).try_into().unwrap()
);

// Check that the transfer-fee extension is correctly configured
let extension = mint_state.get_extension::<TransferFeeConfig>().unwrap();
assert_eq!(
u16::from(extension.older_transfer_fee.transfer_fee_basis_points),
transfer_fee_basis_points
);
assert_eq!(
u64::from(extension.older_transfer_fee.maximum_fee),
maximum_fee
);
assert_eq!(
u16::from(extension.newer_transfer_fee.transfer_fee_basis_points),
transfer_fee_basis_points
);
assert_eq!(
u64::from(extension.newer_transfer_fee.maximum_fee),
maximum_fee
);

// Make sure that parsing transfer hook accounts and expected-fee works
let blockhash = Hash::default();
let program_client: Arc<dyn ProgramClient<ProgramRpcClientSendTransaction>> = Arc::new(
ProgramOfflineClient::new(blockhash, ProgramRpcClientSendTransaction),
);
config.program_client = program_client;

let _result = exec_test_cmd(
&config,
&[
"spl-token",
CommandName::Transfer.into(),
&mint.to_string(),
"100",
&Pubkey::new_unique().to_string(),
"--blockhash",
&blockhash.to_string(),
"--nonce",
&Pubkey::new_unique().to_string(),
"--nonce-authority",
&Pubkey::new_unique().to_string(),
"--sign-only",
"--mint-decimals",
&format!("{}", TEST_DECIMALS),
"--from",
&Pubkey::new_unique().to_string(),
"--owner",
&Pubkey::new_unique().to_string(),
"--transfer-hook-account",
&format!("{}:readonly", Pubkey::new_unique()),
"--transfer-hook-account",
&format!("{}:writable", Pubkey::new_unique()),
"--transfer-hook-account",
&format!("{}:readonly-signer", Pubkey::new_unique()),
"--transfer-hook-account",
&format!("{}:writable-signer", Pubkey::new_unique()),
"--expected-fee",
"1",
"--program-id",
&program_id.to_string(),
],
)
.await
.unwrap();
}

async fn metadata(test_validator: &TestValidator, payer: &Keypair) {
let program_id = spl_token_2022::id();
let config = test_config_with_default_signer(test_validator, payer, &program_id);
Expand Down
Loading