@uniswap/universal-router-sdk 4.1.1 → 4.2.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.
@@ -3,7 +3,7 @@ import { abi } from '@uniswap/universal-router/artifacts/contracts/UniversalRout
3
3
  import { Interface } from '@ethersproject/abi';
4
4
  import { BigNumber, ethers } from 'ethers';
5
5
  import { toHex, Trade as Trade$1, encodeRouteToPath, Pool, NonfungiblePositionManager, Route } from '@uniswap/v3-sdk';
6
- import { Trade as Trade$2, V4Planner, V4PositionManager } from '@uniswap/v4-sdk';
6
+ import { Trade as Trade$2, V4Planner, V4PositionManager, V4BaseActionsParser } from '@uniswap/v4-sdk';
7
7
  import { TradeType, Percent, validateAndParseAddress, CHAIN_TO_ADDRESSES_MAP, CurrencyAmount, Ether, Token } from '@uniswap/sdk-core';
8
8
  import { defaultAbiCoder } from 'ethers/lib/utils';
9
9
  import { Trade, Pair, Route as Route$1 } from '@uniswap/v2-sdk';
@@ -74,7 +74,7 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
74
74
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
75
75
  }
76
76
 
77
- var _COMMAND_ABI_DEFINITI;
77
+ var _COMMAND_DEFINITION;
78
78
  /**
79
79
  * CommandTypes
80
80
  * @description Flags that modify a command's execution
@@ -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([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[CommandType.EXECUTE_SUB_PLAN] = [{
117
- name: 'commands',
118
- type: 'bytes'
119
- }, {
120
- name: 'inputs',
121
- type: 'bytes[]'
122
- }], _COMMAND_ABI_DEFINITI[CommandType.PERMIT2_PERMIT] = [{
123
- name: 'permit',
124
- type: PERMIT_STRUCT
125
- }, {
126
- name: 'signature',
127
- type: 'bytes'
128
- }], _COMMAND_ABI_DEFINITI[CommandType.PERMIT2_PERMIT_BATCH] = [{
129
- name: 'permit',
130
- type: PERMIT_BATCH_STRUCT
131
- }, {
132
- name: 'signature',
133
- type: 'bytes'
134
- }], _COMMAND_ABI_DEFINITI[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[CommandType.PERMIT2_TRANSFER_FROM_BATCH] = [{
144
- name: 'transferFrom',
145
- type: PERMIT2_TRANSFER_FROM_BATCH_STRUCT
146
- }], _COMMAND_ABI_DEFINITI[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[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[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[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[CommandType.V4_SWAP] = [{
209
- name: 'command',
210
- type: 'bytes'
211
- }], _COMMAND_ABI_DEFINITI[CommandType.WRAP_ETH] = [{
212
- name: 'recipient',
213
- type: 'address'
214
- }, {
215
- name: 'amount',
216
- type: 'uint256'
217
- }], _COMMAND_ABI_DEFINITI[CommandType.UNWRAP_WETH] = [{
218
- name: 'recipient',
219
- type: 'address'
220
- }, {
221
- name: 'amountMin',
222
- type: 'uint256'
223
- }], _COMMAND_ABI_DEFINITI[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[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[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[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[CommandType.V3_POSITION_MANAGER_PERMIT] = [{
260
- name: 'calldata',
261
- type: 'bytes'
262
- }], _COMMAND_ABI_DEFINITI[CommandType.V3_POSITION_MANAGER_CALL] = [{
263
- name: 'calldata',
264
- type: 'bytes'
265
- }], _COMMAND_ABI_DEFINITI[CommandType.V4_POSITION_CALL] = [{
266
- name: 'calldata',
267
- type: 'bytes'
268
- }], _COMMAND_ABI_DEFINITI);
122
+ var COMMAND_DEFINITION = (_COMMAND_DEFINITION = {}, _COMMAND_DEFINITION[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[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[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[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[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[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[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[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[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[CommandType.V4_SWAP] = {
242
+ parser: Parser.V4Actions
243
+ }, _COMMAND_DEFINITION[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[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[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[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[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[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[CommandType.V3_POSITION_MANAGER_PERMIT] = {
310
+ parser: Parser.V3Actions
311
+ }, _COMMAND_DEFINITION[CommandType.V3_POSITION_MANAGER_CALL] = {
312
+ parser: Parser.V3Actions
313
+ }, _COMMAND_DEFINITION[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 === CommandType.V4_SWAP || type === CommandType.V3_POSITION_MANAGER_CALL || type === CommandType.V3_POSITION_MANAGER_PERMIT || type === 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 = 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 = 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
  var RouterActionType;
@@ -1049,34 +1106,54 @@ var CommandParser = /*#__PURE__*/function () {
1049
1106
  var commandTypes = CommandParser.getCommands(commands);
1050
1107
  return {
1051
1108
  commands: commandTypes.map(function (commandType, i) {
1052
- var abiDef = COMMAND_ABI_DEFINITION[commandType];
1053
- var rawParams = ethers.utils.defaultAbiCoder.decode(abiDef.map(function (command) {
1054
- return command.type;
1055
- }), inputs[i]);
1056
- var params = rawParams.map(function (param, j) {
1057
- switch (abiDef[j].subparser) {
1058
- case Subparser.V3PathExactIn:
1059
- return {
1060
- name: abiDef[j].name,
1061
- value: parseV3PathExactIn(param)
1062
- };
1063
- case Subparser.V3PathExactOut:
1064
- return {
1065
- name: abiDef[j].name,
1066
- value: parseV3PathExactOut(param)
1067
- };
1068
- default:
1069
- return {
1070
- name: abiDef[j].name,
1071
- value: param
1072
- };
1073
- }
1074
- });
1075
- return {
1076
- commandName: CommandType[commandType],
1077
- commandType: commandType,
1078
- params: params
1079
- };
1109
+ var commandDef = COMMAND_DEFINITION[commandType];
1110
+ if (commandDef.parser === Parser.V4Actions) {
1111
+ var _V4BaseActionsParser$ = V4BaseActionsParser.parseCalldata(inputs[i]),
1112
+ actions = _V4BaseActionsParser$.actions;
1113
+ return {
1114
+ commandName: CommandType[commandType],
1115
+ commandType: commandType,
1116
+ params: v4RouterCallToParams(actions)
1117
+ };
1118
+ } else if (commandDef.parser === Parser.Abi) {
1119
+ var abiDef = commandDef.params;
1120
+ var rawParams = ethers.utils.defaultAbiCoder.decode(abiDef.map(function (command) {
1121
+ return command.type;
1122
+ }), inputs[i]);
1123
+ var params = rawParams.map(function (param, j) {
1124
+ switch (abiDef[j].subparser) {
1125
+ case Subparser.V3PathExactIn:
1126
+ return {
1127
+ name: abiDef[j].name,
1128
+ value: parseV3PathExactIn(param)
1129
+ };
1130
+ case Subparser.V3PathExactOut:
1131
+ return {
1132
+ name: abiDef[j].name,
1133
+ value: parseV3PathExactOut(param)
1134
+ };
1135
+ default:
1136
+ return {
1137
+ name: abiDef[j].name,
1138
+ value: param
1139
+ };
1140
+ }
1141
+ });
1142
+ return {
1143
+ commandName: CommandType[commandType],
1144
+ commandType: commandType,
1145
+ params: params
1146
+ };
1147
+ } else if (commandDef.parser === Parser.V3Actions) {
1148
+ // TODO: implement better parsing here
1149
+ return {
1150
+ commandName: CommandType[commandType],
1151
+ commandType: commandType,
1152
+ params: inputs
1153
+ };
1154
+ } else {
1155
+ throw new Error("Unsupported parser: " + commandDef);
1156
+ }
1080
1157
  })
1081
1158
  };
1082
1159
  }
@@ -1131,6 +1208,19 @@ function parseV3PathExactOut(path) {
1131
1208
  }
1132
1209
  return res;
1133
1210
  }
1211
+ function v4RouterCallToParams(actions) {
1212
+ return actions.map(function (action) {
1213
+ return {
1214
+ name: action.actionName,
1215
+ value: action.params.map(function (param) {
1216
+ return {
1217
+ name: param.name,
1218
+ value: param.value
1219
+ };
1220
+ })
1221
+ };
1222
+ });
1223
+ }
1134
1224
 
1135
1225
  export { CommandParser, CommandType, PoolType, ROUTER_AS_RECIPIENT, RoutePlanner, RouterActionType, RouterTradeAdapter, SwapRouter, UNIVERSAL_ROUTER_ADDRESS, UNIVERSAL_ROUTER_CREATION_BLOCK, UniswapTrade, UniversalRouterVersion, UnwrapWETH, WETH_ADDRESS, isNativeCurrency };
1136
1226
  //# sourceMappingURL=universal-router-sdk.esm.js.map