@uniswap/universal-router-sdk 3.3.0 → 3.4.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
@@ -3,3 +3,4 @@ export * from './entities';
3
3
  export * from './utils/routerTradeAdapter';
4
4
  export { RoutePlanner, CommandType } from './utils/routerCommands';
5
5
  export { UNIVERSAL_ROUTER_CREATION_BLOCK, UNIVERSAL_ROUTER_ADDRESS, PERMIT2_ADDRESS, ROUTER_AS_RECIPIENT, WETH_ADDRESS, UniversalRouterVersion, } from './utils/constants';
6
+ export { CommandParser, UniversalRouterCommand, UniversalRouterCall, Param } from './utils/commandParser';
@@ -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 _ABI_DEFINITION;
83
+ var _COMMAND_ABI_DEFINITI;
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";
@@ -102,13 +102,170 @@ var _ABI_DEFINITION;
102
102
  CommandType[CommandType["V4_POSITION_CALL"] = 19] = "V4_POSITION_CALL";
103
103
  CommandType[CommandType["EXECUTE_SUB_PLAN"] = 33] = "EXECUTE_SUB_PLAN";
104
104
  })(exports.CommandType || (exports.CommandType = {}));
105
+ var Subparser;
106
+ (function (Subparser) {
107
+ Subparser[Subparser["V3PathExactIn"] = 0] = "V3PathExactIn";
108
+ Subparser[Subparser["V3PathExactOut"] = 1] = "V3PathExactOut";
109
+ })(Subparser || (Subparser = {}));
105
110
  var ALLOW_REVERT_FLAG = 0x80;
106
111
  var REVERTIBLE_COMMANDS = /*#__PURE__*/new Set([exports.CommandType.EXECUTE_SUB_PLAN]);
107
112
  var PERMIT_STRUCT = '((address token,uint160 amount,uint48 expiration,uint48 nonce) details,address spender,uint256 sigDeadline)';
108
113
  var PERMIT_BATCH_STRUCT = '((address token,uint160 amount,uint48 expiration,uint48 nonce)[] details,address spender,uint256 sigDeadline)';
109
114
  var PERMIT2_TRANSFER_FROM_STRUCT = '(address from,address to,uint160 amount,address token)';
110
115
  var PERMIT2_TRANSFER_FROM_BATCH_STRUCT = PERMIT2_TRANSFER_FROM_STRUCT + '[]';
