PHOAM-001: Integer overflow will prevent swaps
Closed this issue · 0 comments
Location
./contracts/pool/src/contract.rs:1047
Description
Swapping will be halted in pools with tokens represented with a high number of decimals due to overflow an issue arising from the multiplication of offer_pool and ask_pool in the swap function. This overflow causes the function to panic, halting the swap function. As shown in the snippet below, the result is stored in the cp variable of type i128. This multiplication can exceed the capacity of i128.
// Calculate the cross product of offer_pool and ask_pool
let cp: i128 = offer_pool * ask_pool;
It's important to note that an i128 variable supports approximately 39 decimals. Given that many tokens (like WETH) use 18 decimals, overflow scenarios are quite likely.
Recommendation
Store intermediate results in a u256 variable and safely convert it to i128. Alternatively, explore using an approximation formula that avoids cross product calculations between pool reserves, similar to what is done in UniswapV2.