Skip to main content

checked_mul_div

Function checked_mul_div 

Source
pub fn checked_mul_div(a: u64, b: u64, c: u64) -> Result<u64, ProgramError>
Expand description

Compute (a * b) / c with u128 intermediate to prevent overflow.

The core DeFi math primitive. Without u128 intermediate, a * b overflows for any token amounts > ~4.2B (common with 9-decimal mints). Returns floor division.

// Constant-product swap: dy = (y * dx) / (x + dx)
let output = checked_mul_div(reserve_y, input, reserve_x + input)?;