From 7ff3e1c4da1ca111e1761e901b531a3b8fc2acba Mon Sep 17 00:00:00 2001 From: "Siyu Jiang (See-You John)" <91580504+jsy1218@users.noreply.github.com> Date: Fri, 31 Jan 2025 06:18:37 -0800 Subject: [PATCH] fix(v4-sdk): loosen v4 last pool check invariant (#288) ## PR Scope Please title your PR according to the following types and scopes following [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/): - `fix(SDK name):` will trigger a patch version - `chore():` will not trigger any release and should be used for internal repo changes - `(public):` will trigger a patch version for non-code changes (e.g. README changes) - `feat(SDK name):` will trigger a minor version - `feat(breaking):` will trigger a major version for a breaking change ## Description _[Summary of the change, motivation, and context]_ ## How Has This Been Tested? _[e.g. Manually, E2E tests, unit tests, Storybook]_ ## Are there any breaking changes? _[e.g. Type definitions, API definitions]_ If there are breaking changes, please ensure you bump the major version Bump the major version (by using the title `feat(breaking): ...`), post a notice in #eng-sdks, and explicitly notify all Uniswap Labs consumers of the SDK. ## (Optional) Feedback Focus _[Specific parts of this PR you'd like feedback on, or that reviewers should pay closer attention to]_ ## (Optional) Follow Ups _[Things that weren't addressed in this PR, ways you plan to build on this work, or other ways this work could be extended]_ --- sdks/v4-sdk/src/entities/pool.test.ts | 34 ++++++++++++++++++++++++++- sdks/v4-sdk/src/entities/pool.ts | 14 +++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/sdks/v4-sdk/src/entities/pool.test.ts b/sdks/v4-sdk/src/entities/pool.test.ts index 24fc13e23..5ad1a600a 100644 --- a/sdks/v4-sdk/src/entities/pool.test.ts +++ b/sdks/v4-sdk/src/entities/pool.test.ts @@ -1,4 +1,4 @@ -import { Token, CurrencyAmount, WETH9 } from '@uniswap/sdk-core' +import { Token, CurrencyAmount, WETH9, Ether, ChainId } from '@uniswap/sdk-core' import { Pool, DYNAMIC_FEE_FLAG } from './pool' import JSBI from 'jsbi' import { nearestUsableTick, encodeSqrtRatioX96, TickMath } from '@uniswap/v3-sdk' @@ -261,6 +261,38 @@ describe('Pool', () => { expect(pool.involvesCurrency(WETH9[1])).toEqual(false) }) + describe('#v4InvolvesToken', () => { + const pool = new Pool( + Ether.onChain(ChainId.MAINNET), + DAI, + FEE_AMOUNT_LOW, + TICK_SPACING_TEN, + ADDRESS_ZERO, + encodeSqrtRatioX96(1, 1), + 0, + 0, + [] + ) + expect(pool.v4InvolvesToken(Ether.onChain(ChainId.MAINNET))).toEqual(true) + expect(pool.v4InvolvesToken(DAI)).toEqual(true) + expect(pool.v4InvolvesToken(WETH9[1])).toEqual(true) + + const pool2 = new Pool( + Ether.onChain(ChainId.MAINNET).wrapped, + DAI, + FEE_AMOUNT_LOW, + TICK_SPACING_TEN, + ADDRESS_ZERO, + encodeSqrtRatioX96(1, 1), + 0, + 0, + [] + ) + expect(pool2.v4InvolvesToken(Ether.onChain(ChainId.MAINNET))).toEqual(true) + expect(pool2.v4InvolvesToken(DAI)).toEqual(true) + expect(pool2.v4InvolvesToken(WETH9[1])).toEqual(true) + }) + describe('swaps', () => { let pool: Pool let poolWithSwapHook: Pool diff --git a/sdks/v4-sdk/src/entities/pool.ts b/sdks/v4-sdk/src/entities/pool.ts index c66454218..9ddc01361 100644 --- a/sdks/v4-sdk/src/entities/pool.ts +++ b/sdks/v4-sdk/src/entities/pool.ts @@ -160,6 +160,20 @@ export class Pool { return this.involvesCurrency(currency) } + /** + * v4-only involvesToken convenience method, used for mixed route ETH <-> WETH connection only + * @param currency + */ + public v4InvolvesToken(currency: Currency): boolean { + return ( + this.involvesCurrency(currency) || + currency.wrapped.equals(this.currency0) || + currency.wrapped.equals(this.currency1) || + currency.wrapped.equals(this.currency0.wrapped) || + currency.wrapped.equals(this.currency1.wrapped) + ) + } + /** * Returns the current mid price of the pool in terms of currency0, i.e. the ratio of currency1 over currency0 */