This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 434
/
swapRouter.ts
212 lines (182 loc) · 7.27 KB
/
swapRouter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Interface } from '@ethersproject/abi'
import { BigintIsh, Currency, CurrencyAmount, Percent, TradeType, validateAndParseAddress } from '@uniswap/sdk-core'
import invariant from 'tiny-invariant'
import { Trade } from './entities/trade'
import { ADDRESS_ZERO } from './constants'
import { PermitOptions, SelfPermit } from './selfPermit'
import { encodeRouteToPath } from './utils'
import { MethodParameters, toHex } from './utils/calldata'
import ISwapRouter from '@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json'
import { Multicall } from './multicall'
import { FeeOptions, Payments } from './payments'
/**
* Options for producing the arguments to send calls to the router.
*/
export interface SwapOptions {
/**
* How much the execution price is allowed to move unfavorably from the trade execution price.
*/
slippageTolerance: Percent
/**
* The account that should receive the output.
*/
recipient: string
/**
* When the transaction expires, in epoch seconds.
*/
deadline: BigintIsh
/**
* The optional permit parameters for spending the input.
*/
inputTokenPermit?: PermitOptions
/**
* The optional price limit for the trade.
*/
sqrtPriceLimitX96?: BigintIsh
/**
* Optional information for taking a fee on output.
*/
fee?: FeeOptions
}
/**
* Represents the Uniswap V3 SwapRouter, and has static methods for helping execute trades.
*/
export abstract class SwapRouter {
public static INTERFACE: Interface = new Interface(ISwapRouter.abi)
/**
* Cannot be constructed.
*/
private constructor() {}
/**
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
* @param trade to produce call parameters for
* @param options options for the call parameters
*/
public static swapCallParameters(
trades: Trade<Currency, Currency, TradeType> | Trade<Currency, Currency, TradeType>[],
options: SwapOptions
): MethodParameters {
if (!Array.isArray(trades)) {
trades = [trades]
}
const sampleTrade = trades[0]
const tokenIn = sampleTrade.inputAmount.currency.wrapped
const tokenOut = sampleTrade.outputAmount.currency.wrapped
// All trades should have the same starting and ending token.
invariant(
trades.every(trade => trade.inputAmount.currency.wrapped.equals(tokenIn)),
'TOKEN_IN_DIFF'
)
invariant(
trades.every(trade => trade.outputAmount.currency.wrapped.equals(tokenOut)),
'TOKEN_OUT_DIFF'
)
const calldatas: string[] = []
const ZERO_IN: CurrencyAmount<Currency> = CurrencyAmount.fromRawAmount(trades[0].inputAmount.currency, 0)
const ZERO_OUT: CurrencyAmount<Currency> = CurrencyAmount.fromRawAmount(trades[0].outputAmount.currency, 0)
const totalAmountOut: CurrencyAmount<Currency> = trades.reduce(
(sum, trade) => sum.add(trade.minimumAmountOut(options.slippageTolerance)),
ZERO_OUT
)
// flag for whether a refund needs to happen
const mustRefund = sampleTrade.inputAmount.currency.isNative && sampleTrade.tradeType === TradeType.EXACT_OUTPUT
const inputIsNative = sampleTrade.inputAmount.currency.isNative
// flags for whether funds should be send first to the router
const outputIsNative = sampleTrade.outputAmount.currency.isNative
const routerMustCustody = outputIsNative || !!options.fee
const totalValue: CurrencyAmount<Currency> = inputIsNative
? trades.reduce((sum, trade) => sum.add(trade.maximumAmountIn(options.slippageTolerance)), ZERO_IN)
: ZERO_IN
// encode permit if necessary
if (options.inputTokenPermit) {
invariant(sampleTrade.inputAmount.currency.isToken, 'NON_TOKEN_PERMIT')
calldatas.push(SelfPermit.encodePermit(sampleTrade.inputAmount.currency, options.inputTokenPermit))
}
const recipient: string = validateAndParseAddress(options.recipient)
const deadline = toHex(options.deadline)
for (const trade of trades) {
for (const { route, inputAmount, outputAmount } of trade.swaps) {
const amountIn: string = toHex(trade.maximumAmountIn(options.slippageTolerance, inputAmount).quotient)
const amountOut: string = toHex(trade.minimumAmountOut(options.slippageTolerance, outputAmount).quotient)
// flag for whether the trade is single hop or not
const singleHop = route.pools.length === 1
if (singleHop) {
if (trade.tradeType === TradeType.EXACT_INPUT) {
const exactInputSingleParams = {
tokenIn: route.tokenPath[0].address,
tokenOut: route.tokenPath[1].address,
fee: route.pools[0].fee,
recipient: routerMustCustody ? ADDRESS_ZERO : recipient,
deadline,
amountIn,
amountOutMinimum: amountOut,
sqrtPriceLimitX96: toHex(options.sqrtPriceLimitX96 ?? 0)
}
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInputSingle', [exactInputSingleParams]))
} else {
const exactOutputSingleParams = {
tokenIn: route.tokenPath[0].address,
tokenOut: route.tokenPath[1].address,
fee: route.pools[0].fee,
recipient: routerMustCustody ? ADDRESS_ZERO : recipient,
deadline,
amountOut,
amountInMaximum: amountIn,
sqrtPriceLimitX96: toHex(options.sqrtPriceLimitX96 ?? 0)
}
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutputSingle', [exactOutputSingleParams]))
}
} else {
invariant(options.sqrtPriceLimitX96 === undefined, 'MULTIHOP_PRICE_LIMIT')
const path: string = encodeRouteToPath(route, trade.tradeType === TradeType.EXACT_OUTPUT)
if (trade.tradeType === TradeType.EXACT_INPUT) {
const exactInputParams = {
path,
recipient: routerMustCustody ? ADDRESS_ZERO : recipient,
deadline,
amountIn,
amountOutMinimum: amountOut
}
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactInput', [exactInputParams]))
} else {
const exactOutputParams = {
path,
recipient: routerMustCustody ? ADDRESS_ZERO : recipient,
deadline,
amountOut,
amountInMaximum: amountIn
}
calldatas.push(SwapRouter.INTERFACE.encodeFunctionData('exactOutput', [exactOutputParams]))
}
}
}
}
// unwrap
if (routerMustCustody) {
if (!!options.fee) {
if (outputIsNative) {
calldatas.push(Payments.encodeUnwrapWETH9(totalAmountOut.quotient, recipient, options.fee))
} else {
calldatas.push(
Payments.encodeSweepToken(
sampleTrade.outputAmount.currency.wrapped,
totalAmountOut.quotient,
recipient,
options.fee
)
)
}
} else {
calldatas.push(Payments.encodeUnwrapWETH9(totalAmountOut.quotient, recipient))
}
}
// refund
if (mustRefund) {
calldatas.push(Payments.encodeRefundETH())
}
return {
calldata: Multicall.encodeMulticall(calldatas),
value: toHex(totalValue.quotient)
}
}
}