@uniswap/universal-router-sdk 4.1.1 → 4.3.0

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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { SwapRouter } from './swapRouter';
1
+ export { SwapRouter, MigrateV3ToV4Options } from './swapRouter';
2
2
  export * from './entities';
3
3
  export * from './utils/routerTradeAdapter';
4
4
  export { RoutePlanner, CommandType } from './utils/routerCommands';
@@ -80,7 +80,7 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
80
80
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
81
81
  }
82
82
 
83
- var _COMMAND_ABI_DEFINITI;
83
+ var _COMMAND_DEFINITION;
84
84
  (function (CommandType) {
85
85
  CommandType[CommandType["V3_SWAP_EXACT_IN"] = 0] = "V3_SWAP_EXACT_IN";
86
86
  CommandType[CommandType["V3_SWAP_EXACT_OUT"] = 1] = "V3_SWAP_EXACT_OUT";
@@ -107,165 +107,212 @@ var Subparser;
107
107
  Subparser[Subparser["V3PathExactIn"] = 0] = "V3PathExactIn";
108
108
  Subparser[Subparser["V3PathExactOut"] = 1] = "V3PathExactOut";
109
109
  })(Subparser || (Subparser = {}));
110
+ var Parser;
111
+ (function (Parser) {
112
+ Parser[Parser["Abi"] = 0] = "Abi";
113
+ Parser[Parser["V4Actions"] = 1] = "V4Actions";
114
+ Parser[Parser["V3Actions"] = 2] = "V3Actions";
115
+ })(Parser || (Parser = {}));
110
116
  var ALLOW_REVERT_FLAG = 0x80;
111
117
  var REVERTIBLE_COMMANDS = /*#__PURE__*/new Set([exports.CommandType.EXECUTE_SUB_PLAN]);
112
118
  var PERMIT_STRUCT = '((address token,uint160 amount,uint48 expiration,uint48 nonce) details,address spender,uint256 sigDeadline)';
113
119
  var PERMIT_BATCH_STRUCT = '((address token,uint160 amount,uint48 expiration,uint48 nonce)[] details,address spender,uint256 sigDeadline)';
114
120
  var PERMIT2_TRANSFER_FROM_STRUCT = '(address from,address to,uint160 amount,address token)';
115
121
  var PERMIT2_TRANSFER_FROM_BATCH_STRUCT = PERMIT2_TRANSFER_FROM_STRUCT + '[]';
