From 39994ab2c49ba4e1d0adbde40dd7c4ac28cc2a2d Mon Sep 17 00:00:00 2001 From: Siyu Jiang <91580504+jsy1218@users.noreply.github.com> Date: Tue, 12 Sep 2023 00:04:13 -0700 Subject: [PATCH] fix cached routes side of bugs that caused fot quote to not work properly --- package-lock.json | 8 +++---- src/providers/v2/quote-provider.ts | 24 ++++++++++++++++--- src/routers/alpha-router/alpha-router.ts | 2 +- .../alpha-router/gas-models/gas-model.ts | 1 + src/routers/alpha-router/quoters/v2-quoter.ts | 1 + .../routers/alpha-router/alpha-router.test.ts | 7 ++++++ 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index a249f0448..e469e2c57 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3119,7 +3119,7 @@ "@ethersproject/abi": "^5.5.0", "@uniswap/sdk-core": "^4", "@uniswap/swap-router-contracts": "1.1.0", - "@uniswap/v2-sdk": "^3.2.0", + "@uniswap/v2-sdk": "^3.2.1", "@uniswap/v3-sdk": "^3.10.0" } }, @@ -3253,7 +3253,7 @@ "@uniswap/router-sdk": "^1.6.0", "@uniswap/sdk-core": "^4.0.0", "@uniswap/universal-router": "1.4.3", - "@uniswap/v2-sdk": "^3.2.0", + "@uniswap/v2-sdk": "^3.2.1", "@uniswap/v3-sdk": "^3.10.0", "bignumber.js": "^9.0.2", "ethers": "^5.3.1" @@ -14114,7 +14114,7 @@ "@ethersproject/abi": "^5.5.0", "@uniswap/sdk-core": "^4", "@uniswap/swap-router-contracts": "1.1.0", - "@uniswap/v2-sdk": "^3.2.0", + "@uniswap/v2-sdk": "^3.2.1", "@uniswap/v3-sdk": "^3.10.0" }, "dependencies": { @@ -14234,7 +14234,7 @@ "@uniswap/router-sdk": "^1.6.0", "@uniswap/sdk-core": "^4.0.0", "@uniswap/universal-router": "1.4.3", - "@uniswap/v2-sdk": "^3.2.0", + "@uniswap/v2-sdk": "^3.2.1", "@uniswap/v3-sdk": "^3.10.0", "bignumber.js": "^9.0.2", "ethers": "^5.3.1" diff --git a/src/providers/v2/quote-provider.ts b/src/providers/v2/quote-provider.ts index bf2f2bdc4..b22ba9d51 100644 --- a/src/providers/v2/quote-provider.ts +++ b/src/providers/v2/quote-provider.ts @@ -75,8 +75,18 @@ export class V2QuoteProvider implements IV2QuoteProvider { let outputAmount = amount.wrapped; for (const pair of route.pairs) { - const [outputAmountNew] = pair.getOutputAmount(outputAmount); - outputAmount = outputAmountNew; + if (pair.token0.equals(outputAmount.currency) && pair.token0.sellFeeBps?.gt(BigNumber.from(0))) { + const outputAmountWithSellFeeBps = CurrencyAmount.fromRawAmount(pair.token0, outputAmount.quotient); + const [outputAmountNew] = pair.getOutputAmount(outputAmountWithSellFeeBps); + outputAmount = outputAmountNew; + } else if (pair.token1.equals(outputAmount.currency) && pair.token1.sellFeeBps?.gt(BigNumber.from(0))) { + const outputAmountWithSellFeeBps = CurrencyAmount.fromRawAmount(pair.token1, outputAmount.quotient); + const [outputAmountNew] = pair.getOutputAmount(outputAmountWithSellFeeBps); + outputAmount = outputAmountNew; + } else { + const [outputAmountNew] = pair.getOutputAmount(outputAmount); + outputAmount = outputAmountNew; + } } amountQuotes.push({ @@ -88,7 +98,15 @@ export class V2QuoteProvider implements IV2QuoteProvider { for (let i = route.pairs.length - 1; i >= 0; i--) { const pair = route.pairs[i]!; - [inputAmount] = pair.getInputAmount(inputAmount); + if (pair.token0.equals(inputAmount.currency) && pair.token0.buyFeeBps?.gt(BigNumber.from(0))) { + const inputAmountWithBuyFeeBps = CurrencyAmount.fromRawAmount(pair.token0, inputAmount.quotient); + [inputAmount] = pair.getInputAmount(inputAmountWithBuyFeeBps); + } else if (pair.token1.equals(inputAmount.currency) && pair.token1.buyFeeBps?.gt(BigNumber.from(0))) { + const inputAmountWithSellFeeBps = CurrencyAmount.fromRawAmount(pair.token1, inputAmount.quotient); + [inputAmount] = pair.getInputAmount(inputAmountWithSellFeeBps); + } else { + [inputAmount] = pair.getInputAmount(inputAmount); + } } amountQuotes.push({ diff --git a/src/routers/alpha-router/alpha-router.ts b/src/routers/alpha-router/alpha-router.ts index 48520693b..54c624d7b 100644 --- a/src/routers/alpha-router/alpha-router.ts +++ b/src/routers/alpha-router/alpha-router.ts @@ -1182,7 +1182,7 @@ export class AlphaRouter cacheMode !== CacheMode.Darkmode && swapRouteFromChain ) { - const tokenPropertiesMap = await this.tokenPropertiesProvider.getTokensProperties([tokenIn, tokenOut], routingConfig); + const tokenPropertiesMap = await this.tokenPropertiesProvider.getTokensProperties([tokenIn, tokenOut], providerConfig); const tokenInWithFotTax = (tokenPropertiesMap[tokenIn.address.toLowerCase()] diff --git a/src/routers/alpha-router/gas-models/gas-model.ts b/src/routers/alpha-router/gas-models/gas-model.ts index aabfb68b3..ba3c71b50 100644 --- a/src/routers/alpha-router/gas-models/gas-model.ts +++ b/src/routers/alpha-router/gas-models/gas-model.ts @@ -175,6 +175,7 @@ export abstract class IOnChainGasModelFactory { quoteToken, v2poolProvider: V2poolProvider, l2GasDataProvider, + providerConfig, }: BuildOnChainGasModelFactoryType): Promise< IGasModel >; diff --git a/src/routers/alpha-router/quoters/v2-quoter.ts b/src/routers/alpha-router/quoters/v2-quoter.ts index 71c64778a..63695eb92 100644 --- a/src/routers/alpha-router/quoters/v2-quoter.ts +++ b/src/routers/alpha-router/quoters/v2-quoter.ts @@ -153,6 +153,7 @@ export class V2Quoter extends BaseQuoter { gasPriceWei, poolProvider: this.v2PoolProvider, token: quoteToken, + providerConfig: _routingConfig, // TODO: implement wrap overhead for v2 routes }); diff --git a/test/unit/routers/alpha-router/alpha-router.test.ts b/test/unit/routers/alpha-router/alpha-router.test.ts index a6eef7be8..0790e0e57 100644 --- a/test/unit/routers/alpha-router/alpha-router.test.ts +++ b/test/unit/routers/alpha-router/alpha-router.test.ts @@ -515,6 +515,7 @@ describe('alpha router', () => { gasPriceWei: mockGasPriceWeiBN, poolProvider: sinon.match.any, token: WRAPPED_NATIVE_CURRENCY[1], + providerConfig: sinon.match.any, }) ).toBeTruthy(); expect( @@ -918,6 +919,7 @@ describe('alpha router', () => { gasPriceWei: mockGasPriceWeiBN, poolProvider: sinon.match.any, token: WRAPPED_NATIVE_CURRENCY[1], + providerConfig: sinon.match.any, }) ).toBeTruthy(); @@ -1103,6 +1105,7 @@ describe('alpha router', () => { gasPriceWei: mockGasPriceWeiBN, poolProvider: sinon.match.any, token: WRAPPED_NATIVE_CURRENCY[1], + providerConfig: sinon.match.any, }) ).toBeTruthy(); @@ -1512,6 +1515,7 @@ describe('alpha router', () => { gasPriceWei: mockGasPriceWeiBN, poolProvider: sinon.match.any, token: WRAPPED_NATIVE_CURRENCY[1], + providerConfig: sinon.match.any, }) ).toBeTruthy(); @@ -1884,6 +1888,7 @@ describe('alpha router', () => { gasPriceWei: mockGasPriceWeiBN, poolProvider: sinon.match.any, token: USDC, + providerConfig: sinon.match.any, }) ).toBeTruthy(); expect( @@ -2046,6 +2051,7 @@ describe('alpha router', () => { gasPriceWei: mockGasPriceWeiBN, poolProvider: sinon.match.any, token: USDC, + providerConfig: sinon.match.any, }) ).toBeTruthy(); expect( @@ -2125,6 +2131,7 @@ describe('alpha router', () => { gasPriceWei: mockGasPriceWeiBN, poolProvider: sinon.match.any, token: USDC, + providerConfig: sinon.match.any, }) ).toBeTruthy(); expect(