@uniswap/universal-router-sdk 1.2.3 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -3
- package/dist/entities/Command.d.ts +5 -0
- package/dist/entities/NFTTrade.d.ts +4 -3
- package/dist/entities/index.d.ts +1 -0
- package/dist/entities/protocols/cryptopunk.d.ts +2 -2
- package/dist/entities/protocols/foundation.d.ts +2 -2
- package/dist/entities/protocols/looksRare.d.ts +2 -2
- package/dist/entities/protocols/nft20.d.ts +2 -2
- package/dist/entities/protocols/nftx.d.ts +1 -1
- package/dist/entities/protocols/sudoswap.d.ts +2 -2
- package/dist/entities/protocols/uniswap.d.ts +2 -1
- package/dist/entities/protocols/x2y2.d.ts +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/swapRouter.d.ts +7 -1
- package/dist/universal-router-sdk.cjs.development.js +89 -19
- package/dist/universal-router-sdk.cjs.development.js.map +1 -1
- package/dist/universal-router-sdk.cjs.production.min.js +1 -1
- package/dist/universal-router-sdk.cjs.production.min.js.map +1 -1
- package/dist/universal-router-sdk.esm.js +90 -20
- package/dist/universal-router-sdk.esm.js.map +1 -1
- package/dist/utils/constants.d.ts +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@ This SDK facilitates interactions with the contracts in [Universal Router](https
|
|
|
5
5
|
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.
|
|
6
6
|
|
|
7
7
|
### Trading NFTs
|
|
8
|
+
warning: `swapNFTCallParameters()` to be deprecated in favor of `swapCallParameters()`
|
|
8
9
|
```typescript
|
|
9
10
|
import {
|
|
10
11
|
LooksRareTrade,
|
|
@@ -20,10 +21,11 @@ const seaportTrades = new SeaportTrade([seaportData1])
|
|
|
20
21
|
|
|
21
22
|
// Use the raw calldata and value returned to call into Universal Swap Router contracts
|
|
22
23
|
// Trades will happen in the order that they are handed in
|
|
23
|
-
const { calldata, value } = SwapRouter.
|
|
24
|
+
const { calldata, value } = SwapRouter.swapCallParameters([looksRareTrades, seaportTrades])
|
|
24
25
|
```
|
|
25
26
|
|
|
26
27
|
### Trading ERC20s on Uniswap
|
|
28
|
+
warning: `swapERC20CallParameters()` to be deprecated in favor of `swapCallParameters()`
|
|
27
29
|
```typescript
|
|
28
30
|
import { TradeType } from '@uniswap/sdk-core'
|
|
29
31
|
import { Trade as V2TradeSDK } from '@uniswap/v2-sdk'
|
|
@@ -31,11 +33,44 @@ import { Trade as V3TradeSDK } from '@uniswap/v3-sdk'
|
|
|
31
33
|
import { MixedRouteTrade, MixedRouteSDK, Trade as RouterTrade } from '@uniswap/router-sdk'
|
|
32
34
|
|
|
33
35
|
const options = { slippageTolerance, recipient }
|
|
34
|
-
const routerTrade = new
|
|
36
|
+
const routerTrade = new UniswapTrade(
|
|
37
|
+
new RouterTrade({ v2Routes, v3Routes, mixedRoutes, tradeType: TradeType.EXACT_INPUT },
|
|
38
|
+
options
|
|
39
|
+
)
|
|
35
40
|
// Use the raw calldata and value returned to call into Universal Swap Router contracts
|
|
36
|
-
const { calldata, value } = SwapRouter.
|
|
41
|
+
const { calldata, value } = SwapRouter.swapCallParameters(routerTrade)
|
|
37
42
|
```
|
|
38
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
|
+
|
|
39
74
|
## Running this package
|
|
40
75
|
Make sure you are running `node v16`
|
|
41
76
|
Install dependencies and run typescript unit tests
|
|
@@ -2,6 +2,11 @@ import { RoutePlanner } from '../utils/routerCommands';
|
|
|
2
2
|
export declare type TradeConfig = {
|
|
3
3
|
allowRevert: boolean;
|
|
4
4
|
};
|
|
5
|
+
export declare enum RouterTradeType {
|
|
6
|
+
UniswapTrade = "UniswapTrade",
|
|
7
|
+
NFTTrade = "NFTTrade"
|
|
8
|
+
}
|
|
5
9
|
export interface Command {
|
|
10
|
+
tradeType: RouterTradeType;
|
|
6
11
|
encode(planner: RoutePlanner, config: TradeConfig): void;
|
|
7
12
|
}
|
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import { BigNumberish } from 'ethers';
|
|
1
|
+
import { BigNumber, BigNumberish } from 'ethers';
|
|
2
2
|
import { SeaportData } from './protocols/seaport';
|
|
3
3
|
import { FoundationData } from './protocols/foundation';
|
|
4
4
|
import { NFTXData } from './protocols/nftx';
|
|
5
5
|
import { NFT20Data } from './protocols/nft20';
|
|
6
6
|
import { RoutePlanner } from '../utils/routerCommands';
|
|
7
|
-
import { Command, TradeConfig } from './Command';
|
|
7
|
+
import { Command, RouterTradeType, TradeConfig } from './Command';
|
|
8
8
|
import { LooksRareData } from './protocols/looksRare';
|
|
9
9
|
import { SudoswapData } from './protocols/sudoswap';
|
|
10
10
|
import { CryptopunkData } from './protocols/cryptopunk';
|
|
11
11
|
import { X2Y2Data } from './protocols/x2y2';
|
|
12
12
|
export declare type SupportedProtocolsData = SeaportData | FoundationData | NFTXData | LooksRareData | X2Y2Data | CryptopunkData | NFT20Data | SudoswapData;
|
|
13
13
|
export declare abstract class NFTTrade<T> implements Command {
|
|
14
|
+
readonly tradeType: RouterTradeType;
|
|
14
15
|
readonly orders: T[];
|
|
15
16
|
readonly market: Market;
|
|
16
17
|
constructor(market: Market, orders: T[]);
|
|
17
18
|
abstract encode(planner: RoutePlanner, config: TradeConfig): void;
|
|
18
19
|
abstract getBuyItems(): BuyItem[];
|
|
19
|
-
abstract getTotalPrice():
|
|
20
|
+
abstract getTotalPrice(): BigNumber;
|
|
20
21
|
}
|
|
21
22
|
export declare type BuyItem = {
|
|
22
23
|
tokenAddress: string;
|
package/dist/entities/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TradeConfig } from '../Command';
|
|
2
2
|
import { NFTTrade, BuyItem } from '../NFTTrade';
|
|
3
3
|
import { RoutePlanner } from '../../utils/routerCommands';
|
|
4
|
-
import { BigNumberish } from 'ethers';
|
|
4
|
+
import { BigNumber, BigNumberish } from 'ethers';
|
|
5
5
|
export declare type CryptopunkData = {
|
|
6
6
|
tokenId: BigNumberish;
|
|
7
7
|
recipient: string;
|
|
@@ -12,5 +12,5 @@ export declare class CryptopunkTrade extends NFTTrade<CryptopunkData> {
|
|
|
12
12
|
constructor(orders: CryptopunkData[]);
|
|
13
13
|
encode(planner: RoutePlanner, config: TradeConfig): void;
|
|
14
14
|
getBuyItems(): BuyItem[];
|
|
15
|
-
getTotalPrice():
|
|
15
|
+
getTotalPrice(): BigNumber;
|
|
16
16
|
}
|
|
@@ -2,7 +2,7 @@ import { Interface } from '@ethersproject/abi';
|
|
|
2
2
|
import { BuyItem, NFTTrade } from '../NFTTrade';
|
|
3
3
|
import { TradeConfig } from '../Command';
|
|
4
4
|
import { RoutePlanner } from '../../utils/routerCommands';
|
|
5
|
-
import { BigNumberish } from 'ethers';
|
|
5
|
+
import { BigNumber, BigNumberish } from 'ethers';
|
|
6
6
|
export declare type FoundationData = {
|
|
7
7
|
recipient: string;
|
|
8
8
|
tokenAddress: string;
|
|
@@ -15,5 +15,5 @@ export declare class FoundationTrade extends NFTTrade<FoundationData> {
|
|
|
15
15
|
constructor(orders: FoundationData[]);
|
|
16
16
|
encode(planner: RoutePlanner, config: TradeConfig): void;
|
|
17
17
|
getBuyItems(): BuyItem[];
|
|
18
|
-
getTotalPrice():
|
|
18
|
+
getTotalPrice(): BigNumber;
|
|
19
19
|
}
|
|
@@ -2,7 +2,7 @@ import { Interface } from '@ethersproject/abi';
|
|
|
2
2
|
import { BuyItem, NFTTrade, TokenType } from '../NFTTrade';
|
|
3
3
|
import { TradeConfig } from '../Command';
|
|
4
4
|
import { RoutePlanner } from '../../utils/routerCommands';
|
|
5
|
-
import { BigNumberish } from 'ethers';
|
|
5
|
+
import { BigNumber, BigNumberish } from 'ethers';
|
|
6
6
|
export declare type MakerOrder = {
|
|
7
7
|
collection: string;
|
|
8
8
|
tokenId: BigNumberish;
|
|
@@ -40,5 +40,5 @@ export declare class LooksRareTrade extends NFTTrade<LooksRareData> {
|
|
|
40
40
|
constructor(orders: LooksRareData[]);
|
|
41
41
|
encode(planner: RoutePlanner, config: TradeConfig): void;
|
|
42
42
|
getBuyItems(): BuyItem[];
|
|
43
|
-
getTotalPrice():
|
|
43
|
+
getTotalPrice(): BigNumber;
|
|
44
44
|
}
|
|
@@ -2,7 +2,7 @@ import { Interface } from '@ethersproject/abi';
|
|
|
2
2
|
import { TradeConfig } from '../Command';
|
|
3
3
|
import { NFTTrade, BuyItem } from '../NFTTrade';
|
|
4
4
|
import { RoutePlanner } from '../../utils/routerCommands';
|
|
5
|
-
import { BigNumberish } from 'ethers';
|
|
5
|
+
import { BigNumber, BigNumberish } from 'ethers';
|
|
6
6
|
export declare type NFT20Data = {
|
|
7
7
|
tokenAddress: string;
|
|
8
8
|
tokenIds: BigNumberish[];
|
|
@@ -17,5 +17,5 @@ export declare class NFT20Trade extends NFTTrade<NFT20Data> {
|
|
|
17
17
|
constructor(orders: NFT20Data[]);
|
|
18
18
|
encode(planner: RoutePlanner, config: TradeConfig): void;
|
|
19
19
|
getBuyItems(): BuyItem[];
|
|
20
|
-
getTotalPrice():
|
|
20
|
+
getTotalPrice(): BigNumber;
|
|
21
21
|
}
|
|
@@ -2,7 +2,7 @@ import { Interface } from '@ethersproject/abi';
|
|
|
2
2
|
import { BuyItem, NFTTrade } from '../NFTTrade';
|
|
3
3
|
import { TradeConfig } from '../Command';
|
|
4
4
|
import { RoutePlanner } from '../../utils/routerCommands';
|
|
5
|
-
import { BigNumberish } from 'ethers';
|
|
5
|
+
import { BigNumber, BigNumberish } from 'ethers';
|
|
6
6
|
declare type PairSwap = {
|
|
7
7
|
swapInfo: {
|
|
8
8
|
pair: string;
|
|
@@ -22,6 +22,6 @@ export declare class SudoswapTrade extends NFTTrade<SudoswapData> {
|
|
|
22
22
|
constructor(orders: SudoswapData[]);
|
|
23
23
|
encode(planner: RoutePlanner, config: TradeConfig): void;
|
|
24
24
|
getBuyItems(): BuyItem[];
|
|
25
|
-
getTotalPrice():
|
|
25
|
+
getTotalPrice(): BigNumber;
|
|
26
26
|
}
|
|
27
27
|
export {};
|
|
@@ -2,13 +2,14 @@ import { RoutePlanner } from '../../utils/routerCommands';
|
|
|
2
2
|
import { Trade as RouterTrade, SwapOptions as RouterSwapOptions } from '@uniswap/router-sdk';
|
|
3
3
|
import { Permit2Permit } from '../../utils/permit2';
|
|
4
4
|
import { Currency, TradeType } from '@uniswap/sdk-core';
|
|
5
|
-
import { Command, TradeConfig } from '../Command';
|
|
5
|
+
import { Command, RouterTradeType, TradeConfig } from '../Command';
|
|
6
6
|
export declare type SwapOptions = Omit<RouterSwapOptions, 'inputTokenPermit'> & {
|
|
7
7
|
inputTokenPermit?: Permit2Permit;
|
|
8
8
|
};
|
|
9
9
|
export declare class UniswapTrade implements Command {
|
|
10
10
|
trade: RouterTrade<Currency, Currency, TradeType>;
|
|
11
11
|
options: SwapOptions;
|
|
12
|
+
readonly tradeType: RouterTradeType;
|
|
12
13
|
constructor(trade: RouterTrade<Currency, Currency, TradeType>, options: SwapOptions);
|
|
13
14
|
encode(planner: RoutePlanner, _config: TradeConfig): void;
|
|
14
15
|
}
|
|
@@ -2,7 +2,7 @@ import { Interface } from '@ethersproject/abi';
|
|
|
2
2
|
import { BuyItem, NFTTrade, TokenType } from '../NFTTrade';
|
|
3
3
|
import { TradeConfig } from '../Command';
|
|
4
4
|
import { RoutePlanner } from '../../utils/routerCommands';
|
|
5
|
-
import { BigNumberish } from 'ethers';
|
|
5
|
+
import { BigNumber, BigNumberish } from 'ethers';
|
|
6
6
|
declare type X2Y2PartialData = {
|
|
7
7
|
signedInput: string;
|
|
8
8
|
recipient: string;
|
|
@@ -23,6 +23,6 @@ export declare class X2Y2Trade extends NFTTrade<X2Y2Data> {
|
|
|
23
23
|
constructor(orders: X2Y2Data[]);
|
|
24
24
|
encode(planner: RoutePlanner, config: TradeConfig): void;
|
|
25
25
|
getBuyItems(): BuyItem[];
|
|
26
|
-
getTotalPrice():
|
|
26
|
+
getTotalPrice(): BigNumber;
|
|
27
27
|
}
|
|
28
28
|
export {};
|
package/dist/index.d.ts
CHANGED
package/dist/swapRouter.d.ts
CHANGED
|
@@ -3,20 +3,25 @@ import { BigNumberish } from 'ethers';
|
|
|
3
3
|
import { MethodParameters } from '@uniswap/v3-sdk';
|
|
4
4
|
import { Trade as RouterTrade } from '@uniswap/router-sdk';
|
|
5
5
|
import { Currency, TradeType } from '@uniswap/sdk-core';
|
|
6
|
+
import { Command } from './entities/Command';
|
|
6
7
|
import { NFTTrade, SupportedProtocolsData } from './entities/NFTTrade';
|
|
7
8
|
import { SwapOptions } from './entities/protocols/uniswap';
|
|
8
9
|
export declare type SwapRouterConfig = {
|
|
9
10
|
sender?: string;
|
|
10
11
|
deadline?: BigNumberish;
|
|
11
12
|
};
|
|
13
|
+
declare type SupportedNFTTrade = NFTTrade<SupportedProtocolsData>;
|
|
12
14
|
export declare abstract class SwapRouter {
|
|
13
15
|
static INTERFACE: Interface;
|
|
16
|
+
static swapCallParameters(trades: Command[] | Command, config?: SwapRouterConfig): MethodParameters;
|
|
14
17
|
/**
|
|
18
|
+
* @deprecated in favor of swapCallParameters. Update before next major version 2.0.0
|
|
15
19
|
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given swap.
|
|
16
20
|
* @param trades to produce call parameters for
|
|
17
21
|
*/
|
|
18
|
-
static swapNFTCallParameters(trades:
|
|
22
|
+
static swapNFTCallParameters(trades: SupportedNFTTrade[], config?: SwapRouterConfig): MethodParameters;
|
|
19
23
|
/**
|
|
24
|
+
* @deprecated in favor of swapCallParameters. Update before next major version 2.0.0
|
|
20
25
|
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
21
26
|
* @param trades to produce call parameters for
|
|
22
27
|
* @param options options for the call parameters
|
|
@@ -30,3 +35,4 @@ export declare abstract class SwapRouter {
|
|
|
30
35
|
*/
|
|
31
36
|
private static encodePlan;
|
|
32
37
|
}
|
|
38
|
+
export {};
|
|
@@ -8,12 +8,12 @@ var invariant = _interopDefault(require('tiny-invariant'));
|
|
|
8
8
|
var UniversalRouter_json = require('@uniswap/universal-router/artifacts/contracts/UniversalRouter.sol/UniversalRouter.json');
|
|
9
9
|
var abi$7 = require('@ethersproject/abi');
|
|
10
10
|
var ethers = require('ethers');
|
|
11
|
-
var sdkCore = require('@uniswap/sdk-core');
|
|
12
11
|
var JSBI = _interopDefault(require('jsbi'));
|
|
13
12
|
var utils = require('ethers/lib/utils');
|
|
14
13
|
var v2Sdk = require('@uniswap/v2-sdk');
|
|
15
14
|
var v3Sdk = require('@uniswap/v3-sdk');
|
|
16
15
|
var routerSdk = require('@uniswap/router-sdk');
|
|
16
|
+
var sdkCore = require('@uniswap/sdk-core');
|
|
17
17
|
|
|
18
18
|
function _inheritsLoose(subClass, superClass) {
|
|
19
19
|
subClass.prototype = Object.create(superClass.prototype);
|
|
@@ -59,6 +59,11 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
|
59
59
|
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
(function (RouterTradeType) {
|
|
63
|
+
RouterTradeType["UniswapTrade"] = "UniswapTrade";
|
|
64
|
+
RouterTradeType["NFTTrade"] = "NFTTrade";
|
|
65
|
+
})(exports.RouterTradeType || (exports.RouterTradeType = {}));
|
|
66
|
+
|
|
62
67
|
var _ABI_DEFINITION;
|
|
63
68
|
/**
|
|
64
69
|
* CommandTypes
|
|
@@ -162,8 +167,8 @@ var UNIVERSAL_ROUTER_ADDRESS = function UNIVERSAL_ROUTER_ADDRESS(chainId) {
|
|
|
162
167
|
var PERMIT2_ADDRESS = '0x000000000022D473030F116dDEE9F6B43aC78BA3';
|
|
163
168
|
var CONTRACT_BALANCE = /*#__PURE__*/ethers.BigNumber.from(2).pow(255);
|
|
164
169
|
var ETH_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
165
|
-
var
|
|
166
|
-
var
|
|
170
|
+
var SENDER_AS_RECIPIENT = '0x0000000000000000000000000000000000000001';
|
|
171
|
+
var ROUTER_AS_RECIPIENT = '0x0000000000000000000000000000000000000002';
|
|
167
172
|
|
|
168
173
|
var REFUND_ETH_PRICE_IMPACT_THRESHOLD = /*#__PURE__*/new sdkCore.Percent( /*#__PURE__*/JSBI.BigInt(50), /*#__PURE__*/JSBI.BigInt(100));
|
|
169
174
|
// Wrapper for uniswap router-sdk trade entity to encode swaps for Universal Router
|
|
@@ -172,6 +177,7 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
172
177
|
function UniswapTrade(trade, options) {
|
|
173
178
|
this.trade = trade;
|
|
174
179
|
this.options = options;
|
|
180
|
+
this.tradeType = exports.RouterTradeType.UniswapTrade;
|
|
175
181
|
}
|
|
176
182
|
var _proto = UniswapTrade.prototype;
|
|
177
183
|
_proto.encode = function encode(planner, _config) {
|
|
@@ -179,11 +185,11 @@ var UniswapTrade = /*#__PURE__*/function () {
|
|
|
179
185
|
var payerIsUser = true;
|
|
180
186
|
if (this.trade.inputAmount.currency.isNative) {
|
|
181
187
|
// TODO: optimize if only one v2 pool we can directly send this to the pool
|
|
182
|
-
planner.addCommand(CommandType.WRAP_ETH, [
|
|
188
|
+
planner.addCommand(CommandType.WRAP_ETH, [ROUTER_AS_RECIPIENT, this.trade.maximumAmountIn(this.options.slippageTolerance).quotient.toString()]);
|
|
183
189
|
// since WETH is now owned by the router, the router pays for inputs
|
|
184
190
|
payerIsUser = false;
|
|
185
191
|
}
|
|
186
|
-
this.options.recipient = (_this$options$recipie = this.options.recipient) != null ? _this$options$recipie :
|
|
192
|
+
this.options.recipient = (_this$options$recipie = this.options.recipient) != null ? _this$options$recipie : SENDER_AS_RECIPIENT;
|
|
187
193
|
// flag for whether we want to perform slippage check on aggregate output of multiple routes
|
|
188
194
|
// 1. when there are >2 exact input trades. this is only a heuristic,
|
|
189
195
|
// as it's still more gas-expensive even in this case, but has benefits
|
|
@@ -232,11 +238,11 @@ function addV2Swap(planner, _ref, tradeType, options, payerIsUser, routerMustCus
|
|
|
232
238
|
if (tradeType == sdkCore.TradeType.EXACT_INPUT) {
|
|
233
239
|
planner.addCommand(CommandType.V2_SWAP_EXACT_IN, [
|
|
234
240
|
// if native, we have to unwrap so keep in the router for now
|
|
235
|
-
routerMustCustody ?
|
|
241
|
+
routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient, trade.maximumAmountIn(options.slippageTolerance).quotient.toString(), trade.minimumAmountOut(options.slippageTolerance).quotient.toString(), route.path.map(function (pool) {
|
|
236
242
|
return pool.address;
|
|
237
243
|
}), payerIsUser]);
|
|
238
244
|
} else if (tradeType == sdkCore.TradeType.EXACT_OUTPUT) {
|
|
239
|
-
planner.addCommand(CommandType.V2_SWAP_EXACT_OUT, [routerMustCustody ?
|
|
245
|
+
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(function (pool) {
|
|
240
246
|
return pool.address;
|
|
241
247
|
}), payerIsUser]);
|
|
242
248
|
}
|
|
@@ -254,9 +260,9 @@ function addV3Swap(planner, _ref2, tradeType, options, payerIsUser, routerMustCu
|
|
|
254
260
|
});
|
|
255
261
|
var path = v3Sdk.encodeRouteToPath(route, trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT);
|
|
256
262
|
if (tradeType == sdkCore.TradeType.EXACT_INPUT) {
|
|
257
|
-
planner.addCommand(CommandType.V3_SWAP_EXACT_IN, [routerMustCustody ?
|
|
263
|
+
planner.addCommand(CommandType.V3_SWAP_EXACT_IN, [routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient, trade.maximumAmountIn(options.slippageTolerance).quotient.toString(), trade.minimumAmountOut(options.slippageTolerance).quotient.toString(), path, payerIsUser]);
|
|
258
264
|
} else if (tradeType == sdkCore.TradeType.EXACT_OUTPUT) {
|
|
259
|
-
planner.addCommand(CommandType.V3_SWAP_EXACT_OUT, [routerMustCustody ?
|
|
265
|
+
planner.addCommand(CommandType.V3_SWAP_EXACT_OUT, [routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient, trade.minimumAmountOut(options.slippageTolerance).quotient.toString(), trade.maximumAmountIn(options.slippageTolerance).quotient.toString(), path, payerIsUser]);
|
|
260
266
|
}
|
|
261
267
|
}
|
|
262
268
|
// encode a mixed route swap, i.e. including both v2 and v3 pools
|
|
@@ -264,7 +270,7 @@ function addMixedSwap(planner, swap, tradeType, options, payerIsUser, routerMust
|
|
|
264
270
|
var route = swap.route,
|
|
265
271
|
inputAmount = swap.inputAmount,
|
|
266
272
|
outputAmount = swap.outputAmount;
|
|
267
|
-
var tradeRecipient = routerMustCustody ?
|
|
273
|
+
var tradeRecipient = routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient;
|
|
268
274
|
// single hop, so it can be reduced to plain v2 or v3 swap logic
|
|
269
275
|
if (route.pools.length === 1) {
|
|
270
276
|
if (route.pools[0] instanceof v3Sdk.Pool) {
|
|
@@ -311,7 +317,7 @@ function addMixedSwap(planner, swap, tradeType, options, payerIsUser, routerMust
|
|
|
311
317
|
// note: because of the partitioning function we can be sure that the next section is v2
|
|
312
318
|
isLastSectionInRoute(i) ? tradeRecipient : sections[i + 1][0].liquidityToken.address, i == 0 ? amountIn : CONTRACT_BALANCE, !isLastSectionInRoute(i) ? 0 : amountOut, path, payerIsUser && i === 0]);
|
|
313
319
|
} else {
|
|
314
|
-
planner.addCommand(CommandType.V2_SWAP_EXACT_IN, [isLastSectionInRoute(i) ? tradeRecipient :
|
|
320
|
+
planner.addCommand(CommandType.V2_SWAP_EXACT_IN, [isLastSectionInRoute(i) ? tradeRecipient : ROUTER_AS_RECIPIENT, i === 0 ? amountIn : CONTRACT_BALANCE, !isLastSectionInRoute(i) ? 0 : amountOut, newRoute.path.map(function (pool) {
|
|
315
321
|
return pool.address;
|
|
316
322
|
}), payerIsUser && i === 0]);
|
|
317
323
|
}
|
|
@@ -328,37 +334,99 @@ function encodePermit(planner, permit) {
|
|
|
328
334
|
|
|
329
335
|
var SwapRouter = /*#__PURE__*/function () {
|
|
330
336
|
function SwapRouter() {}
|
|
337
|
+
SwapRouter.swapCallParameters = function swapCallParameters(trades, config) {
|
|
338
|
+
if (config === void 0) {
|
|
339
|
+
config = {};
|
|
340
|
+
}
|
|
341
|
+
if (!Array.isArray(trades)) trades = [trades];
|
|
342
|
+
var nftTrades = trades.filter(function (trade, _, _ref) {
|
|
343
|
+
return trade.hasOwnProperty('market');
|
|
344
|
+
});
|
|
345
|
+
var allowRevert = nftTrades.length == 1 && nftTrades[0].orders.length == 1 ? false : true;
|
|
346
|
+
var planner = new RoutePlanner();
|
|
347
|
+
// track value flow to require the right amount of native value
|
|
348
|
+
var currentNativeValueInRouter = ethers.BigNumber.from(0);
|
|
349
|
+
var transactionValue = ethers.BigNumber.from(0);
|
|
350
|
+
for (var _iterator = _createForOfIteratorHelperLoose(trades), _step; !(_step = _iterator()).done;) {
|
|
351
|
+
var trade = _step.value;
|
|
352
|
+
// is NFTTrade
|
|
353
|
+
if (trade.tradeType == exports.RouterTradeType.NFTTrade) {
|
|
354
|
+
var nftTrade = trade;
|
|
355
|
+
nftTrade.encode(planner, {
|
|
356
|
+
allowRevert: allowRevert
|
|
357
|
+
});
|
|
358
|
+
var tradePrice = nftTrade.getTotalPrice();
|
|
359
|
+
// send enough native value to contract for NFT purchase
|
|
360
|
+
if (currentNativeValueInRouter.lt(tradePrice)) {
|
|
361
|
+
transactionValue = transactionValue.add(tradePrice.sub(currentNativeValueInRouter));
|
|
362
|
+
currentNativeValueInRouter = ethers.BigNumber.from(0);
|
|
363
|
+
} else {
|
|
364
|
+
currentNativeValueInRouter = currentNativeValueInRouter.sub(tradePrice);
|
|
365
|
+
}
|
|
366
|
+
// is UniswapTrade
|
|
367
|
+
} else if (trade.tradeType == exports.RouterTradeType.UniswapTrade) {
|
|
368
|
+
var uniswapTrade = trade;
|
|
369
|
+
var inputIsNative = uniswapTrade.trade.inputAmount.currency.isNative;
|
|
370
|
+
var outputIsNative = uniswapTrade.trade.outputAmount.currency.isNative;
|
|
371
|
+
var swapOptions = uniswapTrade.options;
|
|
372
|
+
!!(inputIsNative && !!swapOptions.inputTokenPermit) ? invariant(false, 'NATIVE_INPUT_PERMIT') : void 0;
|
|
373
|
+
if (!!swapOptions.inputTokenPermit) {
|
|
374
|
+
encodePermit(planner, swapOptions.inputTokenPermit);
|
|
375
|
+
}
|
|
376
|
+
if (inputIsNative) {
|
|
377
|
+
transactionValue = transactionValue.add(ethers.BigNumber.from(uniswapTrade.trade.maximumAmountIn(swapOptions.slippageTolerance).quotient.toString()));
|
|
378
|
+
}
|
|
379
|
+
// track amount of native currency in the router
|
|
380
|
+
if (outputIsNative && swapOptions.recipient == ROUTER_AS_RECIPIENT) {
|
|
381
|
+
currentNativeValueInRouter = currentNativeValueInRouter.add(ethers.BigNumber.from(uniswapTrade.trade.minimumAmountOut(swapOptions.slippageTolerance).quotient.toString()));
|
|
382
|
+
}
|
|
383
|
+
uniswapTrade.encode(planner, {
|
|
384
|
+
allowRevert: false
|
|
385
|
+
});
|
|
386
|
+
} else {
|
|
387
|
+
throw 'trade must be of instance: UniswapTrade or NFTTrade';
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// TODO: matches current logic for now, but should eventually only sweep for multiple NFT trades
|
|
391
|
+
// or NFT trades with potential slippage (i.e. sudo)
|
|
392
|
+
if (nftTrades.length > 0) planner.addCommand(CommandType.SWEEP, [ETH_ADDRESS, SENDER_AS_RECIPIENT, 0]);
|
|
393
|
+
return SwapRouter.encodePlan(planner, transactionValue, config);
|
|
394
|
+
}
|
|
331
395
|
/**
|
|
396
|
+
* @deprecated in favor of swapCallParameters. Update before next major version 2.0.0
|
|
332
397
|
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given swap.
|
|
333
398
|
* @param trades to produce call parameters for
|
|
334
|
-
|
|
399
|
+
*/;
|
|
335
400
|
SwapRouter.swapNFTCallParameters = function swapNFTCallParameters(trades, config) {
|
|
336
|
-
|
|
401
|
+
if (config === void 0) {
|
|
402
|
+
config = {};
|
|
403
|
+
}
|
|
337
404
|
var planner = new RoutePlanner();
|
|
338
405
|
var totalPrice = ethers.BigNumber.from(0);
|
|
339
406
|
var allowRevert = trades.length == 1 && trades[0].orders.length == 1 ? false : true;
|
|
340
|
-
for (var
|
|
341
|
-
var trade =
|
|
407
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(trades), _step2; !(_step2 = _iterator2()).done;) {
|
|
408
|
+
var trade = _step2.value;
|
|
342
409
|
trade.encode(planner, {
|
|
343
410
|
allowRevert: allowRevert
|
|
344
411
|
});
|
|
345
412
|
totalPrice = totalPrice.add(trade.getTotalPrice());
|
|
346
413
|
}
|
|
347
|
-
planner.addCommand(CommandType.SWEEP, [ETH_ADDRESS,
|
|
414
|
+
planner.addCommand(CommandType.SWEEP, [ETH_ADDRESS, SENDER_AS_RECIPIENT, 0]);
|
|
348
415
|
return SwapRouter.encodePlan(planner, totalPrice, config);
|
|
349
416
|
}
|
|
350
417
|
/**
|
|
418
|
+
* @deprecated in favor of swapCallParameters. Update before next major version 2.0.0
|
|
351
419
|
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
352
420
|
* @param trades to produce call parameters for
|
|
353
421
|
* @param options options for the call parameters
|
|
354
422
|
*/;
|
|
355
423
|
SwapRouter.swapERC20CallParameters = function swapERC20CallParameters(trades, options) {
|
|
356
|
-
// TODO: use permit if signature included in
|
|
424
|
+
// TODO: use permit if signature included in swapOptions
|
|
357
425
|
var planner = new RoutePlanner();
|
|
358
426
|
var trade = new UniswapTrade(trades, options);
|
|
359
427
|
var inputCurrency = trade.trade.inputAmount.currency;
|
|
360
428
|
!!(inputCurrency.isNative && !!options.inputTokenPermit) ? invariant(false, 'NATIVE_INPUT_PERMIT') : void 0;
|
|
361
|
-
if (options.inputTokenPermit
|
|
429
|
+
if (options.inputTokenPermit) {
|
|
362
430
|
encodePermit(planner, options.inputTokenPermit);
|
|
363
431
|
}
|
|
364
432
|
var nativeCurrencyValue = inputCurrency.isNative ? ethers.BigNumber.from(trade.trade.maximumAmountIn(options.slippageTolerance).quotient.toString()) : ethers.BigNumber.from(0);
|
|
@@ -386,7 +454,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
386
454
|
var calldata = SwapRouter.INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
387
455
|
return {
|
|
388
456
|
calldata: calldata,
|
|
389
|
-
value: nativeCurrencyValue.
|
|
457
|
+
value: nativeCurrencyValue.toHexString()
|
|
390
458
|
};
|
|
391
459
|
};
|
|
392
460
|
return SwapRouter;
|
|
@@ -394,6 +462,7 @@ var SwapRouter = /*#__PURE__*/function () {
|
|
|
394
462
|
SwapRouter.INTERFACE = /*#__PURE__*/new abi$7.Interface(UniversalRouter_json.abi);
|
|
395
463
|
|
|
396
464
|
var NFTTrade = function NFTTrade(market, orders) {
|
|
465
|
+
this.tradeType = exports.RouterTradeType.NFTTrade;
|
|
397
466
|
!(orders.length > 0) ? invariant(false, 'no buy Items') : void 0;
|
|
398
467
|
this.market = market;
|
|
399
468
|
this.orders = orders;
|
|
@@ -9287,6 +9356,7 @@ exports.NFT20Trade = NFT20Trade;
|
|
|
9287
9356
|
exports.NFTTrade = NFTTrade;
|
|
9288
9357
|
exports.NFTXTrade = NFTXTrade;
|
|
9289
9358
|
exports.PERMIT2_ADDRESS = PERMIT2_ADDRESS;
|
|
9359
|
+
exports.ROUTER_AS_RECIPIENT = ROUTER_AS_RECIPIENT;
|
|
9290
9360
|
exports.SeaportTrade = SeaportTrade;
|
|
9291
9361
|
exports.SudoswapTrade = SudoswapTrade;
|
|
9292
9362
|
exports.SwapRouter = SwapRouter;
|