Skip to content

Commit

Permalink
fix(router-sdk): fix MixedRoute tokenpath (#287)
Browse files Browse the repository at this point in the history
## 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(<type>):` will not trigger any release and should be used for internal repo changes
- `<type>(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]_
  • Loading branch information
jsy1218 authored Jan 31, 2025
1 parent be5aa26 commit 1bd65ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 9 additions & 3 deletions sdks/router-sdk/src/entities/mixedRoute/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('MixedRoute', () => {
const pool_v4_0_weth = new V4Pool(token0, weth, FeeAmount.MEDIUM, 60, ADDRESS_ZERO, SQRT_RATIO_ONE, 0, 0, [])
const pool_v4_1_eth = new V4Pool(token1, ETHER, FeeAmount.MEDIUM, 60, ADDRESS_ZERO, SQRT_RATIO_ONE, 0, 0, [])
const pool_v4_0_1 = new V4Pool(token0, token1, FeeAmount.MEDIUM, 60, ADDRESS_ZERO, SQRT_RATIO_ONE, 0, 0, [])
const pool_v4_weth_eth = new V4Pool(weth, ETHER, 0, 0, ADDRESS_ZERO, SQRT_RATIO_ONE, 0, 0)

const pool_v3_0_1 = new V3Pool(token0, token1, FeeAmount.MEDIUM, encodeSqrtRatioX96(1, 1), 0, 0, [])
const pool_v3_0_weth = new V3Pool(token0, weth, FeeAmount.MEDIUM, encodeSqrtRatioX96(1, 1), 0, 0, [])
Expand All @@ -34,6 +35,11 @@ describe('MixedRoute', () => {
const pair_2_3 = new Pair(CurrencyAmount.fromRawAmount(token2, '100'), CurrencyAmount.fromRawAmount(token3, '200'))

describe('path', () => {
it('real v3 weth pool and fake v4 eth/weth pool', () => {
const route = new MixedRouteSDK([pool_v3_0_weth, pool_v4_weth_eth], token0, ETHER)
expect(route.path).toEqual([token0, weth, ETHER])
})

it('wraps pure v3 route object and successfully constructs a path from the tokens', () => {
/// @dev since the MixedRoute sdk object lives here in router-sdk we don't need to wrap it
const routeOriginal = new MixedRouteSDK([pool_v3_0_1], token0, token1)
Expand Down Expand Up @@ -84,9 +90,9 @@ describe('MixedRoute', () => {
})

it('wraps mixed route object with mixed v4 route that converts WETH -> ETH ', () => {
const route = new MixedRouteSDK([pool_v3_0_weth, pool_v4_1_eth], token0, token1)
expect(route.pools).toEqual([pool_v3_0_weth, pool_v4_1_eth])
expect(route.path).toEqual([token0, weth, token1])
const route = new MixedRouteSDK([pool_v3_0_weth, pool_v4_weth_eth, pool_v4_1_eth], token0, token1)
expect(route.pools).toEqual([pool_v3_0_weth, pool_v4_weth_eth, pool_v4_1_eth])
expect(route.path).toEqual([token0, weth, ETHER, token1])
expect(route.input).toEqual(token0)
expect(route.output).toEqual(token1)
expect(route.pathInput).toEqual(token0)
Expand Down
12 changes: 10 additions & 2 deletions sdks/router-sdk/src/entities/mixedRoute/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,17 @@ export class MixedRouteSDK<TInput extends Currency, TOutput extends Currency> {
const prevPool = pools[i - 1]
const pool = pools[i]
const inputToken = tokenPath[i]
const outputToken = pool.token0.wrapped.equals(inputToken.wrapped) ? pool.token1 : pool.token0

invariant(isValidTokenPath(prevPool, pool, inputToken), 'PATH')
const outputToken =
pool instanceof V4Pool
? pool.token0.equals(inputToken)
? pool.token1
: pool.token0
: pool.token0.wrapped.equals(inputToken.wrapped)
? pool.token1
: pool.token0

invariant(isValidTokenPath(prevPool, pool, inputToken), `PATH`)
tokenPath.push(outputToken)
}

Expand Down

0 comments on commit 1bd65ba

Please sign in to comment.