0

This is more of a general rust question but feels like a common enough situation when writing solana contracts such that I feel justified in asking it here.

Suppose I have a token with decimals set to 8 (or anything non-zero, really), and have a fee_basis_points param to work with that is of type u16 with a minimum value of 0 and maximum value of 10000, such that 0 == 0% fees and 10000 == 100% fees. In the contract logic, I want to transfer an arbitrary amount of tokens X to a PDA but want to make sure to apply fee_basis_points to the token amount (which is u64, ofc).

The intuitive thing approach when not worried about unsigned int math would simply be: token_amount + (token_amount * (fee_basis_points/10000)) but using checked_div and checked_mul in the contract and trying the above formula clearly ignores all precision which is not what we want.

let amount: u64 = arbitrary_token_account.amount;
let fee: u16 = some_state.fee_basis_points;

// let amount_to_actually_transfer_in_cpi = ????; 

1 Answer 1

0
let amount: u64 = arbitrary_token_account.amount;
let fee: u16 = some_state.fee_basis_points;

let amount_to_actually_transfer_in_cpi =  amount.checked_mul(fee).unwrap().checked_div(10000).unwrap();

seems to be it. not sure if im not accounting for something here though :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.