Skip to content

Commit

Permalink
feat(router-sdk): add getter for amounts which returns native amounts…
Browse files Browse the repository at this point in the history
… in a trade (#284)
  • Loading branch information
zhongeric authored Feb 6, 2025
1 parent 13a909c commit 210d614
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 5 deletions.
7 changes: 7 additions & 0 deletions sdks/router-sdk/src/entities/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,11 @@ describe('RouteV2', () => {
expect(route.input).toEqual(token0)
expect(route.output).toEqual(ETHER)
})

it('assigns pathInput and pathOutput correctly', () => {
const routeOriginal = new V2RouteSDK([pair_0_weth], token0, ETHER)
const route = new RouteV2(routeOriginal)
expect(route.pathInput).toEqual(token0)
expect(route.pathOutput).toEqual(weth)
})
})
22 changes: 22 additions & 0 deletions sdks/router-sdk/src/entities/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import { Protocol } from './protocol'
import { Currency, Price, Token } from '@uniswap/sdk-core'
import { MixedRouteSDK } from './mixedRoute/route'

// Helper function to get the pathInput and pathOutput for a V2 / V3 route
// currency could be native so we check against the wrapped version as they don't support native ETH in path
export function getPathToken(currency: Currency, pool: Pair | V3Pool): Token {
if (pool.token0.wrapped.equals(currency.wrapped)) {
return pool.token0
} else if (pool.token1.wrapped.equals(currency.wrapped)) {
return pool.token1
} else {
throw new Error(`Expected token ${currency.symbol} to be either ${pool.token0.symbol} or ${pool.token1.symbol}`)
}
}

export interface IRoute<TInput extends Currency, TOutput extends Currency, TPool extends Pair | V3Pool | V4Pool> {
protocol: Protocol
// array of pools if v3 or pairs if v2
Expand All @@ -15,6 +27,8 @@ export interface IRoute<TInput extends Currency, TOutput extends Currency, TPool
midPrice: Price<TInput, TOutput>
input: TInput
output: TOutput
pathInput: Currency
pathOutput: Currency
}

// V2 route wrapper
Expand All @@ -24,10 +38,14 @@ export class RouteV2<TInput extends Currency, TOutput extends Currency>
{
public readonly protocol: Protocol = Protocol.V2
public readonly pools: Pair[]
public pathInput: Currency
public pathOutput: Currency

constructor(v2Route: V2RouteSDK<TInput, TOutput>) {
super(v2Route.pairs, v2Route.input, v2Route.output)
this.pools = this.pairs
this.pathInput = getPathToken(v2Route.input, this.pairs[0])
this.pathOutput = getPathToken(v2Route.output, this.pairs[this.pairs.length - 1])
}
}

Expand All @@ -38,10 +56,14 @@ export class RouteV3<TInput extends Currency, TOutput extends Currency>
{
public readonly protocol: Protocol = Protocol.V3
public readonly path: Token[]
public pathInput: Currency
public pathOutput: Currency

constructor(v3Route: V3RouteSDK<TInput, TOutput>) {
super(v3Route.pools, v3Route.input, v3Route.output)
this.path = v3Route.tokenPath
this.pathInput = getPathToken(v3Route.input, this.pools[0])
this.pathOutput = getPathToken(v3Route.output, this.pools[this.pools.length - 1])
}
}

Expand Down
Loading

0 comments on commit 210d614

Please sign in to comment.