Submitted by Lambda
The calculate_stableswap_y function uses Uint256 for intermediate calculations which can cause arithmetic overflow errors when dealing with large token amounts, especially for tokens with high decimal places (e.g. 18 decimals). This significantly impacts the reliability of the StableSwap pool implementation.
The issue occurs in src/helpers.rs#L106-L152 where calculate_stableswap_y performs multiplication and division operations using Uint256.
The problem manifests when performing swaps with large token amounts. A proof of concept that demonstrates the failure is provided here:
The test fails with CheckedMultiplyRatioError(Overflow) because intermediate calculations in calculate_stableswap_y overflow Uint256 when dealing with:
These are realistic values that could occur in production, making this an issue that could prevent legitimate swaps from executing. Moreover, the following was mentioned in the audit description:
Replace Uint256 with Uint512 for intermediate calculations in calculate_stableswap_y. This provides sufficient precision to handle large token amounts with high decimal places.
The final result can still be converted back to Uint128 since the actual swap amounts will fit within that range. This change ensures the contract can handle realistic token amounts while maintaining precision in the StableSwap calculations.
A test suite should be added that verifies the contract handles large token amounts correctly across different pool configurations and swap scenarios.
jvr0x (MANTRA) confirmed
