IndexPool : Poor conversion from Balancer V1’s corresponding functions
Several mathematical functions in the IndexPool contract were incorrectly converted from Balancer V1, leading to wrong calculations. This could cause incorrect pool token pricing and potentially allow attackers to exploit the pool for profit.
Submitted by GreyArt
Impact
A number of functions suffer from the erroneous conversion of Balancer V1’s implementation.
-
_compute()(equivalent to Balancer’sbpow())if (remain == 0) output = wholePow;when a return statement should be used instead.
-
_computeSingleOutGivenPoolIn()(equivalent to Balancer’s_calcSingleOutGivenPoolIn())tokenOutRatioshould be calculated with_compute()instead of_pow()zazshould be calculated with_mul()instead of the native*
-
_pow()(equivalent to Balancer’sbpowi())- Missing brackets
{}for the for loop causes a different interpretation _mulshould be used instead of the native*
- Missing brackets
Recommended Mitigation Steps
The fixed implementation is provided below.
function _computeSingleOutGivenPoolIn(
uint256 tokenOutBalance,
uint256 tokenOutWeight,
uint256 _totalSupply,
uint256 _totalWeight,
uint256 toBurn,
uint256 _swapFee
) internal pure returns (uint256 amountOut) {
uint256 normalizedWeight = _div(tokenOutWeight, _totalWeight);
uint256 newPoolSupply = _totalSupply - toBurn;
uint256 poolRatio = _div(newPoolSupply, _totalSupply);
uint256 tokenOutRatio = _compute(poolRatio, _div(BASE, normalizedWeight));
uint256 newBalanceOut = _mul(tokenOutRatio, tokenOutBalance);
uint256 tokenAmountOutBeforeSwapFee = tokenOutBalance - newBalanceOut;
uint256 zaz = _mul(BASE - normalizedWeight, _swapFee);
amountOut = _mul(tokenAmountOutBeforeSwapFee, (BASE - zaz));
}
function _compute(uint256 base, uint256 exp) internal pure returns (uint256 output) {
require(MIN_POW_BASE <= base && base <= MAX_POW_BASE, "INVALID_BASE");
uint256 whole = (exp / BASE) * BASE;
uint256 remain = exp - whole;
uint256 wholePow = _pow(base, whole / BASE);
if (remain == 0) return wholePow;
uint256 partialResult = _powApprox(base, remain, POW_PRECISION);
output = _mul(wholePow, partialResult);
}
function _pow(uint256 a, uint256 n) internal pure returns (uint256 output) {
output = n % 2 != 0 ? a : BASE;
for (n /= 2; n != 0; n /= 2) {
a = _mul(a, a);
if (n % 2 != 0) output = _mul(output, a);
}
}
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely, the conversion from Balancer V1 introduced errors such as using native multiplication instead of safe math functions, missing brackets in a loop, and incorrect return statements, leading to incorrect mathematical results.
What the attacker needed
None
The fix
The report provides corrected implementations of the affected functions, and the project acknowledged the issue.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
This report is about a smart contract that handles a pool of tokens. The code that calculates how many tokens you get when you swap or add/remove liquidity had mistakes because it was copied from an older version but not correctly adapted. These mistakes could make the pool give out wrong amounts, which someone might exploit to take more tokens than they should.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.