hermes-swap 0.1.7 → 0.1.8

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/cjs/index.js CHANGED
@@ -133,15 +133,6 @@ var Hermes = class {
133
133
  toCoin: pathItem.toCoinAddress,
134
134
  extra: pathItem.extra ?? "0x"
135
135
  }));
136
- const erc20 = new import_ethers.Contract(fromTokenAddress, ["function balanceOf(address) view returns (uint256)", "function allowance(address, address) view returns (uint256)"], wallet);
137
- const userBalance = await erc20.balanceOf(params.user);
138
- if (userBalance < params.amountInWei) {
139
- throw new Error("Insufficient balance for swap");
140
- }
141
- const currentAllowance = await erc20.allowance(params.user, aggregatorAddress);
142
- if (currentAllowance < params.amountInWei) {
143
- throw new Error("Insufficient allowance token amount for swap");
144
- }
145
136
  let estimateGas;
146
137
  const { gasLimit: _ignore, ...estimationOverrides } = txReq;
147
138
  try {
@@ -316,10 +307,6 @@ var Hermes = class {
316
307
  if (!wallet) {
317
308
  throw new Error(`Wallet not configured for chain: ${params.chain}`);
318
309
  }
319
- const fromCoinAddress = params.path[0].fromCoinAddress;
320
- if (fromCoinAddress && fromCoinAddress !== import_ethers.ethers.ZeroAddress) {
321
- await this.checkIsEnoughToken(fromCoinAddress, params.user, params.amountInWei, aggregatorAddress, wallet);
322
- }
323
310
  const swapParams = params.path.map((pathItem) => ({
324
311
  dexType: pathItem.dexType,
325
312
  pool: pathItem.poolAddress,
package/dist/esm/index.js CHANGED
@@ -189,9 +189,6 @@ var Hermes = /*#__PURE__*/function () {
189
189
  wallet,
190
190
  aggregator,
191
191
  swapParams,
192
- erc20,
193
- userBalance,
194
- currentAllowance,
195
192
  estimateGas,
196
193
  _txReq,
197
194
  _ignore,
@@ -237,108 +234,87 @@ var Hermes = /*#__PURE__*/function () {
237
234
  extra: (_pathItem$extra = pathItem.extra) !== null && _pathItem$extra !== void 0 ? _pathItem$extra : '0x'
238
235
  };
239
236
  });
240
- erc20 = new Contract(fromTokenAddress, ['function balanceOf(address) view returns (uint256)', 'function allowance(address, address) view returns (uint256)'], wallet);
241
- _context3.next = 15;
242
- return erc20.balanceOf(params.user);
243
- case 15:
244
- userBalance = _context3.sent;
245
- if (!(userBalance < params.amountInWei)) {
246
- _context3.next = 18;
247
- break;
248
- }
249
- throw new Error('Insufficient balance for swap');
250
- case 18:
251
- _context3.next = 20;
252
- return erc20.allowance(params.user, aggregatorAddress);
253
- case 20:
254
- currentAllowance = _context3.sent;
255
- if (!(currentAllowance < params.amountInWei)) {
256
- _context3.next = 23;
257
- break;
258
- }
259
- throw new Error('Insufficient allowance token amount for swap');
260
- case 23:
261
237
  _txReq = txReq, _ignore = _txReq.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq, _excluded);
262
- _context3.prev = 24;
263
- _context3.next = 27;
238
+ _context3.prev = 13;
239
+ _context3.next = 16;
264
240
  return aggregator.swap.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, estimationOverrides);
265
- case 27:
241
+ case 16:
266
242
  estimateGas = _context3.sent;
267
- _context3.next = 34;
243
+ _context3.next = 23;
268
244
  break;
269
- case 30:
270
- _context3.prev = 30;
271
- _context3.t0 = _context3["catch"](24);
245
+ case 19:
246
+ _context3.prev = 19;
247
+ _context3.t0 = _context3["catch"](13);
272
248
  console.warn('Aggregator estimateGas.swap failed', _context3.t0);
273
249
  throw _context3.t0;
274
- case 34:
250
+ case 23:
275
251
  txReq = this.resolveGasLimit(txReq, estimateGas);
276
- _context3.next = 37;
252
+ _context3.next = 26;
277
253
  return this.resolveNonce(wallet.provider, wallet.address, txReq);
278
- case 37:
254
+ case 26:
279
255
  txReq = _context3.sent;
280
- _context3.next = 40;
256
+ _context3.next = 29;
281
257
  return this.resolvePricing(wallet.provider, txReq);
282
- case 40:
258
+ case 29:
283
259
  txReq = _context3.sent;
284
- _context3.next = 43;
260
+ _context3.next = 32;
285
261
  return aggregator.swap(params.user, params.amountInWei, swapParams, params.minAmountOutList, txReq);
286
- case 43:
262
+ case 32:
287
263
  txResponse = _context3.sent;
288
- _context3.next = 46;
264
+ _context3.next = 35;
289
265
  return txResponse.wait();
290
- case 46:
266
+ case 35:
291
267
  receipt = _context3.sent;
292
268
  iface = new ethers.Interface(AggregatorAbi);
293
269
  amountOut = null;
294
270
  _iterator = _createForOfIteratorHelper(receipt.logs);
295
- _context3.prev = 50;
271
+ _context3.prev = 39;
296
272
  _iterator.s();
297
- case 52:
273
+ case 41:
298
274
  if ((_step = _iterator.n()).done) {
299
- _context3.next = 66;
275
+ _context3.next = 55;
300
276
  break;
301
277
  }
302
278
  log = _step.value;
303
279
  if (!(log.address.toLowerCase() === aggregatorAddress.toLowerCase())) {
304
- _context3.next = 64;
280
+ _context3.next = 53;
305
281
  break;
306
282
  }
307
- _context3.prev = 55;
283
+ _context3.prev = 44;
308
284
  parsed = iface.parseLog(log);
309
285
  if (!(parsed && parsed.name === 'Swapped' && parsed.args && parsed.args.amountOut !== undefined)) {
310
- _context3.next = 60;
286
+ _context3.next = 49;
311
287
  break;
312
288
  }
313
289
  amountOut = parsed.args.amountOut;
314
- return _context3.abrupt("break", 66);
315
- case 60:
316
- _context3.next = 64;
290
+ return _context3.abrupt("break", 55);
291
+ case 49:
292
+ _context3.next = 53;
317
293
  break;
318
- case 62:
319
- _context3.prev = 62;
320
- _context3.t1 = _context3["catch"](55);
321
- case 64:
322
- _context3.next = 52;
294
+ case 51:
295
+ _context3.prev = 51;
296
+ _context3.t1 = _context3["catch"](44);
297
+ case 53:
298
+ _context3.next = 41;
323
299
  break;
324
- case 66:
325
- _context3.next = 71;
300
+ case 55:
301
+ _context3.next = 60;
326
302
  break;
327
- case 68:
328
- _context3.prev = 68;
329
- _context3.t2 = _context3["catch"](50);
303
+ case 57:
304
+ _context3.prev = 57;
305
+ _context3.t2 = _context3["catch"](39);
330
306
  _iterator.e(_context3.t2);
331
- case 71:
332
- _context3.prev = 71;
307
+ case 60:
308
+ _context3.prev = 60;
333
309
  _iterator.f();
334
- return _context3.finish(71);
335
- case 74:
310
+ return _context3.finish(60);
311
+ case 63:
336
312
  if (amountOut) {
337
- _context3.next = 76;
313
+ _context3.next = 65;
338
314
  break;
339
315
  }
340
316
  throw new Error("Swapped event not found: ".concat(receipt.hash));
341
- case 76:
317
+ case 65:
342
318
  return _context3.abrupt("return", {
343
319
  fromToken: fromTokenAddress,
344
320
  toToken: toTokenAddress,
@@ -348,11 +324,11 @@ var Hermes = /*#__PURE__*/function () {
348
324
  to: receipt.from,
349
325
  logs: receipt.logs
350
326
  });
351
- case 77:
327
+ case 66:
352
328
  case "end":
353
329
  return _context3.stop();
354
330
  }
355
- }, _callee3, this, [[24, 30], [50, 68, 71, 74], [55, 62]]);
331
+ }, _callee3, this, [[13, 19], [39, 57, 60, 63], [44, 51]]);
356
332
  }));
357
333
  function swap(_x3) {
358
334
  return _swap.apply(this, arguments);
@@ -613,7 +589,6 @@ var Hermes = /*#__PURE__*/function () {
613
589
  var txReq,
614
590
  aggregatorAddress,
615
591
  wallet,
616
- fromCoinAddress,
617
592
  swapParams,
618
593
  bridgeArgs,
619
594
  _txReq3,
@@ -645,14 +620,6 @@ var Hermes = /*#__PURE__*/function () {
645
620
  }
646
621
  throw new Error("Wallet not configured for chain: ".concat(params.chain));
647
622
  case 6:
648
- fromCoinAddress = params.path[0].fromCoinAddress;
649
- if (!(fromCoinAddress && fromCoinAddress !== ethers.ZeroAddress)) {
650
- _context6.next = 10;
651
- break;
652
- }
653
- _context6.next = 10;
654
- return this.checkIsEnoughToken(fromCoinAddress, params.user, params.amountInWei, aggregatorAddress, wallet);
655
- case 10:
656
623
  // 准备合约参数
657
624
  swapParams = params.path.map(function (pathItem) {
658
625
  var _pathItem$extra3;
@@ -680,78 +647,78 @@ var Hermes = /*#__PURE__*/function () {
680
647
  }
681
648
  _txReq3 = txReq, _ignore = _txReq3.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq3, _excluded3);
682
649
  aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
683
- _context6.next = 17;
650
+ _context6.next = 13;
684
651
  return aggregator.swapAndBridge.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, estimationOverrides);
685
- case 17:
652
+ case 13:
686
653
  estimateGas = _context6.sent;
687
654
  txReq = this.resolveGasLimit(txReq, estimateGas);
688
- _context6.next = 21;
655
+ _context6.next = 17;
689
656
  return this.resolveNonce(wallet.provider, wallet.address, txReq);
690
- case 21:
657
+ case 17:
691
658
  txReq = _context6.sent;
692
- _context6.next = 24;
659
+ _context6.next = 20;
693
660
  return this.resolvePricing(wallet.provider, txReq);
694
- case 24:
661
+ case 20:
695
662
  txReq = _context6.sent;
696
- _context6.next = 27;
663
+ _context6.next = 23;
697
664
  return aggregator.swapAndBridge(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txReq);
698
- case 27:
665
+ case 23:
699
666
  response = _context6.sent;
700
- _context6.next = 30;
667
+ _context6.next = 26;
701
668
  return response.wait();
702
- case 30:
669
+ case 26:
703
670
  txReceipt = _context6.sent;
704
671
  // 解析 SwapAndBridge 事件
705
672
  iface = new ethers.Interface(AggregatorAbi);
706
673
  outputAmountWei = null;
707
674
  _iterator2 = _createForOfIteratorHelper(txReceipt.logs);
708
- _context6.prev = 34;
675
+ _context6.prev = 30;
709
676
  _iterator2.s();
710
- case 36:
677
+ case 32:
711
678
  if ((_step2 = _iterator2.n()).done) {
712
- _context6.next = 50;
679
+ _context6.next = 46;
713
680
  break;
714
681
  }
715
682
  log = _step2.value;
716
683
  if (!(log.address.toLowerCase() === aggregatorAddress.toLowerCase())) {
717
- _context6.next = 48;
684
+ _context6.next = 44;
718
685
  break;
719
686
  }
720
- _context6.prev = 39;
687
+ _context6.prev = 35;
721
688
  parsed = iface.parseLog(log);
722
689
  if (!(parsed && parsed.name === 'SwapAndBridge' && parsed.args && parsed.args.amountOut !== undefined)) {
723
- _context6.next = 44;
690
+ _context6.next = 40;
724
691
  break;
725
692
  }
726
693
  outputAmountWei = parsed.args.amountOut;
727
- return _context6.abrupt("break", 50);
694
+ return _context6.abrupt("break", 46);
695
+ case 40:
696
+ _context6.next = 44;
697
+ break;
698
+ case 42:
699
+ _context6.prev = 42;
700
+ _context6.t0 = _context6["catch"](35);
728
701
  case 44:
729
- _context6.next = 48;
702
+ _context6.next = 32;
730
703
  break;
731
704
  case 46:
732
- _context6.prev = 46;
733
- _context6.t0 = _context6["catch"](39);
734
- case 48:
735
- _context6.next = 36;
736
- break;
737
- case 50:
738
- _context6.next = 55;
705
+ _context6.next = 51;
739
706
  break;
740
- case 52:
741
- _context6.prev = 52;
742
- _context6.t1 = _context6["catch"](34);
707
+ case 48:
708
+ _context6.prev = 48;
709
+ _context6.t1 = _context6["catch"](30);
743
710
  _iterator2.e(_context6.t1);
744
- case 55:
745
- _context6.prev = 55;
711
+ case 51:
712
+ _context6.prev = 51;
746
713
  _iterator2.f();
747
- return _context6.finish(55);
748
- case 58:
714
+ return _context6.finish(51);
715
+ case 54:
749
716
  if (outputAmountWei) {
750
- _context6.next = 60;
717
+ _context6.next = 56;
751
718
  break;
752
719
  }
753
720
  throw new Error("SwapAndBridge event not found: ".concat(txReceipt.hash));
754
- case 60:
721
+ case 56:
755
722
  return _context6.abrupt("return", {
756
723
  fromToken: params.path[0].fromCoinAddress,
757
724
  toToken: params.path[params.path.length - 1].toCoinAddress,
@@ -761,11 +728,11 @@ var Hermes = /*#__PURE__*/function () {
761
728
  to: txReceipt.to,
762
729
  logs: txReceipt.logs
763
730
  });
764
- case 61:
731
+ case 57:
765
732
  case "end":
766
733
  return _context6.stop();
767
734
  }
768
- }, _callee6, this, [[34, 52, 55, 58], [39, 46]]);
735
+ }, _callee6, this, [[30, 48, 51, 54], [35, 42]]);
769
736
  }));
770
737
  function swapAndBridge(_x6) {
771
738
  return _swapAndBridge.apply(this, arguments);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-swap",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "A TypeScript utility library for swap and bridge",
5
5
  "type": "module",
6
6
  "main": "dist/esm/index.js",