LamboRebalanceOnUniswap::_getTokenInOut formula used to compute rebalancing amount is wrong for a UniV3 pool
The rebalancing formula in LamboRebalanceOnUniswap incorrectly assumes a constant-sum AMM and uses total pool balances, which include liquidity in inactive ticks. This makes the computed rebalancing amount imprecise, potentially failing to maintain the peg.
Submitted by Infect3d, also found by EPSec, ka14ar, King_9aimon, KupiaSec, and pontifex
The formula implemented assumes that the pool is based on a constant sum AMM formula (x+y = k), and also eludes the fact that reserves in a UniV3 pool do not directly relate to the price because of the 1-sided ticks liquidity.
This make the function imprecise at best, and highly imprecise when liquidity is deposited in distant ticks, with no risk involved for actors depositing in those ticks.
Vulnerability details
The previewRebalance function has been developed to output all the necessary input parameters required to call the rebalance function, which goal is to swap tokens in order to keep the peg of the virtual token in comparison to its counterpart (e.g keep vETH/ETH prices = 1):
File: src/rebalance/LamboRebalanceOnUniwap.sol
128: function _getTokenBalances() internal view returns (uint256 wethBalance, uint256 vethBalance) {
129: wethBalance = IERC20(weth).balanceOf(uniswapPool); <<❌(1) this does not represent the active tick
130: vethBalance = IERC20(veth).balanceOf(uniswapPool);
131: }
132:
133: function _getTokenInOut() internal view returns (address tokenIn, address tokenOut, uint256 amountIn) {
134: (uint256 wethBalance, uint256 vethBalance) = _getTokenBalances();
135: uint256 targetBalance = (wethBalance + vethBalance) / 2; <<❌(2) wrong formula
136:
137: if (vethBalance > targetBalance) {
138: amountIn = vethBalance - targetBalance;
139: tokenIn = weth;
140: tokenOut = veth;
141: } else {
142: amountIn = wethBalance - targetBalance;
143: tokenIn = veth;
144: tokenOut = weth;
145: }
146:
147: require(amountIn > 0, "amountIn must be greater than zero");
148: }
149:
The implemented formula is incorrect, as it will not rebalance the pool for 2 reasons:
- In Uniswap V3, LPs can deposit tokens in any ticks they want, even though those ticks are not active and do not participate to the actual price. But those tokens will be held by the pool, and thus be measured by
_getTokenBalances - The formula used to compute the targetBalance is incorrect because of how works the constant product formula
x*y=k
Regarding (2), consider this situation: WETH balance: 1000 vETH balance: 900 targetBalance = (1000 + 900) / 2 = 950 amountIn = 1000 - 950 = 50 (vETH)
Swapping 50 vETH into the pool will not return 50 WETH because of the inherent slippage of the constant product formula.
Now, add to this bullet (1), and the measured balance will be wrong anyway because of the liquidity deposited in inactive ticks, making the result even more shifted from the optimal result.
Impact
The function is not performing as intended, leading to invalid results which complicates the computation of rebalancing amounts necessary to maintain the peg.
Since this function is important to maintain the health of vETH as it has access to on-chain values, allowing precise rebalancing, failing to devise and implement a reliable solution for rebalancing before launch could result in significant issues.
Recommended Mitigation Steps
Reconsider the computations of rebalancing amounts for a more precise one if keeping a 1:1 peg is important.
You might want to get inspiration from USSDRebalancer::rebalance().
Shaneson (Lambo.win) acknowledged
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
Impact
The function does not perform as intended, producing invalid rebalancing amounts that could fail to maintain the peg, though no direct loss is described.
What the attacker needed
None
The fix
The rebalancing amount computation should be reconsidered for more precision, possibly using a reference implementation like USSDRebalancer::rebalance().
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 tries to keep the price of a token stable by swapping tokens in a Uniswap V3 pool. The code calculates how many tokens to swap using a simple average, but that method is wrong because Uniswap V3 pools hold tokens in many price ranges, not just the current one. As a result, the swap amount is inaccurate and may not keep the price stable.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.