111
- var ABI_DEFINITION = (_ABI_DEFINITION = {}, _ABI_DEFINITION[exports.CommandType.EXECUTE_SUB_PLAN] = ['bytes', 'bytes[]'], _ABI_DEFINITION[exports.CommandType.PERMIT2_PERMIT] = [PERMIT_STRUCT, 'bytes'], _ABI_DEFINITION[exports.CommandType.PERMIT2_PERMIT_BATCH] = [PERMIT_BATCH_STRUCT, 'bytes'], _ABI_DEFINITION[exports.CommandType.PERMIT2_TRANSFER_FROM] = ['address', 'address', 'uint160'], _ABI_DEFINITION[exports.CommandType.PERMIT2_TRANSFER_FROM_BATCH] = [PERMIT2_TRANSFER_FROM_BATCH_STRUCT], _ABI_DEFINITION[exports.CommandType.V3_SWAP_EXACT_IN] = ['address', 'uint256', 'uint256', 'bytes', 'bool'], _ABI_DEFINITION[exports.CommandType.V3_SWAP_EXACT_OUT] = ['address', 'uint256', 'uint256', 'bytes', 'bool'], _ABI_DEFINITION[exports.CommandType.V2_SWAP_EXACT_IN] = ['address', 'uint256', 'uint256', 'address[]', 'bool'], _ABI_DEFINITION[exports.CommandType.V2_SWAP_EXACT_OUT] = ['address', 'uint256', 'uint256', 'address[]', 'bool'], _ABI_DEFINITION[exports.CommandType.V4_SWAP] = ['bytes'], _ABI_DEFINITION[exports.CommandType.WRAP_ETH] = ['address', 'uint256'], _ABI_DEFINITION[exports.CommandType.UNWRAP_WETH] = ['address', 'uint256'], _ABI_DEFINITION[exports.CommandType.SWEEP] = ['address', 'address', 'uint256'], _ABI_DEFINITION[exports.CommandType.TRANSFER] = ['address', 'address', 'uint256'], _ABI_DEFINITION[exports.CommandType.PAY_PORTION] = ['address', 'address', 'uint256'], _ABI_DEFINITION[exports.CommandType.BALANCE_CHECK_ERC20] = ['address', 'address', 'uint256'], _ABI_DEFINITION[exports.CommandType.V3_POSITION_MANAGER_PERMIT] = ['bytes'], _ABI_DEFINITION[exports.CommandType.V3_POSITION_MANAGER_CALL] = ['bytes'], _ABI_DEFINITION[exports.CommandType.V4_POSITION_CALL] = ['bytes'], _ABI_DEFINITION);
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);
112
269
  var RoutePlanner = /*#__PURE__*/function () {
113
270
  function RoutePlanner() {
114
271
  this.commands = '0x';
@@ -117,6 +274,7 @@ var RoutePlanner = /*#__PURE__*/function () {
117
274
  var _proto = RoutePlanner.prototype;
118
275
  _proto.addSubPlan = function addSubPlan(subplan) {
119
276
  this.addCommand(exports.CommandType.EXECUTE_SUB_PLAN, [subplan.commands, subplan.inputs], true);
277
+ return this;
120
278
  };
121
279
  _proto.addCommand = function addCommand(type, parameters, allowRevert) {
122
280
  if (allowRevert === void 0) {
@@ -131,6 +289,7 @@ var RoutePlanner = /*#__PURE__*/function () {
131
289
  command.type = command.type | ALLOW_REVERT_FLAG;
132
290
  }
133
291
  this.commands = this.commands.concat(command.type.toString(16).padStart(2, '0'));
292
+ return this;
134
293
  };
135
294
  return RoutePlanner;
136
295
  }();
@@ -141,7 +300,9 @@ function createCommand(type, parameters) {
141
300
  encodedInput: parameters[0]
142
301
  };
143
302
  }
144
- var encodedInput = utils.defaultAbiCoder.encode(ABI_DEFINITION[type], parameters);
303
+ var encodedInput = utils.defaultAbiCoder.encode(COMMAND_ABI_DEFINITION[type].map(function (abi) {
304
+ return abi.type;
305
+ }), parameters);
145
306
  return {
146
307
  type: type,
147
308
  encodedInput: encodedInput
@@ -827,6 +988,103 @@ RouterTradeAdapter.toPair = function (_ref2) {
827
988
  return new v2Sdk.Pair(sdkCore.CurrencyAmount.fromRawAmount(RouterTradeAdapter.toToken(reserve0.token), reserve0.quotient), sdkCore.CurrencyAmount.fromRawAmount(RouterTradeAdapter.toToken(reserve1.token), reserve1.quotient));
828
989
  };
829
990
 
991
+ // Parses UniversalRouter commands
992
+ var CommandParser = /*#__PURE__*/function () {
993
+ function CommandParser() {}
994
+ CommandParser.parseCalldata = function parseCalldata(calldata) {
995
+ var txDescription = CommandParser.INTERFACE.parseTransaction({
996
+ data: calldata
997
+ });
998
+ var _txDescription$args = txDescription.args,
999
+ commands = _txDescription$args.commands,
1000
+ inputs = _txDescription$args.inputs;
1001
+ var commandTypes = CommandParser.getCommands(commands);
1002
+ return {
1003
+ commands: commandTypes.map(function (commandType, i) {
1004
+ var abiDef = COMMAND_ABI_DEFINITION[commandType];
1005
+ var rawParams = ethers.ethers.utils.defaultAbiCoder.decode(abiDef.map(function (command) {
1006
+ return command.type;
1007
+ }), inputs[i]);
1008
+ var params = rawParams.map(function (param, j) {
1009
+ switch (abiDef[j].subparser) {
1010
+ case Subparser.V3PathExactIn:
1011
+ return {
1012
+ name: abiDef[j].name,
1013
+ value: parseV3PathExactIn(param)
1014
+ };
1015
+ case Subparser.V3PathExactOut:
1016
+ return {
1017
+ name: abiDef[j].name,
1018
+ value: parseV3PathExactOut(param)
1019
+ };
1020
+ default:
1021
+ return {
1022
+ name: abiDef[j].name,
1023
+ value: param
1024
+ };
1025
+ }
1026
+ });
1027
+ return {
1028
+ commandName: exports.CommandType[commandType],
1029
+ commandType: commandType,
1030
+ params: params
1031
+ };
1032
+ })
1033
+ };
1034
+ }
1035
+ // parse command types from bytes string
1036
+ ;
1037
+ CommandParser.getCommands = function getCommands(commands) {
1038
+ var commandTypes = [];
1039
+ for (var i = 2; i < commands.length; i += 2) {
1040
+ var _byte = commands.substring(i, i + 2);
1041
+ commandTypes.push(parseInt(_byte, 16));
1042
+ }
1043
+ return commandTypes;
1044
+ };
1045
+ return CommandParser;
1046
+ }();
1047
+ CommandParser.INTERFACE = /*#__PURE__*/new abi.Interface(UniversalRouter_json.abi);
1048
+ function parseV3PathExactIn(path) {
1049
+ var strippedPath = path.replace('0x', '');
1050
+ var tokenIn = ethers.ethers.utils.getAddress(strippedPath.substring(0, 40));
1051
+ var loc = 40;
1052
+ var res = [];
1053
+ while (loc < strippedPath.length) {
1054
+ var feeAndTokenOut = strippedPath.substring(loc, loc + 46);
1055
+ var fee = parseInt(feeAndTokenOut.substring(0, 6), 16);
1056
+ var tokenOut = ethers.ethers.utils.getAddress(feeAndTokenOut.substring(6, 46));
1057
+ res.push({
1058
+ tokenIn: tokenIn,
1059
+ tokenOut: tokenOut,
1060
+ fee: fee
1061
+ });
1062
+ tokenIn = tokenOut;
1063
+ loc += 46;
1064
+ }
1065
+ return res;
1066
+ }
1067
+ function parseV3PathExactOut(path) {
1068
+ var strippedPath = path.replace('0x', '');
1069
+ var tokenIn = ethers.ethers.utils.getAddress(strippedPath.substring(strippedPath.length - 40));
1070
+ var loc = strippedPath.length - 86; // 86 = (20 addr + 3 fee + 20 addr) * 2 (for hex characters)
1071
+ var res = [];
1072
+ while (loc >= 0) {
1073
+ var feeAndTokenOut = strippedPath.substring(loc, loc + 46);
1074
+ var tokenOut = ethers.ethers.utils.getAddress(feeAndTokenOut.substring(0, 40));
1075
+ var fee = parseInt(feeAndTokenOut.substring(40, 46), 16);
1076
+ res.push({
1077
+ tokenIn: tokenIn,
1078
+ tokenOut: tokenOut,
1079
+ fee: fee
1080
+ });
1081
+ tokenIn = tokenOut;
1082
+ loc -= 46;
1083
+ }
1084
+ return res;
1085
+ }
1086
+
1087
+ exports.CommandParser = CommandParser;
830
1088
  exports.PERMIT2_ADDRESS = PERMIT2_ADDRESS;
831
1089
  exports.ROUTER_AS_RECIPIENT = ROUTER_AS_RECIPIENT;
832
1090
  exports.RoutePlanner = RoutePlanner;