From 1cffd007ad034c6cb9267ad0020df817438584c6 Mon Sep 17 00:00:00 2001 From: domenicodev Date: Thu, 18 Jan 2024 11:06:13 +0100 Subject: [PATCH] fix: Moved getPriceAt() to bottom --- evm/lib/forge-std | 2 +- evm/src/integral/IntegralSwapAdapter.sol | 60 ++++++++++++------------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/evm/lib/forge-std b/evm/lib/forge-std index 155d547..f73c73d 160000 --- a/evm/lib/forge-std +++ b/evm/lib/forge-std @@ -1 +1 @@ -Subproject commit 155d547c449afa8715f538d69454b83944117811 +Subproject commit f73c73d2018eb6a111f35e4dae7b4f27401e9421 diff --git a/evm/src/integral/IntegralSwapAdapter.sol b/evm/src/integral/IntegralSwapAdapter.sol index 67ab0bb..ea14885 100644 --- a/evm/src/integral/IntegralSwapAdapter.sol +++ b/evm/src/integral/IntegralSwapAdapter.sol @@ -41,36 +41,6 @@ contract IntegralSwapAdapter is ISwapAdapter { } } - /// @notice Get swap price including fee - /// @param sellToken token to sell - /// @param buyToken token to buy - function getPriceAt(address sellToken, address buyToken) internal view returns(Fraction memory) { - uint256 priceWithoutFee = relayer.getPriceByTokenAddresses(address(sellToken), address(buyToken)); - ITwapFactory factory = ITwapFactory(relayer.factory()); - address pairAddress = factory.getPair(address(sellToken), address(buyToken)); - - // get swapFee formatted; swapFee is a constant - uint256 swapFeeFormatted = (STANDARD_TOKEN_DECIMALS - relayer.swapFee(pairAddress)); - - // get token decimals - uint256 sellTokenDecimals = 10**ERC20(sellToken).decimals(); - uint256 buyTokenDecimals = 10**ERC20(buyToken).decimals(); - - /** - * @dev - * Denominator works as a "standardizer" for the price rather than a reserve value - * as Integral takes prices from oracles and do not operate with reserves; - * it is therefore used to maintain integrity for the Fraction division, - * as numerator and denominator could have different token decimals(es. ETH(18)-USDC(6)). - * Both numerator and denominator are also multiplied by STANDARD_TOKEN_DECIMALS - * to ensure that precision losses are minimized or null. - */ - return Fraction( - priceWithoutFee * STANDARD_TOKEN_DECIMALS, - STANDARD_TOKEN_DECIMALS * sellTokenDecimals * swapFeeFormatted / buyTokenDecimals - ); - } - /// @inheritdoc ISwapAdapter function swap( bytes32, @@ -222,6 +192,36 @@ contract IntegralSwapAdapter is ISwapAdapter { return amountIn; } + + /// @notice Get swap price including fee + /// @param sellToken token to sell + /// @param buyToken token to buy + function getPriceAt(address sellToken, address buyToken) internal view returns(Fraction memory) { + uint256 priceWithoutFee = relayer.getPriceByTokenAddresses(address(sellToken), address(buyToken)); + ITwapFactory factory = ITwapFactory(relayer.factory()); + address pairAddress = factory.getPair(address(sellToken), address(buyToken)); + + // get swapFee formatted; swapFee is a constant + uint256 swapFeeFormatted = (STANDARD_TOKEN_DECIMALS - relayer.swapFee(pairAddress)); + + // get token decimals + uint256 sellTokenDecimals = 10**ERC20(sellToken).decimals(); + uint256 buyTokenDecimals = 10**ERC20(buyToken).decimals(); + + /** + * @dev + * Denominator works as a "standardizer" for the price rather than a reserve value + * as Integral takes prices from oracles and do not operate with reserves; + * it is therefore used to maintain integrity for the Fraction division, + * as numerator and denominator could have different token decimals(es. ETH(18)-USDC(6)). + * Both numerator and denominator are also multiplied by STANDARD_TOKEN_DECIMALS + * to ensure that precision losses are minimized or null. + */ + return Fraction( + priceWithoutFee * STANDARD_TOKEN_DECIMALS, + STANDARD_TOKEN_DECIMALS * sellTokenDecimals * swapFeeFormatted / buyTokenDecimals + ); + } } interface ITwapRelayer {