Fee Arithmetic in Program Escrow
Overview
The Program Escrow contract uses precise fee arithmetic to manage prize pools and fee collection. Ensuring no value is lost or created out of thin air is critical for security and financial integrity.
Rounding Policy
The contract employs floor (round-down) rounding for all fee calculations. This ensures that the protocol never overcharges the user.
Any remainder from basis-point division (dust) remains with the payer as part of the net amount, adhering to the fundamental invariant:
fee + net_amount == gross_amount
Implementation
Fee calculation is performed in contracts/program-escrow/src/token_math.rs:
pub fn calculate_fee(amount: i128, fee_rate: i128) -> i128 {
amount
.checked_mul(fee_rate)
.and_then(|x| x.checked_div(BASIS_POINTS))
.unwrap_or(0)
}
Security Invariants
- Conservation of Value: The sum of
feeandnet_amountmust always equal thegross_amountinput. - No Over-charge: The fee must never exceed the calculated share based on basis points.
- No Overflow: Calculations must be safe against integer overflow, panicking if they occur.
- Dust Control: For equal splits, any rounding remainder (dust) is accounted for in the
net_amount, ensuring no token loss.