116
- var COMMAND_ABI_DEFINITION = (_COMMAND_ABI_DEFINITI = {}, _COMMAND_ABI_DEFINITI[exports.CommandType.EXECUTE_SUB_PLAN] = [{
117
- name: 'commands',
118
- type: 'bytes'
119
- }, {
120
- name: 'inputs',
121
- type: 'bytes[]'
122
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.PERMIT2_PERMIT] = [{
123
- name: 'permit',
124
- type: PERMIT_STRUCT
125
- }, {
126
- name: 'signature',
127
- type: 'bytes'
128
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.PERMIT2_PERMIT_BATCH] = [{
129
- name: 'permit',
130
- type: PERMIT_BATCH_STRUCT
131
- }, {
132
- name: 'signature',
133
- type: 'bytes'
134
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.PERMIT2_TRANSFER_FROM] = [{
135
- name: 'token',
136
- type: 'address'
137
- }, {
138
- name: 'recipient',
139
- type: 'address'
140
- }, {
141
- name: 'amount',
142
- type: 'uint160'
143
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.PERMIT2_TRANSFER_FROM_BATCH] = [{
144
- name: 'transferFrom',
145
- type: PERMIT2_TRANSFER_FROM_BATCH_STRUCT
146
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.V3_SWAP_EXACT_IN] = [{
147
- name: 'recipient',
148
- type: 'address'
149
- }, {
150
- name: 'amountIn',
151
- type: 'uint256'
152
- }, {
153
- name: 'amountOutMin',
154
- type: 'uint256'
155
- }, {
156
- name: 'path',
157
- subparser: Subparser.V3PathExactIn,
158
- type: 'bytes'
159
- }, {
160
- name: 'payerIsUser',
161
- type: 'bool'
162
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.V3_SWAP_EXACT_OUT] = [{
163
- name: 'recipient',
164
- type: 'address'
165
- }, {
166
- name: 'amountOut',
167
- type: 'uint256'
168
- }, {
169
- name: 'amountInMax',
170
- type: 'uint256'
171
- }, {
172
- name: 'path',
173
- subparser: Subparser.V3PathExactOut,
174
- type: 'bytes'
175
- }, {
176
- name: 'payerIsUser',
177
- type: 'bool'
178
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.V2_SWAP_EXACT_IN] = [{
179
- name: 'recipient',
180
- type: 'address'
181
- }, {
182
- name: 'amountIn',
183
- type: 'uint256'
184
- }, {
185
- name: 'amountOutMin',
186
- type: 'uint256'
187
- }, {
188
- name: 'path',
189
- type: 'address[]'
190
- }, {
191
- name: 'payerIsUser',
192
- type: 'bool'
193
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.V2_SWAP_EXACT_OUT] = [{
194
- name: 'recipient',
195
- type: 'address'
196
- }, {
197
- name: 'amountOut',
198
- type: 'uint256'
199
- }, {
200
- name: 'amountInMax',
201
- type: 'uint256'
202
- }, {
203
- name: 'path',
204
- type: 'address[]'
205
- }, {
206
- name: 'payerIsUser',
207
- type: 'bool'
208
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.V4_SWAP] = [{
209
- name: 'command',
210
- type: 'bytes'
211
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.WRAP_ETH] = [{
212
- name: 'recipient',
213
- type: 'address'
214
- }, {
215
- name: 'amount',
216
- type: 'uint256'
217
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.UNWRAP_WETH] = [{
218
- name: 'recipient',
219
- type: 'address'
220
- }, {
221
- name: 'amountMin',
222
- type: 'uint256'
223
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.SWEEP] = [{
224
- name: 'token',
225
- type: 'address'
226
- }, {
227
- name: 'recipient',
228
- type: 'address'
229
- }, {
230
- name: 'amountMin',
231
- type: 'uint256'
232
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.TRANSFER] = [{
233
- name: 'token',
234
- type: 'address'
235
- }, {
236
- name: 'recipient',
237
- type: 'address'
238
- }, {
239
- name: 'value',
240
- type: 'uint256'
241
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.PAY_PORTION] = [{
242
- name: 'token',
243
- type: 'address'
244
- }, {
245
- name: 'recipient',
246
- type: 'address'
247
- }, {
248
- name: 'bips',
249
- type: 'uint256'
250
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.BALANCE_CHECK_ERC20] = [{
251
- name: 'owner',
252
- type: 'address'
253
- }, {
254
- name: 'token',
255
- type: 'address'
256
- }, {
257
- name: 'minBalance',
258
- type: 'uint256'
259
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.V3_POSITION_MANAGER_PERMIT] = [{
260
- name: 'calldata',
261
- type: 'bytes'
262
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.V3_POSITION_MANAGER_CALL] = [{
263
- name: 'calldata',
264
- type: 'bytes'
265
- }], _COMMAND_ABI_DEFINITI[exports.CommandType.V4_POSITION_CALL] = [{
266
- name: 'calldata',
267
- type: 'bytes'
268
- }], _COMMAND_ABI_DEFINITI);
122
+ var COMMAND_DEFINITION = (_COMMAND_DEFINITION = {}, _COMMAND_DEFINITION[exports.CommandType.EXECUTE_SUB_PLAN] = {
123
+ parser: Parser.Abi,
124
+ params: [{
125
+ name: 'commands',
126
+ type: 'bytes'
127
+ }, {
128
+ name: 'inputs',
129
+ type: 'bytes[]'
130
+ }]
131
+ }, _COMMAND_DEFINITION[exports.CommandType.PERMIT2_PERMIT] = {
132
+ parser: Parser.Abi,
133
+ params: [{
134
+ name: 'permit',
135
+ type: PERMIT_STRUCT
136
+ }, {
137
+ name: 'signature',
138
+ type: 'bytes'
139
+ }]
140
+ }, _COMMAND_DEFINITION[exports.CommandType.PERMIT2_PERMIT_BATCH] = {
141
+ parser: Parser.Abi,
142
+ params: [{
143
+ name: 'permit',
144
+ type: PERMIT_BATCH_STRUCT
145
+ }, {
146
+ name: 'signature',
147
+ type: 'bytes'
148
+ }]
149
+ }, _COMMAND_DEFINITION[exports.CommandType.PERMIT2_TRANSFER_FROM] = {
150
+ parser: Parser.Abi,
151
+ params: [{
152
+ name: 'token',
153
+ type: 'address'
154
+ }, {
155
+ name: 'recipient',
156
+ type: 'address'
157
+ }, {
158
+ name: 'amount',
159
+ type: 'uint160'
160
+ }]
161
+ }, _COMMAND_DEFINITION[exports.CommandType.PERMIT2_TRANSFER_FROM_BATCH] = {
162
+ parser: Parser.Abi,
163
+ params: [{
164
+ name: 'transferFrom',
165
+ type: PERMIT2_TRANSFER_FROM_BATCH_STRUCT
166
+ }]
167
+ }, _COMMAND_DEFINITION[exports.CommandType.V3_SWAP_EXACT_IN] = {
168
+ parser: Parser.Abi,
169
+ params: [{
170
+ name: 'recipient',
171
+ type: 'address'
172
+ }, {
173
+ name: 'amountIn',
174
+ type: 'uint256'
175
+ }, {
176
+ name: 'amountOutMin',
177
+ type: 'uint256'
178
+ }, {
179
+ name: 'path',
180
+ subparser: Subparser.V3PathExactIn,
181
+ type: 'bytes'
182
+ }, {
183
+ name: 'payerIsUser',
184
+ type: 'bool'
185
+ }]
186
+ }, _COMMAND_DEFINITION[exports.CommandType.V3_SWAP_EXACT_OUT] = {
187
+ parser: Parser.Abi,
188
+ params: [{
189
+ name: 'recipient',
190
+ type: 'address'
191
+ }, {
192
+ name: 'amountOut',
193
+ type: 'uint256'
194
+ }, {
195
+ name: 'amountInMax',
196
+ type: 'uint256'
197
+ }, {
198
+ name: 'path',
199
+ subparser: Subparser.V3PathExactOut,
200
+ type: 'bytes'
201
+ }, {
202
+ name: 'payerIsUser',
203
+ type: 'bool'
204
+ }]
205
+ }, _COMMAND_DEFINITION[exports.CommandType.V2_SWAP_EXACT_IN] = {
206
+ parser: Parser.Abi,
207
+ params: [{
208
+ name: 'recipient',
209
+ type: 'address'
210
+ }, {
211
+ name: 'amountIn',
212
+ type: 'uint256'
213
+ }, {
214
+ name: 'amountOutMin',
215
+ type: 'uint256'
216
+ }, {
217
+ name: 'path',
218
+ type: 'address[]'
219
+ }, {
220
+ name: 'payerIsUser',
221
+ type: 'bool'
222
+ }]
223
+ }, _COMMAND_DEFINITION[exports.CommandType.V2_SWAP_EXACT_OUT] = {
224
+ parser: Parser.Abi,
225
+ params: [{
226
+ name: 'recipient',
227
+ type: 'address'
228
+ }, {
229
+ name: 'amountOut',
230
+ type: 'uint256'
231
+ }, {
232
+ name: 'amountInMax',
233
+ type: 'uint256'
234
+ }, {
235
+ name: 'path',
236
+ type: 'address[]'
237
+ }, {
238
+ name: 'payerIsUser',
239
+ type: 'bool'
240
+ }]
241
+ }, _COMMAND_DEFINITION[exports.CommandType.V4_SWAP] = {
242
+ parser: Parser.V4Actions
243
+ }, _COMMAND_DEFINITION[exports.CommandType.WRAP_ETH] = {
244
+ parser: Parser.Abi,
245
+ params: [{
246
+ name: 'recipient',
247
+ type: 'address'
248
+ }, {
249
+ name: 'amount',
250
+ type: 'uint256'
251
+ }]
252
+ }, _COMMAND_DEFINITION[exports.CommandType.UNWRAP_WETH] = {
253
+ parser: Parser.Abi,
254
+ params: [{
255
+ name: 'recipient',
256
+ type: 'address'
257
+ }, {
258
+ name: 'amountMin',
259
+ type: 'uint256'
260
+ }]
261
+ }, _COMMAND_DEFINITION[exports.CommandType.SWEEP] = {
262
+ parser: Parser.Abi,
263
+ params: [{
264
+ name: 'token',
265
+ type: 'address'
266
+ }, {
267
+ name: 'recipient',
268
+ type: 'address'
269
+ }, {
270
+ name: 'amountMin',
271
+ type: 'uint256'
272
+ }]
273
+ }, _COMMAND_DEFINITION[exports.CommandType.TRANSFER] = {
274
+ parser: Parser.Abi,
275
+ params: [{
276
+ name: 'token',
277
+ type: 'address'
278
+ }, {
279
+ name: 'recipient',
280
+ type: 'address'
281
+ }, {
282
+ name: 'value',
283
+ type: 'uint256'
284
+ }]
285
+ }, _COMMAND_DEFINITION[exports.CommandType.PAY_PORTION] = {
286
+ parser: Parser.Abi,
287
+ params: [{
288
+ name: 'token',
289
+ type: 'address'
290
+ }, {
291
+ name: 'recipient',
292
+ type: 'address'
293
+ }, {
294
+ name: 'bips',
295
+ type: 'uint256'
296
+ }]
297
+ }, _COMMAND_DEFINITION[exports.CommandType.BALANCE_CHECK_ERC20] = {
298
+ parser: Parser.Abi,
299
+ params: [{
300
+ name: 'owner',
301
+ type: 'address'
302
+ }, {
303
+ name: 'token',
304
+ type: 'address'
305
+ }, {
306
+ name: 'minBalance',
307
+ type: 'uint256'
308
+ }]
309
+ }, _COMMAND_DEFINITION[exports.CommandType.V3_POSITION_MANAGER_PERMIT] = {
310
+ parser: Parser.V3Actions
311
+ }, _COMMAND_DEFINITION[exports.CommandType.V3_POSITION_MANAGER_CALL] = {
312
+ parser: Parser.V3Actions
313
+ }, _COMMAND_DEFINITION[exports.CommandType.V4_POSITION_CALL] = {
314
+ parser: Parser.V4Actions
315
+ }, _COMMAND_DEFINITION);
269
316
  var RoutePlanner = /*#__PURE__*/function () {
270
317
  function RoutePlanner() {
271
318
  this.commands = '0x';
@@ -294,19 +341,29 @@ var RoutePlanner = /*#__PURE__*/function () {
294
341
  return RoutePlanner;
295
342
  }();
296
343
  function createCommand(type, parameters) {
297
- if (type === exports.CommandType.V4_SWAP || type === exports.CommandType.V3_POSITION_MANAGER_CALL || type === exports.CommandType.V3_POSITION_MANAGER_PERMIT || type === exports.CommandType.V4_POSITION_CALL) {
298
- return {
299
- type: type,
300
- encodedInput: parameters[0]
301
- };
344
+ var commandDef = COMMAND_DEFINITION[type];
345
+ switch (commandDef.parser) {
346
+ case Parser.Abi:
347
+ var encodedInput = utils.defaultAbiCoder.encode(commandDef.params.map(function (abi) {
348
+ return abi.type;
349
+ }), parameters);
350
+ return {
351
+ type: type,
352
+ encodedInput: encodedInput
353
+ };
354
+ case Parser.V4Actions:
355
+ // v4 swap data comes pre-encoded at index 0
356
+ return {
357
+ type: type,
358
+ encodedInput: parameters[0]
359
+ };
360
+ case Parser.V3Actions:
361
+ // v4 swap data comes pre-encoded at index 0
362
+ return {
363
+ type: type,
364
+ encodedInput: parameters[0]
365
+ };
302
366
  }
303
- var encodedInput = utils.defaultAbiCoder.encode(COMMAND_ABI_DEFINITION[type].map(function (abi) {
304
- return abi.type;
305
- }), parameters);
306
- return {
307
- type: type,
308
- encodedInput: encodedInput
309
- };
310
367
  }
311
368
 
312
369
  (function (RouterActionType) {
@@ -1046,34 +1103,54 @@ var CommandParser = /*#__PURE__*/function () {
1046
1103
  var commandTypes = CommandParser.getCommands(commands);
1047
1104
  return {
1048
1105
  commands: commandTypes.map(function (commandType, i) {
1049
- var abiDef = COMMAND_ABI_DEFINITION[commandType];
1050
- var rawParams = ethers.ethers.utils.defaultAbiCoder.decode(abiDef.map(function (command) {
1051
- return command.type;
1052
- }), inputs[i]);
1053
- var params = rawParams.map(function (param, j) {
1054
- switch (abiDef[j].subparser) {
1055
- case Subparser.V3PathExactIn:
1056
- return {
1057
- name: abiDef[j].name,
1058
- value: parseV3PathExactIn(param)
1059
- };
1060
- case Subparser.V3PathExactOut:
1061
- return {
1062
- name: abiDef[j].name,
1063
- value: parseV3PathExactOut(param)
1064
- };
1065
- default:
1066
- return {
1067
- name: abiDef[j].name,
1068
- value: param
1069
- };
1070
- }
1071
- });
1072
- return {
1073
- commandName: exports.CommandType[commandType],
1074
- commandType: commandType,
1075
- params: params
1076
- };
1106
+ var commandDef = COMMAND_DEFINITION[commandType];
1107
+ if (commandDef.parser === Parser.V4Actions) {
1108
+ var _V4BaseActionsParser$ = v4Sdk.V4BaseActionsParser.parseCalldata(inputs[i]),
1109
+ actions = _V4BaseActionsParser$.actions;
1110
+ return {
1111
+ commandName: exports.CommandType[commandType],
1112
+ commandType: commandType,
1113
+ params: v4RouterCallToParams(actions)
1114
+ };
1115
+ } else if (commandDef.parser === Parser.Abi) {
1116
+ var abiDef = commandDef.params;
1117
+ var rawParams = ethers.ethers.utils.defaultAbiCoder.decode(abiDef.map(function (command) {
1118
+ return command.type;
1119
+ }), inputs[i]);
1120
+ var params = rawParams.map(function (param, j) {
1121
+ switch (abiDef[j].subparser) {
1122
+ case Subparser.V3PathExactIn:
1123
+ return {
1124
+ name: abiDef[j].name,
1125
+ value: parseV3PathExactIn(param)
1126
+ };
1127
+ case Subparser.V3PathExactOut:
1128
+ return {
1129
+ name: abiDef[j].name,
1130
+ value: parseV3PathExactOut(param)
1131
+ };
1132
+ default:
1133
+ return {
1134
+ name: abiDef[j].name,
1135
+ value: param
1136
+ };
1137
+ }
1138
+ });
1139
+ return {
1140
+ commandName: exports.CommandType[commandType],
1141
+ commandType: commandType,
1142
+ params: params
1143
+ };
1144
+ } else if (commandDef.parser === Parser.V3Actions) {
1145
+ // TODO: implement better parsing here
1146
+ return {
1147
+ commandName: exports.CommandType[commandType],
1148
+ commandType: commandType,
1149
+ params: inputs
1150
+ };
1151
+ } else {
1152
+ throw new Error("Unsupported parser: " + commandDef);
1153
+ }
1077
1154
  })
1078
1155
  };
1079
1156
  }
@@ -1128,6 +1205,19 @@ function parseV3PathExactOut(path) {
1128
1205
  }
1129
1206
  return res;
1130
1207
  }
1208
+ function v4RouterCallToParams(actions) {
1209
+ return actions.map(function (action) {
1210
+ return {
1211
+ name: action.actionName,
1212
+ value: action.params.map(function (param) {
1213
+ return {
1214
+ name: param.name,
1215
+ value: param.value
1216
+ };
1217
+ })
1218
+ };
1219
+ });
1220
+ }
1131
1221
 
1132
1222
  exports.CommandParser = CommandParser;
1133
1223
  exports.ROUTER_AS_RECIPIENT = ROUTER_AS_RECIPIENT;