Users are forced to use the first pool returned by the Curve Registry
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-02-notional-judging/issues/11
Found by
xiaoming90
Summary
If multiple pools support the exchange, users are forced to use the first pool returned by the Curve Registry. The first pool returned by Curve Registry might not be the most optimal pool to trade with. The first pool might have lesser liquidity, larger slippage, and higher fee than the other pools, resulting in the trade returning lesser assets than expected.
Vulnerability Detail
When performing a trade via the CurveAdapter._exactInSingle function, it will call the CURVE_REGISTRY.find_pool_for_coins function to find the available pools for exchanging two coins.
File: CurveAdapter.sol
34: function _exactInSingle(Trade memory trade)
35: internal view returns (address target, bytes memory executionCallData)
36: {
37: address sellToken = _getTokenAddress(trade.sellToken);
38: address buyToken = _getTokenAddress(trade.buyToken);
39: ICurvePool pool = ICurvePool(Deployments.CURVE_REGISTRY.find_pool_for_coins(sellToken, buyToken));
40:
41: if (address(pool) == address(0)) revert InvalidTrade();
42:
43: int128 i = -1;
44: int128 j = -1;
45: for (int128 c = 0; c < MAX_TOKENS; c++) {
46: address coin = pool.coins(uint256(int256(c)));
47: if (coin == sellToken) i = c;
48: if (coin == buyToken) j = c;
49: if (i > -1 && j > -1) break;
50: }
51:
52: if (i == -1 || j == -1) revert InvalidTrade();
53:
54: return (
55: address(pool),
56: abi.encodeWithSelector(
57: ICurvePool.exchange.selector,
58: i,
59: j,
60: trade.amount,
61: trade.limit
62: )
63: );
64: }
However, it was observed that when multiple pools are available, users can choose the pool to return by defining the i parameter of the find_pool_for_coins function as shown below.
https://etherscan.io/address/0x90E00ACe148ca3b23Ac1bC8C240C2a7Dd9c2d7f5#code
@view
@external
def find_pool_for_coins(_from: address, _to: address, i: uint256 = 0) -> address:
"""
@notice Find an available pool for exchanging two coins
@param _from Address of coin to be sent
@param _to Address of coin to be received
@param i Index value. When multiple pools are available
this value is used to return the n'th address.
@return Pool address
"""
key: uint256 = bitwise_xor(convert(_from, uint256), convert(_to, uint256))
return self.markets[key][i]
However, the CurveAdapter._exactInSingle did not allow users to define the i parameter of the find_pool_for_coins function. As a result, users are forced to trade against the first pool returned by the Curve Registry.
Impact
The first pool returned by Curve Registry might not be the most optimal pool to trade with. The first pool might have lesser liquidity, larger slippage, and higher fee than the other pools, resulting in the trade returning lesser assets than expected.
Code Snippet
Tool used
Manual Review
Recommendation
If multiple pools support the exchange, consider allowing the users to choose which pool they want to trade against.
function _exactInSingle(Trade memory trade)
internal view returns (address target, bytes memory executionCallData)
{
address sellToken = _getTokenAddress(trade.sellToken);
address buyToken = _getTokenAddress(trade.buyToken);
- ICurvePool pool = ICurvePool(Deployments.CURVE_REGISTRY.find_pool_for_coins(sellToken, buyToken));
+ ICurvePool pool = ICurvePool(Deployments.CURVE_REGISTRY.find_pool_for_coins(sellToken, buyToken, trade.pool_index));
Discussion
jeffywu
Valid
hrishibhat
Given that this is only a possibility and all the conditions for loss of funds are not guaranteed, Considering this issue as a valid medium
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Notional Update #4. Sherlock made it public on 30 March 2023. Found in the 52 audit contest (finding M-5).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.