H-1: CurvePoolConnector.getPoolPrice() provides incorrect input amount for pools containing tokens with non-standard decimals
Summary:
CurvePoolConnector.getPoolPrice() calls into a given Curve pool to fetch the pool's current pricing of two assets in the pool. It achieves this by fetching a quote for the output amount of the swap's output token when providing 1e18 of the swap's input token.
Description:
The highlighted method pulls the price between the two assets by invoking the Curve pool's get_dy method, which expects to receive the amount of input token provided in a swap and returns the swap's output amount.
Hard-coding the input amount as 1e18 stands to create a significant error in the received quote in the case in which the input token does not have 18 decimals of fixed precision: this is the case for USDC and USDT on Ethereum which have six decimals of fixed precision or GUSD on Ethereum which only has two.
The curve pool will interpret 1e18 as a 10 ** (18 - token.decimals()) input amount, which will generate an error in the reported price's value and magnitude.
The method does this when requesting both the direct and inverse price for two assets:
Impact:
High. Querying a Curve pool's reported price can malfunction when dealing with tokens that have non-standard decimals, such as the most popular stablecoins.
Recommendation:
Refactor the highlighted method to read the number of decimals used by the input token and use such value to determine the quote's input amount:
@@ -14,6 +14,10 @@ struct CurveSwapExtraData {
int128 j;
}
+interface IERC20Decimals {
+ function decimals() external view returns(uint256);
+}
+
@@ -69,18 +73,24 @@ contract CurvePoolConnector is ILiquidityConnector {
uint256 baseTokenIndex,
uint256 quoteTokenIndex
) external view override returns (uint256 price) {
+ address inputToken = ICurvePool(lpToken).coins(baseTokenIndex);
+ uint256 inputTokenDecimals = IERC20Decimals(inputToken).decimals();
+
uint256 amountOut0 = ICurvePool(lpToken).get_dy(
int128(uint128(baseTokenIndex)),
int128(uint128(quoteTokenIndex)),
- 1e18
+ 10 ** inputTokenDecimals
);
if (amountOut0 > 0) {
price = amountOut0;
} else {
+ inputToken = ICurvePool(lpToken).coins(quoteTokenIndex);
+ inputTokenDecimals = IERC20Decimals(inputToken).decimals();
+
uint256 amountOut1 = ICurvePool(lpToken).get_dy(
int128(uint128(quoteTokenIndex)),
int128(uint128(baseTokenIndex)),
- 1e18
+ 10 ** inputTokenDecimals
);
if (amountOut1 == 0) {
revert InvalidPrice();
Developer Response:
Fixed in commit 296d744.