_curveSwap: getDpxEthPrice and getEthPrice is in wrong order
The _curveSwap function uses the wrong price oracle when calculating the minimum output amount for slippage protection. This causes the slippage tolerance to be ineffective, allowing users to receive significantly less than expected during swaps.
Submitted by QiuhaoLi, also found by 0xvj, Toshii, bin2chen, Udsen, sces60107, carrotsmuggler, bart1e, said, T1MOH, 0xDING99YA, SBSecurity, wintermute, and pep7siup
When _curveSwap is called with minAmount = 0 (upperDepeg/lowerDepeg use the default slippage 0.5%), the minOut will be a wrong value which leads to slippage protect failure.
Proof of Concept
In _curveSwap, the getDpxEthPrice and getEthPrice is in wrong order:
uint256 minOut = _ethToDpxEth
? (((_amount * getDpxEthPrice()) / 1e8) -
(((_amount * getDpxEthPrice()) * slippageTolerance) / 1e16))
: (((_amount * getEthPrice()) / 1e8) -
(((_amount * getEthPrice()) * slippageTolerance) / 1e16));
When _ethToDpxEth is true, we swap eth for dpxeth. However, we use getDpxEthPrice, which is dpxeth’s price in eth (dpxeth/eth). Also, when we swap dpxeth for eth, we use getEthPrice which is eth’s price in dpxeth.
Below is a PoC that demonstrates we get the wrong slippage when calling upperDepeg with default slippage tolerance:
// add this to _curveSwap
amountOut = dpxEthCurvePool.exchange(
_ethToDpxEth ? int128(int256(a)) : int128(int256(b)),
_ethToDpxEth ? int128(int256(b)) : int128(int256(a)),
_amount,
minAmount > 0 ? minAmount : minOut
);
if (!_ethToDpxEth) {
console.log("getDpxEthPrice = %s > 1e8, swap %s dpxEth for Eth (0.5% slippage):", getDpxEthPrice(), _amount);
console.log("minOut (wrong slippage) = \t%s", minOut);
uint256 ideal_Out = (_amount * getDpxEthPrice()) / 1e8;
uint256 correct_minOut = (ideal_Out - (((_amount * getDpxEthPrice()) * slippageTolerance) / 1e16));
console.log("minOut (correct slippage) = \t%s", correct_minOut);
console.log("Actual got Eth amountOut = \t%s", amountOut);
console.log("Actual slippage = \t\t%s% > 0.5%", (ideal_Out - amountOut)*1e2/ideal_Out);
}
// tests/rdpxV2-core/Unit.t.sol
function testUpperDepeg_default_slippageTolerance() public {
// swap weth for dpxETH (price after swap 180000000)
dpxETH.approve(address(curvePool), 200 * 1e18);
address coin0 = IStableSwap(curvePool).coins(0);
if (coin0 == address(weth)) {
IStableSwap(curvePool).exchange(0, 1, 200 * 1e18, 0);
} else {
IStableSwap(curvePool).exchange(1, 0, 200 * 1e18, 0);
}
// dpxEth : Eth = 1.8 : 1
dpxEthPriceOracle.updateDpxEthPrice(180000000);
// mint dpxEth and swap for Eth, using default slippage (0.5%)
rdpxV2Core.upperDepeg(10e18, 0);
}
Output:
$ forge test -vv --match-test "testUpperDepeg_default_slippageTolerance"
Running 1 test for tests/rdpxV2-core/Unit.t.sol:Unit
[PASS] testUpperDepeg_default_slippageTolerance() (gas: 246264)
Logs:
getDpxEthPrice = 180000000 > 1e8, swap 10000000000000000000 dpxEth for Eth (0.5% slippage):
minOut (wrong slippage) = 5527777722500000000
minOut (correct slippage) = 17910000000000000000
Actual got Eth amountOut = 15885460869832720611
Actual slippage = 11% > 0.5%
Recommended Mitigation Steps
Reverse the order:
uint256 minOut = _ethToDpxEth
? (((_amount * getEthPrice()) / 1e8) -
(((_amount * getEthPrice) * slippageTolerance) / 1e16))
: (((_amount * getDpxEthPrice()) / 1e8) -
(((_amount * getDpxEthPrice()) * slippageTolerance) / 1e16));
psytama (Dopex) confirmed via duplicate issue 970
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 conditional branches for calculating minOut swapped the price functions: getDpxEthPrice() was used when swapping ETH to dpxETH, and getEthPrice() when swapping dpxETH to ETH.
Impact
Users could suffer higher slippage than intended, potentially losing a significant portion of their funds during swaps.
What the attacker needed
None
The fix
The order of price functions should be reversed in the minOut calculation.
How it was found
- Set up a test where dpxETH is swapped for ETH using upperDepeg with default slippage tolerance.
- Observe that the calculated minOut is much lower than the correct value, leading to excessive slippage.
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.
When you swap tokens, a slippage tolerance protects you from getting much less than you expect. This code used the wrong price to set that protection, so the protection was too loose and users could lose more than intended.
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.