Skip to content

Commit

Permalink
add support for weth input requiring unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
snreynolds committed Feb 7, 2025
1 parent 02b8465 commit 3d556b4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
28 changes: 28 additions & 0 deletions sdks/router-sdk/src/entities/trade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1591,4 +1591,32 @@ describe('Trade', () => {
expect(trade.executionPrice).toEqual(expectedPrice)
})
})

describe('split route', () => {
it('returns correct #numberOfSplitsRequiringUnwrap', async() => {

Check failure on line 1596 in sdks/router-sdk/src/entities/trade.test.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Insert `·`
// TRADE OBJECT
// input : protocol : path
// [WETH] : v2 : [WETH - token1]
// [WETH] : v4 : [ETH - token1]

const routev2 = new V2RouteSDK([pair_weth_1], weth, token1)
const routev4 = new V4RouteSDK([pool_v4_1_eth], weth, token1)
const amountv2 = CurrencyAmount.fromRawAmount(weth, 100)
const amountv4 = CurrencyAmount.fromRawAmount(weth, 200)

const splitTrade = await Trade.fromRoutes(
[{ routev2, amount: amountv2 }],
[],
TradeType.EXACT_INPUT,
[],
[{ routev4, amount: amountv4 }]
)

expect(splitTrade.numberOfSplitsRequiringUnwrap).toEqual(1)
expect(splitTrade.nativeRoutes.length).toEqual(1)
expect(splitTrade.nativeRoutes[0]).toEqual(new RouteV4(routev4))
expect(splitTrade.wethRoutes.length).toEqual(1)
expect(splitTrade.wethRoutes[0]).toEqual(new RouteV2(routev2))
})
})
})
35 changes: 34 additions & 1 deletion sdks/router-sdk/src/entities/trade.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Currency, CurrencyAmount, Fraction, Percent, Price, TradeType } from '@uniswap/sdk-core'
import { Currency, CurrencyAmount, Fraction, Percent, Price, TradeType, Ether } from '@uniswap/sdk-core'
import { Pair, Route as V2RouteSDK, Trade as V2TradeSDK } from '@uniswap/v2-sdk'
import { Pool as V3Pool, Route as V3RouteSDK, Trade as V3TradeSDK } from '@uniswap/v3-sdk'
import { Pool as V4Pool, Route as V4RouteSDK, Trade as V4TradeSDK } from '@uniswap/v4-sdk'
Expand All @@ -13,6 +13,8 @@ export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType
public readonly tradeType: TTradeType
private _outputAmount: CurrencyAmount<TOutput> | undefined
private _inputAmount: CurrencyAmount<TInput> | undefined
private _nativeRoutes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[] | undefined
private _wethRoutes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[] | undefined

/**
* The swaps of the trade, i.e. which routes and how much is swapped in each that
Expand Down Expand Up @@ -202,6 +204,32 @@ export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType
}
}


Check failure on line 207 in sdks/router-sdk/src/entities/trade.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Replace `⏎··public·get·numberOfSplitsRequiringUnwrap()·` with `··public·get·numberOfSplitsRequiringUnwrap()`
public get numberOfSplitsRequiringUnwrap() : number {
// if the trade's input is weth, it may require an unwrap
if (this.isWrappedNative(this.inputAmount.currency)) {
return this.nativeRoutes.length
} else return 0
}

public get nativeRoutes() : IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[] {

Check failure on line 215 in sdks/router-sdk/src/entities/trade.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Delete `·`
if (this._nativeRoutes) {
return this._nativeRoutes
}

this._nativeRoutes = this.routes.filter(route => route.pathInput.isNative)

Check failure on line 220 in sdks/router-sdk/src/entities/trade.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Replace `(route` with `((route)`
return this._nativeRoutes
}

public get wethRoutes() : IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[] {

Check failure on line 224 in sdks/router-sdk/src/entities/trade.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Delete `·`
if (this._wethRoutes) {
return this._wethRoutes
}

this._wethRoutes = this.routes.filter(route => this.isWrappedNative(route.pathInput))

Check failure on line 229 in sdks/router-sdk/src/entities/trade.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Replace `route` with `(route)`
return this._wethRoutes
}

private _executionPrice: Price<TInput, TOutput> | undefined

/**
Expand Down Expand Up @@ -239,6 +267,11 @@ export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType
return new Percent(outputCurrency.wrapped.buyFeeBps.toNumber(), 10000)
}

private isWrappedNative(currency : Currency): boolean {

Check failure on line 270 in sdks/router-sdk/src/entities/trade.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Replace `··private·isWrappedNative(currency·` with `private·isWrappedNative(currency`
const chainId = currency.chainId

Check failure on line 271 in sdks/router-sdk/src/entities/trade.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Delete `··`
return currency.equals(Ether.onChain(chainId).wrapped)

Check failure on line 272 in sdks/router-sdk/src/entities/trade.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test

Delete `··`
}

/**
* The cached result of the price impact computation
* @private
Expand Down

0 comments on commit 3d556b4

Please sign in to comment.