diff --git a/src/connectors/uniswap/uniswap.ts b/src/connectors/uniswap/uniswap.ts index 9c4fbe456a..c65c2acdd1 100644 --- a/src/connectors/uniswap/uniswap.ts +++ b/src/connectors/uniswap/uniswap.ts @@ -211,7 +211,8 @@ export class Uniswap implements Uniswapish { baseToken: Token, quoteToken: Token, amount: BigNumber, - allowedSlippage?: string + allowedSlippage?: string, + poolId?: string ): Promise { const nativeTokenAmount: CurrencyAmount = CurrencyAmount.fromRawAmount(baseToken, amount.toString()); @@ -246,7 +247,7 @@ export class Uniswap implements Uniswapish { ); return { trade: route.trade, expectedAmount }; } else { - const pool = await this.getPool(baseToken, quoteToken, this._feeTier); + const pool = await this.getPool(baseToken, quoteToken, this._feeTier, poolId); if (!pool) { throw new UniswapishPriceError( `priceSwapIn: no trade pair found for ${baseToken.address} to ${quoteToken.address}.` @@ -291,7 +292,8 @@ export class Uniswap implements Uniswapish { quoteToken: Token, baseToken: Token, amount: BigNumber, - allowedSlippage?: string + allowedSlippage?: string, + poolId?: string ): Promise { const nativeTokenAmount: CurrencyAmount = CurrencyAmount.fromRawAmount(baseToken, amount.toString()); @@ -325,7 +327,7 @@ export class Uniswap implements Uniswapish { ); return { trade: route.trade, expectedAmount }; } else { - const pool = await this.getPool(quoteToken, baseToken, this._feeTier); + const pool = await this.getPool(quoteToken, baseToken, this._feeTier, poolId); if (!pool) { throw new UniswapishPriceError( `priceSwapOut: no trade pair found for ${quoteToken.address} to ${baseToken.address}.` @@ -426,7 +428,8 @@ export class Uniswap implements Uniswapish { private async getPool( tokenA: Token, tokenB: Token, - feeTier: FeeAmount + feeTier: FeeAmount, + poolId?: string ): Promise { const uniswapFactory = new Contract( this._factory, @@ -434,12 +437,12 @@ export class Uniswap implements Uniswapish { this.chain.provider ); // Use Uniswap V3 factory to get pool address instead of `Pool.getAddress` to check if pool exists. - const poolAddress = await uniswapFactory.getPool( + const poolAddress = poolId || await uniswapFactory.getPool( tokenA.address, tokenB.address, feeTier ); - if (poolAddress === constants.AddressZero) { + if (poolAddress === constants.AddressZero || poolAddress === undefined || poolAddress === '') { return null; } const poolContract = new Contract( @@ -448,16 +451,17 @@ export class Uniswap implements Uniswapish { this.chain.provider ); - const [liquidity, slot0] = await Promise.all([ + const [liquidity, slot0, fee] = await Promise.all([ poolContract.liquidity(), poolContract.slot0(), + poolContract.fee(), ]); const [sqrtPriceX96, tick] = slot0; const pool = new Pool( tokenA, tokenB, - this._feeTier, + fee, sqrtPriceX96, liquidity, tick