Skip to content

Commit

Permalink
fix(universal-router-sdk): bump router-sdk to 1.10.0 and add v4-sdk t…
Browse files Browse the repository at this point in the history
…o support SOR V4 (#68)

## Description

bump router-sdk to 1.10.0 and add v4-sdk 1.0.0. This is needed for SOR.

## How Has This Been Tested?

I used local tarball of router-sdk, v4-sdk and ur-sdk inside SOR for the v4 subgraph download. I need to bump router-sdk in ur-sdk

## Are there any breaking changes?

No

## (Optional) Feedback Focus

No

## (Optional) Follow Ups

https://linear.app/uniswap/issue/ROUTE-219/support-v4-trade-in-universal-router-sdk
  • Loading branch information
jsy1218 authored Aug 7, 2024
1 parent 21309d7 commit cd01c63
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
3 changes: 2 additions & 1 deletion sdks/universal-router-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
},
"dependencies": {
"@uniswap/permit2-sdk": "^1.3.0",
"@uniswap/router-sdk": "^1.9.3",
"@uniswap/router-sdk": "^1.10.0",
"@uniswap/sdk-core": "^5.3.1",
"@uniswap/universal-router": "1.6.0",
"@uniswap/v2-sdk": "^4.4.1",
"@uniswap/v3-sdk": "^3.13.1",
"@uniswap/v4-sdk": "^1.0.0",
"bignumber.js": "^9.0.2",
"ethers": "^5.7.0"
},
Expand Down
17 changes: 9 additions & 8 deletions sdks/universal-router-sdk/src/entities/protocols/uniswap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RoutePlanner, CommandType } from '../../utils/routerCommands'
import { Trade as V2Trade, Pair } from '@uniswap/v2-sdk'
import { Trade as V3Trade, Pool, encodeRouteToPath } from '@uniswap/v3-sdk'
import { Trade as V3Trade, Pool as V3Pool, encodeRouteToPath } from '@uniswap/v3-sdk'
import { Pool as V4Pool } from '@uniswap/v4-sdk'
import {
Trade as RouterTrade,
MixedRouteTrade,
Expand Down Expand Up @@ -41,7 +42,7 @@ export type SwapOptions = Omit<RouterSwapOptions, 'inputTokenPermit'> & {
const REFUND_ETH_PRICE_IMPACT_THRESHOLD = new Percent(50, 100)

interface Swap<TInput extends Currency, TOutput extends Currency> {
route: IRoute<TInput, TOutput, Pair | Pool>
route: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>
inputAmount: CurrencyAmount<TInput>
outputAmount: CurrencyAmount<TOutput>
}
Expand Down Expand Up @@ -187,15 +188,15 @@ function addV2Swap<TInput extends Currency, TOutput extends Currency>(
routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient,
trade.maximumAmountIn(options.slippageTolerance).quotient.toString(),
trade.minimumAmountOut(options.slippageTolerance).quotient.toString(),
route.path.map((pool) => pool.address),
route.path.map((token) => token.wrapped.address),
payerIsUser,
])
} else if (tradeType == TradeType.EXACT_OUTPUT) {
planner.addCommand(CommandType.V2_SWAP_EXACT_OUT, [
routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient,
trade.minimumAmountOut(options.slippageTolerance).quotient.toString(),
trade.maximumAmountIn(options.slippageTolerance).quotient.toString(),
route.path.map((pool) => pool.address),
route.path.map((token) => token.wrapped.address),
payerIsUser,
])
}
Expand Down Expand Up @@ -251,7 +252,7 @@ function addMixedSwap<TInput extends Currency, TOutput extends Currency>(

// single hop, so it can be reduced to plain v2 or v3 swap logic
if (route.pools.length === 1) {
if (route.pools[0] instanceof Pool) {
if (route.pools[0] instanceof V3Pool) {
return addV3Swap(planner, swap, tradeType, options, payerIsUser, routerMustCustody)
} else if (route.pools[0] instanceof Pair) {
return addV2Swap(planner, swap, tradeType, options, payerIsUser, routerMustCustody)
Expand Down Expand Up @@ -293,10 +294,10 @@ function addMixedSwap<TInput extends Currency, TOutput extends Currency>(
const newRoute = new MixedRoute(newRouteOriginal)

/// Previous output is now input
inputToken = outputToken
inputToken = outputToken.wrapped

const mixedRouteIsAllV3 = (route: MixedRouteSDK<Currency, Currency>) => {
return route.pools.every((pool) => pool instanceof Pool)
return route.pools.every((pool) => pool instanceof V3Pool)
}

if (mixedRouteIsAllV3(newRoute)) {
Expand All @@ -316,7 +317,7 @@ function addMixedSwap<TInput extends Currency, TOutput extends Currency>(
isLastSectionInRoute(i) ? tradeRecipient : ROUTER_AS_RECIPIENT, // recipient
i === 0 ? amountIn : CONTRACT_BALANCE, // amountIn
!isLastSectionInRoute(i) ? 0 : amountOut, // amountOutMin
newRoute.path.map((pool) => pool.address), // path
newRoute.path.map((token) => token.wrapped.address), // path
payerIsUser && i === 0,
])
}
Expand Down
2 changes: 2 additions & 0 deletions sdks/universal-router-sdk/src/utils/routerTradeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export class RouterTradeAdapter {
inputAmount: route.inputAmount,
outputAmount: route.outputAmount,
})),
// TODO: ROUTE-219 - Support v4 trade in universal-router sdk
v4Routes: [],
mixedRoutes: typedRoutes
.filter((route) => route.mixedRoute)
.map((route) => ({
Expand Down
4 changes: 3 additions & 1 deletion sdks/universal-router-sdk/test/utils/uniswapData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import JSBI from 'jsbi'
import { BigNumber, ethers } from 'ethers'
import { ethers } from 'ethers'
import { MixedRouteTrade, MixedRouteSDK, Trade as RouterTrade } from '@uniswap/router-sdk'
import { Trade as V2Trade, Pair, Route as RouteV2, computePairAddress } from '@uniswap/v2-sdk'
import {
Expand Down Expand Up @@ -151,6 +151,8 @@ export function buildTrade(
inputAmount: trade.inputAmount,
outputAmount: trade.outputAmount,
})),
// TODO: ROUTE-219 - Support v4 trade in universal-router sdk
v4Routes: [],
mixedRoutes: trades
.filter((trade) => trade instanceof MixedRouteTrade)
.map((trade) => ({
Expand Down
12 changes: 7 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4602,16 +4602,17 @@ __metadata:
languageName: unknown
linkType: soft

"@uniswap/router-sdk@npm:^1.9.3":
version: 1.9.3
resolution: "@uniswap/router-sdk@npm:1.9.3"
"@uniswap/router-sdk@npm:^1.10.0":
version: 1.10.0
resolution: "@uniswap/router-sdk@npm:1.10.0"
dependencies:
"@ethersproject/abi": ^5.5.0
"@uniswap/sdk-core": ^5.3.1
"@uniswap/swap-router-contracts": ^1.3.0
"@uniswap/v2-sdk": ^4.3.2
"@uniswap/v3-sdk": ^3.11.2
checksum: 92922d43432908b9159e0f9cd1c9bb9658ceb89fb1e8e5ef4fc0350b4309cbd3767586135887333bb8d586c5ed304d2c2910b86bf276485421d2be025101485f
"@uniswap/v4-sdk": ^1.0.0
checksum: df468dfd4c60ad311e51ce6414b67e3ee1fb07fde69979c468f478d84b6754a9a7e23c7d20b0bac53a9092c04b17fae2da365e8e084f953c39c4204e62c2268c
languageName: node
linkType: hard

Expand Down Expand Up @@ -4721,11 +4722,12 @@ __metadata:
"@types/node": ^18.7.16
"@types/node-fetch": ^2.6.2
"@uniswap/permit2-sdk": ^1.3.0
"@uniswap/router-sdk": ^1.9.3
"@uniswap/router-sdk": ^1.10.0
"@uniswap/sdk-core": ^5.3.1
"@uniswap/universal-router": 1.6.0
"@uniswap/v2-sdk": ^4.4.1
"@uniswap/v3-sdk": ^3.13.1
"@uniswap/v4-sdk": ^1.0.0
bignumber.js: ^9.0.2
chai: ^4.3.6
dotenv: ^16.0.3
Expand Down

0 comments on commit cd01c63

Please sign in to comment.