Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token-cli: create transfer-fee-basis-points and transfer-fee-maximum-fee flags #7053

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
23 changes: 23 additions & 0 deletions token/cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,11 +763,34 @@ pub fn app<'a, 'b>(
.value_names(&["FEE_IN_BASIS_POINTS", "MAXIMUM_FEE"])
.takes_value(true)
.number_of_values(2)
.hidden(true)
joncinque marked this conversation as resolved.
Show resolved Hide resolved
.help(
"Add a transfer fee to the mint. \
The mint authority can set the fee and withdraw collected fees.",
),
)
.arg(
Arg::with_name("transfer_fee_basis_points")
.long("transfer-fee-basis-points")
.value_names(&["FEE_IN_BASIS_POINTS"])
joncinque marked this conversation as resolved.
Show resolved Hide resolved
.takes_value(true)
joncinque marked this conversation as resolved.
Show resolved Hide resolved
.number_of_values(1)
.help(
"Add transfer fee to the mint. \
The mint authority can set the fee.",
),
)
.arg(
Arg::with_name("transfer_fee_maximum_fee")
.long("transfer-fee-maximum-fee")
.value_names(&["MAXIMUM_FEE"])
joncinque marked this conversation as resolved.
Show resolved Hide resolved
.takes_value(true)
joncinque marked this conversation as resolved.
Show resolved Hide resolved
.number_of_values(1)
.help(
"Add a UI amount transfer fee to the mint. \
joncinque marked this conversation as resolved.
Show resolved Hide resolved
The mint authority can set the fee and collected fee"
joncinque marked this conversation as resolved.
Show resolved Hide resolved
)
)
.arg(
Arg::with_name("enable_permanent_delegate")
.long("enable-permanent-delegate")
Expand Down
40 changes: 40 additions & 0 deletions token/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3472,6 +3472,7 @@ pub async fn process_command<'a>(
let member_address = value_t!(arg_matches, "member_address", Pubkey).ok();

let transfer_fee = arg_matches.values_of("transfer_fee").map(|mut v| {
println_display(config,"transfer_fee flag has been deprecated".to_string());
joncinque marked this conversation as resolved.
Show resolved Hide resolved
(
v.next()
.unwrap()
Expand All @@ -3484,6 +3485,45 @@ pub async fn process_command<'a>(
)
});

let transfer_fee_basis_point = arg_matches.values_of("transfer_fee_basis_points").map(|mut v| {
v.next()
.unwrap()
.parse::<u16>()
.unwrap_or_else(print_error_and_exit)
});
joncinque marked this conversation as resolved.
Show resolved Hide resolved

let transfer_fee_maximum_fee = arg_matches.values_of("transfer_fee_maximum_fee").map(|mut v| {
v.next()
.unwrap()
.parse::<f64>()
.unwrap_or_else(print_error_and_exit)
});
joncinque marked this conversation as resolved.
Show resolved Hide resolved

let transfer_fee = match transfer_fee.is_some() {
true => match transfer_fee_basis_point.is_some() || transfer_fee_maximum_fee.is_some() {
true => Err("transfer-fee-basis-point and transfer-fee-maximum-fee flag disable when using transfer-fee flag"),
false => Ok(transfer_fee)
},
false => match transfer_fee_basis_point.is_some() && transfer_fee_maximum_fee.is_some() {
true => Ok(Some((transfer_fee_basis_point.unwrap(), (transfer_fee_maximum_fee.unwrap() * (u64::pow(10, decimals.into())) as f64) as u64))),
false => if transfer_fee_basis_point.is_some() {
Err("Missing transfer_fee_maximum_fee flag")
}else if transfer_fee_maximum_fee.is_some() {
Err("Missing transfer_fee_basis_point")
}else{
Ok(None)
}
}
};
joncinque marked this conversation as resolved.
Show resolved Hide resolved

let transfer_fee = match transfer_fee {
Ok(result) => result,
Err(e) => {
eprintln!("{}",e);
exit(1);
}
};
joncinque marked this conversation as resolved.
Show resolved Hide resolved

let (token_signer, token) =
get_signer(arg_matches, "token_keypair", &mut wallet_manager)
.unwrap_or_else(new_throwaway_signer);
Expand Down
49 changes: 49 additions & 0 deletions token/cli/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ async fn main() {
async_trial!(non_transferable, test_validator, payer),
async_trial!(default_account_state, test_validator, payer),
async_trial!(transfer_fee, test_validator, payer),
async_trial!(transfer_fee_basis_point, test_validator, payer),
// async_trial!(transfer_fee_maximum_fee, test_validator, payer),
joncinque marked this conversation as resolved.
Show resolved Hide resolved
async_trial!(confidential_transfer, test_validator, payer),
async_trial!(multisig_transfer, test_validator, payer),
async_trial!(offline_multisig_transfer_with_nonce, test_validator, payer),
Expand Down Expand Up @@ -2521,6 +2523,53 @@ async fn transfer_fee(test_validator: &TestValidator, payer: &Keypair) {
);
}

async fn transfer_fee_basis_point(test_validator: &TestValidator, payer: &Keypair) {
let config = test_config_with_default_signer(test_validator, payer, &spl_token_2022::id());

let transfer_fee_basis_points = 100;
let maximum_fee = 1.2;
let decimal = 9;

let token = Keypair::new();
let token_keypair_file = NamedTempFile::new().unwrap();
write_keypair_file(&token, &token_keypair_file);
let token_pubkey = token.pubkey();
process_test_command(
&config,
payer,
&[
"spl-token",
CommandName::CreateToken.into(),
token_keypair_file.path().to_str().unwrap(),
"--transfer-fee-basis-points",
&transfer_fee_basis_points.to_string(),
"--transfer-fee-maximum-fee",
&maximum_fee.to_string()
]
).await
.unwrap();

let account = config.rpc_client.get_account(&token_pubkey).await.unwrap();
let test_mint = StateWithExtensionsOwned::<Mint>::unpack(account.data).unwrap();
let extension = test_mint.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 * i32::pow(10,decimal) as f64) as u64
);
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 * i32::pow(10, decimal) as f64) as u64
);
}

async fn confidential_transfer(test_validator: &TestValidator, payer: &Keypair) {
use spl_token_2022::solana_zk_token_sdk::encryption::elgamal::ElGamalKeypair;

Expand Down