forked from paraswap/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniswap-v3.ts
242 lines (222 loc) · 6.22 KB
/
uniswap-v3.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { Interface, JsonFragment } from '@ethersproject/abi';
import { pack } from '@ethersproject/solidity';
import { Network, SwapSide } from '../constants';
import {
AdapterExchangeParam,
Address,
DexExchangeParam,
SimpleExchangeParam,
} from '../types';
import { IDexTxBuilder } from './idex';
import {
getLocalDeadlineAsFriendlyPlaceholder,
SimpleExchange,
} from './simple-exchange';
import UniswapV3RouterABI from '../abi/UniswapV3Router.json';
import { NumberAsString } from '@paraswap/core';
import { IDexHelper } from '../dex-helper';
import { extractReturnAmountPosition } from '../executor/utils';
const UNISWAP_V3_ROUTER_ADDRESSES: { [network: number]: Address } = {
[Network.MAINNET]: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
[Network.POLYGON]: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
[Network.ARBITRUM]: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
[Network.OPTIMISM]: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
};
export type UniswapV3Data = {
// ExactInputSingleParams
path: {
tokenIn: Address;
tokenOut: Address;
fee: number;
}[];
};
type UniswapV3SellParam = {
path: string;
recipient: Address;
deadline: string;
amountIn: NumberAsString;
amountOutMinimum: NumberAsString;
};
type UniswapV3BuyParam = {
path: string;
recipient: Address;
deadline: string;
amountOut: NumberAsString;
amountInMaximum: NumberAsString;
};
export type UniswapV3Param = UniswapV3SellParam | UniswapV3BuyParam;
enum UniswapV3Functions {
exactInput = 'exactInput',
exactOutput = 'exactOutput',
}
export class UniswapV3
extends SimpleExchange
implements IDexTxBuilder<UniswapV3Data, UniswapV3Param>
{
static dexKeys = ['uniswapv3'];
exchangeRouterInterface: Interface;
needWrapNative = true;
protected routerAddress: string;
constructor(dexHelper: IDexHelper, dexKey: string, routerAddress?: Address) {
super(dexHelper, dexKey);
this.exchangeRouterInterface = new Interface(
UniswapV3RouterABI as JsonFragment[],
);
this.routerAddress =
routerAddress || UNISWAP_V3_ROUTER_ADDRESSES[this.network];
}
protected encodePath(
path: {
tokenIn: Address;
tokenOut: Address;
fee: number;
}[],
side: SwapSide,
): string {
if (path.length === 0) {
return '0x';
}
const { _path, types } = path.reduce(
(
{ _path, types }: { _path: string[]; types: string[] },
curr,
index,
): { _path: string[]; types: string[] } => {
if (index === 0) {
return {
types: ['address', 'uint24', 'address'],
_path: [curr.tokenIn, curr.fee.toString(), curr.tokenOut],
};
} else {
return {
types: [...types, 'uint24', 'address'],
_path: [..._path, curr.fee.toString(), curr.tokenOut],
};
}
},
{ _path: [], types: [] },
);
return side === SwapSide.BUY
? pack(types.reverse(), _path.reverse())
: pack(types, _path);
}
getAdapterParam(
srcToken: string,
destToken: string,
srcAmount: string,
destAmount: string,
data: UniswapV3Data,
side: SwapSide,
): AdapterExchangeParam {
const { path: rawPath } = data;
const path = this.encodePath(rawPath, side);
const payload = this.abiCoder.encodeParameter(
{
ParentStruct: {
path: 'bytes',
deadline: 'uint256',
},
},
{
path,
deadline: getLocalDeadlineAsFriendlyPlaceholder(), // FIXME: more gas efficient to pass block.timestamp in adapter
},
);
return {
targetExchange: this.routerAddress,
payload,
networkFee: '0',
};
}
async getSimpleParam(
srcToken: string,
destToken: string,
srcAmount: string,
destAmount: string,
data: UniswapV3Data,
side: SwapSide,
): Promise<SimpleExchangeParam> {
const swapFunction =
side === SwapSide.SELL
? UniswapV3Functions.exactInput
: UniswapV3Functions.exactOutput;
const path = this.encodePath(data.path, side);
const swapFunctionParams: UniswapV3Param =
side === SwapSide.SELL
? {
recipient: this.augustusAddress,
deadline: getLocalDeadlineAsFriendlyPlaceholder(),
amountIn: srcAmount,
amountOutMinimum: destAmount,
path,
}
: {
recipient: this.augustusAddress,
deadline: getLocalDeadlineAsFriendlyPlaceholder(),
amountOut: destAmount,
amountInMaximum: srcAmount,
path,
};
const swapData = this.exchangeRouterInterface.encodeFunctionData(
swapFunction,
[swapFunctionParams],
);
return this.buildSimpleParamWithoutWETHConversion(
srcToken,
srcAmount,
destToken,
destAmount,
swapData,
this.routerAddress,
);
}
getDexParam(
srcToken: Address,
destToken: Address,
srcAmount: NumberAsString,
destAmount: NumberAsString,
recipient: Address,
data: UniswapV3Data,
side: SwapSide,
): DexExchangeParam {
const swapFunction =
side === SwapSide.SELL
? UniswapV3Functions.exactInput
: UniswapV3Functions.exactOutput;
const path = this.encodePath(data.path, side);
const swapFunctionParams: UniswapV3Param =
side === SwapSide.SELL
? {
recipient,
deadline: getLocalDeadlineAsFriendlyPlaceholder(),
amountIn: srcAmount,
amountOutMinimum: destAmount,
path,
}
: {
recipient,
deadline: getLocalDeadlineAsFriendlyPlaceholder(),
amountOut: destAmount,
amountInMaximum: srcAmount,
path,
};
const exchangeData = this.exchangeRouterInterface.encodeFunctionData(
swapFunction,
[swapFunctionParams],
);
return {
needWrapNative: this.needWrapNative,
dexFuncHasRecipient: true,
exchangeData,
targetExchange: this.routerAddress,
returnAmountPos:
side === SwapSide.SELL
? extractReturnAmountPosition(
this.exchangeRouterInterface,
swapFunction,
'amountOut',
)
: undefined,
};
}
}