When broadcastring a token-2022 program transaction using the transferCheckedWithFee instruction, solana documentation suggest to compute token fee using this formula:
const fee = (amountToSend * feeBasisPoints) / 10_000
const feeCharged = fee > maximumFee ? maximumFee : fee
But you can end up getting an error like:
Program log: TransferFeeInstruction: TransferCheckedWithFee","Program log: Calculated fee 18799792, received 18799791","Program log: Calculated fee does not match expected fee","Program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb consumed 7544 of 200000 compute units
Likely a rounding issue. Is it fine to Math.ceil the fee amount?
You've got it right, you need to
ceilthe division.The fee calculation in the program will ceiling the division for you at https://github.com/solana-labs/solana-program-library/blob/b3f68a8c3c362ed2ce3677c565bcf9ed9f136310/token/program-2022/src/extension/transfer_fee/mod.rs#L45-L71.
Posting the code here for posterity: