Skip to content

Commit

Permalink
fix(router-sdk): isValidTokenPath should not cast currency to token f…
Browse files Browse the repository at this point in the history
…or v4 pool (#283)
  • Loading branch information
jsy1218 authored Jan 31, 2025
1 parent ccc0587 commit d0ffdb8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions sdks/router-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './utils/encodeMixedRouteToPath'
export * from './utils/TPool'
export * from './utils/pathCurrency'
export * from './utils'
export * from './utils/isValidTokenPath'
19 changes: 19 additions & 0 deletions sdks/router-sdk/src/utils/isValidTokenPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Ether, Token, WETH9 } from '@uniswap/sdk-core'
import { FeeAmount, encodeSqrtRatioX96 } from '@uniswap/v3-sdk'
import { Pool as V4Pool } from '@uniswap/v4-sdk'
import { ADDRESS_ZERO } from '../constants'
import { isValidTokenPath } from './isValidTokenPath'

describe('#isValidTokenPath', () => {
const SQRT_RATIO_ONE = encodeSqrtRatioX96(1, 1)
const ETHER = Ether.onChain(1)
const token1 = new Token(1, '0x0000000000000000000000000000000000000002', 18, 't1')
const weth = WETH9[1]

const pool_v4_weth_eth = new V4Pool(weth, ETHER, 0, 0, ADDRESS_ZERO, 79228162514264337593543950336, 0, 0)
const pool_v4_1_eth = new V4Pool(token1, ETHER, FeeAmount.MEDIUM, 60, ADDRESS_ZERO, SQRT_RATIO_ONE, 0, 0)

it('v3 pool and v4 pool, with WETH and ETH unwrap', () => {
expect(isValidTokenPath(pool_v4_weth_eth, pool_v4_1_eth, ETHER)).toBe(true)
})
})
4 changes: 3 additions & 1 deletion sdks/router-sdk/src/utils/isValidTokenPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Pool as V4Pool } from '@uniswap/v4-sdk'
import { TPool } from './TPool'

export function isValidTokenPath(prevPool: TPool, currentPool: TPool, inputToken: Currency): boolean {
if (currentPool.involvesToken(inputToken as Token)) return true
if (inputToken instanceof Token && currentPool.involvesToken(inputToken)) return true

if (currentPool instanceof V4Pool && currentPool.involvesToken(inputToken)) return true

// throw if both v4 pools, native/wrapped tokens not interchangeable in v4
if (prevPool instanceof V4Pool && currentPool instanceof V4Pool) return false
Expand Down

0 comments on commit d0ffdb8

Please sign in to comment.