Skip to content

Commit dd4d598

Browse files
ewilzgretzke
andauthored
fix(universal-router-sdk): Strip unviersal-router-sdk of NFT commands to match new UR contracts (#83)
Co-authored-by: gretzke <[email protected]>
1 parent ce9ca55 commit dd4d598

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+150
-3233
lines changed

.github/workflows/monorepo-checks.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: ✅ Checkout
2525
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744
2626
with:
27-
submodules: "true"
27+
submodules: "recursive"
2828
fetch-depth: 2
2929

3030
- name: 💽 Node and Caching

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@
2323
path = sdks/universal-router-sdk/lib/openzeppelin-contracts
2424
url = https://github.com/openzeppelin/openzeppelin-contracts
2525
branch = v4.8.0
26+
[submodule "sdks/universal-router-sdk/lib/universal-router"]
27+
path = sdks/universal-router-sdk/lib/universal-router
28+
url = https://github.com/Uniswap/universal-router

sdks/universal-router-sdk/README.md

+3-77
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,7 @@ This SDK facilitates interactions with the contracts in [Universal Router](https
44
## Usage
55
Install latest version of universal-router-sdk. Then import the corresponding Trade class and Data object for each protocol you'd like to interact with.
66

7-
### Trading NFTs
8-
warning: `swapNFTCallParameters()` to be deprecated in favor of `swapCallParameters()`
9-
```typescript
10-
import {
11-
LooksRareTrade,
12-
LooksRareData,
13-
SeaportTrade,
14-
SeaportData
15-
} from "@uniswap/universal-router-sdk";
16-
17-
// Each protocol data object contains 1 call to that protocol. Some protocols can fit
18-
// many NFT purchase within 1 call, like seaport. Others require multiple calls per NFT (like LooksRare).
19-
const looksRareTrades = new LooksRareTrade([looksrareData1, looksrareData2])
20-
const seaportTrades = new SeaportTrade([seaportData1])
21-
22-
// Use the raw calldata and value returned to call into Universal Swap Router contracts
23-
// Trades will happen in the order that they are handed in
24-
const { calldata, value } = SwapRouter.swapCallParameters([looksRareTrades, seaportTrades])
25-
```
26-
27-
### Trading ERC20s on Uniswap
7+
### Trading on Uniswap
288
warning: `swapERC20CallParameters()` to be deprecated in favor of `swapCallParameters()`
299
```typescript
3010
import { TradeType } from '@uniswap/sdk-core'
@@ -33,63 +13,9 @@ import { Trade as V3TradeSDK } from '@uniswap/v3-sdk'
3313
import { MixedRouteTrade, MixedRouteSDK, Trade as RouterTrade } from '@uniswap/router-sdk'
3414

3515
const options = { slippageTolerance, recipient }
36-
const routerTrade = new UniswapTrade(
37-
new RouterTrade({ v2Routes, v3Routes, mixedRoutes, tradeType: TradeType.EXACT_INPUT },
38-
options
39-
)
40-
// Use the raw calldata and value returned to call into Universal Swap Router contracts
41-
const { calldata, value } = SwapRouter.swapCallParameters(routerTrade)
42-
```
43-
44-
### Using Uniswap for ERC20 NFT Trades
45-
Send ETH to the router by trading an ERC20 for ETH with a Uniswap Trade and encoding the swap recipient as `ROUTER_AS_RECIPIENT` in the trade. Then subsequently list the NFT trades to use the ETH output to buy NFTs. Trades happen in the order they are listed.
46-
47-
Use `trade_type: TradeType.EXACT_OUTPUT` to cover the entire NFT price, alternatively the transaction will send supplemental ETH to fulfill the entire price if the swap does not cover it in full. Keep in mind that `TradeType.EXACT_INPUT` trades are subject to slippage on output, and ETH will be sent to cover potential slippage and any remaining ETH will be returned to sender.
48-
```typescript
49-
import { TradeType } from '@uniswap/sdk-core'
50-
import { Trade as V2TradeSDK } from '@uniswap/v2-sdk'
51-
import { Trade as V3TradeSDK } from '@uniswap/v3-sdk'
52-
import { MixedRouteTrade, MixedRouteSDK, Trade as RouterTrade } from '@uniswap/router-sdk'
53-
import {
54-
ROUTER_AS_RECIPIENT,
55-
UniswapTrade,
56-
LooksRareTrade,
57-
LooksRareData,
58-
SeaportTrade,
59-
SeaportData
60-
} from "@uniswap/universal-router-sdk";
61-
62-
const looksRareTrades = new LooksRareTrade([looksrareData1, looksrareData2])
63-
const seaportTrades = new SeaportTrade([seaportData1])
64-
// WARNING: never send funds to ROUTER_AS_RECIPIENT unless it is ETH that will be used in NFT trades, otherwise funds are lost.
65-
const uniswapTrade = new UniswapTrade(
66-
new RouterTrade({ v2Routes, v3Routes, mixedRoutes, tradeType: TradeType.EXACT_OUTPUT }),
67-
{ slippageTolerance, recipient: ROUTER_AS_RECIPIENT}
68-
)
69-
// Use the raw calldata and value returned to call into Universal Swap Router contracts
70-
const { calldata, value } = SwapRouter.swapCallParameters([uniswapTrade, seaportTrades, looksRareTrades])
71-
```
72-
73-
### Using WETH for NFT Trades
74-
The current router purchases all NFTs with ETH, but you can send WETH to the router to be unwrapped for ETH right before the NFT commands. Similar to ERC20 Uniswap Trades for NFTs, supplemental ETH will be sent in the transaction if the WETH amount will not cover the NFT buys. You can also use ERC20s and WETH to cover the transaction by including both commands before the NFT purchase.
75-
76-
```typescript
77-
import {
78-
ROUTER_AS_RECIPIENT,
79-
UniswapTrade,
80-
LooksRareTrade,
81-
LooksRareData,
82-
SeaportTrade,
83-
SeaportData
84-
} from "@uniswap/universal-router-sdk";
85-
86-
const looksRareTrades = new LooksRareTrade([looksrareData1, looksrareData2])
87-
const seaportTrades = new SeaportTrade([seaportData1])
88-
// if no Permit needed, omit the third var of type Permit2Permit
89-
const unwrapWETH = new UnwrapWETH(amountWETH, chainId, optionalPermit2Params)
90-
16+
const routerTrade = new RouterTrade({ v2Routes, v3Routes, mixedRoutes, tradeType: TradeType.EXACT_INPUT })
9117
// Use the raw calldata and value returned to call into Universal Swap Router contracts
92-
const { calldata, value } = SwapRouter.swapCallParameters([unwrapWETH, seaportTrades, looksRareTrades])
18+
const { calldata, value } = SwapRouter.swapCallParameters(routerTrade, options)
9319
```
9420

9521
## Running this package
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[profile.default]
2-
solc = "0.8.17"
3-
fs_permissions = [{ access = "read", path = "./permit2/out/Permit2.sol/Permit2.json"}, { access = "read", path = "./test/forge/interop.json"}]
2+
solc = "0.8.26"
3+
fs_permissions = [{ access = "read", path = "./permit2/out/Permit2.sol/Permit2.json"}, { access = "read", path = "./test/forge/interop.json"}, { access = "read", path = "./test/forge/bin/permit2.bin"}]
44
src = "./test/forge"
55
via_ir = true
6+
evm_version = "cancun"

sdks/universal-router-sdk/lib/permit2

Submodule permit2 updated 41 files

sdks/universal-router-sdk/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@
1919
"build": "tsdx build",
2020
"docs": "typedoc",
2121
"forge:fix": "forge fmt",
22-
"install:ur": "cd node_modules/@uniswap/universal-router && forge install",
2322
"lint": "yarn prettier",
2423
"lint:fix": "yarn prettier:fix && yarn forge:fix",
2524
"prettier": "prettier --check '**/*.ts' && prettier --check '**/*.json'",
2625
"prettier:fix": "prettier --write '**/*.ts' && prettier --write '**/*.json'",
2726
"release": "semantic-release",
2827
"test": "yarn test:hardhat && yarn test:forge",
29-
"test:forge": "yarn install:ur && forge test",
28+
"test:forge": "forge test",
3029
"test:hardhat": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' hardhat test"
3130
},
3231
"dependencies": {
32+
"@openzeppelin/contracts": "4.7.0",
3333
"@uniswap/permit2-sdk": "^1.3.0",
34-
"@uniswap/router-sdk": "^1.10.0",
34+
"@uniswap/router-sdk": "^1.11.0",
3535
"@uniswap/sdk-core": "^5.3.1",
36-
"@uniswap/universal-router": "1.6.0",
36+
"@uniswap/universal-router": "2.0.0-beta.1",
37+
"@uniswap/v2-core": "^1.0.1",
3738
"@uniswap/v2-sdk": "^4.4.1",
39+
"@uniswap/v3-core": "1.0.0",
3840
"@uniswap/v3-sdk": "^3.13.1",
3941
"@uniswap/v4-sdk": "^1.0.0",
4042
"bignumber.js": "^9.0.2",
+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
@ensdomains/=node_modules/@uniswap/universal-router/node_modules/@ensdomains/
2-
@openzeppelin/=lib/openzeppelin-contracts/
3-
@uniswap/=node_modules/@uniswap/
42
base64-sol/=node_modules/base64-sol/
53
ds-test/=lib/forge-std/lib/ds-test/src/
64
forge-std/=lib/forge-std/src/
75
hardhat/=node_modules/@uniswap/universal-router/node_modules/hardhat/
8-
openzeppelin-contracts=lib/openzeppelin-contracts/
96
permit2/=lib/permit2/
10-
universal-router/=node_modules/@uniswap/universal-router/contracts
7+
universal-router/=lib/universal-router/contracts
8+
@openzeppelin/contracts/=node_modules/@openzeppelin/contracts/
9+
@uniswap/v3-periphery/=lib/universal-router/lib/v3-periphery/
10+
@uniswap/v4-periphery/=lib/universal-router/lib/v4-periphery/
11+
@uniswap/v4-core/=lib/universal-router/lib/v4-periphery/lib/v4-core/
12+
@uniswap/v2-core/=node_modules/@uniswap/v2-core/
13+
@uniswap/v3-core/=node_modules/@uniswap/v3-core/
1114
solmate/=lib/solmate/
1215
v2-core/=node_modules/@uniswap/universal-router/lib/v2-core/
1316
v3-core/=node_modules/@uniswap/universal-router/lib/v3-core/

sdks/universal-router-sdk/src/entities/Command.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ export type TradeConfig = {
44
allowRevert: boolean
55
}
66

7-
export enum RouterTradeType {
7+
export enum RouterActionType {
88
UniswapTrade = 'UniswapTrade',
9-
NFTTrade = 'NFTTrade',
109
UnwrapWETH = 'UnwrapWETH',
1110
}
1211

1312
// interface for entities that can be encoded as a Universal Router command
1413
export interface Command {
15-
tradeType: RouterTradeType
14+
tradeType: RouterActionType
1615
encode(planner: RoutePlanner, config: TradeConfig): void
1716
}

sdks/universal-router-sdk/src/entities/NFTTrade.ts

-68
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './uniswap'
2+
export * from './unwrapWETH'

sdks/universal-router-sdk/src/entities/protocols/uniswap.ts sdks/universal-router-sdk/src/entities/actions/uniswap.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '@uniswap/router-sdk'
1919
import { Permit2Permit } from '../../utils/inputTokens'
2020
import { Currency, TradeType, CurrencyAmount, Percent } from '@uniswap/sdk-core'
21-
import { Command, RouterTradeType, TradeConfig } from '../Command'
21+
import { Command, RouterActionType, TradeConfig } from '../Command'
2222
import { SENDER_AS_RECIPIENT, ROUTER_AS_RECIPIENT, CONTRACT_BALANCE, ETH_ADDRESS } from '../../utils/constants'
2323
import { encodeFeeBips } from '../../utils/numbers'
2424
import { BigNumber, BigNumberish } from 'ethers'
@@ -50,12 +50,11 @@ interface Swap<TInput extends Currency, TOutput extends Currency> {
5050
// Wrapper for uniswap router-sdk trade entity to encode swaps for Universal Router
5151
// also translates trade objects from previous (v2, v3) SDKs
5252
export class UniswapTrade implements Command {
53-
readonly tradeType: RouterTradeType = RouterTradeType.UniswapTrade
53+
readonly tradeType: RouterActionType = RouterActionType.UniswapTrade
5454
readonly payerIsUser: boolean
5555

5656
constructor(public trade: RouterTrade<Currency, Currency, TradeType>, public options: SwapOptions) {
5757
if (!!options.fee && !!options.flatFee) throw new Error('Only one fee option permitted')
58-
5958
if (this.inputRequiresWrap) this.payerIsUser = false
6059
else if (this.options.useRouterBalance) this.payerIsUser = false
6160
else this.payerIsUser = true

sdks/universal-router-sdk/src/entities/protocols/unwrapWETH.ts sdks/universal-router-sdk/src/entities/actions/unwrapWETH.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import invariant from 'tiny-invariant'
22
import { BigNumberish } from 'ethers'
33
import { RoutePlanner, CommandType } from '../../utils/routerCommands'
44
import { encodeInputTokenOptions, Permit2Permit } from '../../utils/inputTokens'
5-
import { Command, RouterTradeType, TradeConfig } from '../Command'
5+
import { Command, RouterActionType, TradeConfig } from '../Command'
66
import { ROUTER_AS_RECIPIENT, WETH_ADDRESS } from '../../utils/constants'
77

88
export class UnwrapWETH implements Command {
9-
readonly tradeType: RouterTradeType = RouterTradeType.UnwrapWETH
9+
readonly tradeType: RouterActionType = RouterActionType.UnwrapWETH
1010
readonly permit2Data: Permit2Permit
1111
readonly wethAddress: string
1212
readonly amount: BigNumberish
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from './protocols'
2-
export * from './NFTTrade'
1+
export * from './actions'
32
export * from './Command'

sdks/universal-router-sdk/src/entities/protocols/cryptopunk.ts

-44
This file was deleted.

0 commit comments

Comments
 (0)