Skip to content

Commit

Permalink
create transfer-fee-basis-points and transfer-fee-maximum-fee flags
Browse files Browse the repository at this point in the history
Signed-off-by: Hrushi20 <[email protected]>
  • Loading branch information
Hrushi20 committed Jul 26, 2024
1 parent d1fa4d5 commit d235652
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
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)
.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"])
.takes_value(true)
.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"])
.takes_value(true)
.number_of_values(1)
.help(
"Add a UI amount transfer fee to the mint. \
The mint authority can set the fee and collected fee"
)
)
.arg(
Arg::with_name("enable_permanent_delegate")
.long("enable-permanent-delegate")
Expand Down
43 changes: 41 additions & 2 deletions token/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ use {
encryption::{
auth_encryption::AeKey,
elgamal::{self, ElGamalKeypair},
},
zk_token_elgamal::pod::ElGamalPubkey,
}, instruction::transfer, zk_token_elgamal::pod::ElGamalPubkey
},
state::{Account, AccountState, Mint},
},
Expand Down Expand Up @@ -3472,6 +3471,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());
(
v.next()
.unwrap()
Expand All @@ -3484,6 +3484,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)
});

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)
});

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)
}
}
};

let transfer_fee = match transfer_fee {
Ok(result) => result,
Err(e) => {
eprintln!("{}",e);
exit(1);
}
};

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),
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

0 comments on commit d235652

Please sign in to comment.