hermes-swap 0.1.9 → 0.1.10

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/esm/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  var _excluded = ["gasLimit"],
2
2
  _excluded2 = ["gasLimit"],
3
3
  _excluded3 = ["gasLimit"],
4
- _excluded4 = ["gasPrice"];
4
+ _excluded4 = ["gasLimit"],
5
+ _excluded5 = ["gasLimit"],
6
+ _excluded6 = ["gasPrice"];
5
7
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
6
8
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
7
9
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
@@ -25,6 +27,7 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
25
27
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
26
28
  import { ChainNameEnum, AddressConst, DexType, BridgeType, IEstimateType, LayerZeroV1ChainIdMap, LayerZeroV1ChainNameMap, LayerZeroV2ChainIdMap, LayerZeroV2ChainNameMap } from "./types.js";
27
29
  import { ethers, Contract } from 'ethers';
30
+ import axios from 'axios';
28
31
  import QuoterAbi from "./abis/quoter.js";
29
32
  import AggregatorAbi from "./abis/aggregator.js";
30
33
 
@@ -38,10 +41,12 @@ var Hermes = /*#__PURE__*/function () {
38
41
  _defineProperty(this, "providerMap", new Map());
39
42
  _defineProperty(this, "walletMap", new Map());
40
43
  _defineProperty(this, "quoterAddressMap", new Map());
44
+ _defineProperty(this, "flashbotSigner", void 0);
41
45
  _defineProperty(this, "aggregatorAddressMap", new Map());
42
46
  _defineProperty(this, "executor", void 0);
43
47
  this.config = config;
44
48
  this.executor = config.executor;
49
+ this.flashbotSigner = new ethers.Wallet(config.flashbotSigner);
45
50
  if (!config.hermesSignalDomain || config.hermesSignalDomain === '') {
46
51
  // 默认 domain
47
52
  this.config.hermesSignalDomain = 'http://127.0.0.1:3002';
@@ -178,10 +183,79 @@ var Hermes = /*#__PURE__*/function () {
178
183
  }
179
184
  return expect;
180
185
  }()
186
+ }, {
187
+ key: "batchMultiExpect",
188
+ value: function () {
189
+ var _batchMultiExpect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
190
+ var provider, address, quoter, quoteParamsArr, amountOutListArr;
191
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
192
+ while (1) switch (_context3.prev = _context3.next) {
193
+ case 0:
194
+ provider = this.providerMap.get(params.chain);
195
+ if (provider) {
196
+ _context3.next = 3;
197
+ break;
198
+ }
199
+ throw new Error("Chain: ".concat(params.chain, " provider is undefined"));
200
+ case 3:
201
+ if (!(params.amountInWei.length !== params.path.length)) {
202
+ _context3.next = 5;
203
+ break;
204
+ }
205
+ throw new Error('amountInWei length must match path length');
206
+ case 5:
207
+ address = this.getQuoterAddress(params.chain);
208
+ if (address) {
209
+ _context3.next = 8;
210
+ break;
211
+ }
212
+ throw new Error("Quoter address not found for chain: ".concat(params.chain));
213
+ case 8:
214
+ quoter = new Contract(address, QuoterAbi, provider);
215
+ quoteParamsArr = params.path.map(function (path) {
216
+ return path.map(function (p) {
217
+ return {
218
+ dexType: p.dexType,
219
+ pool: p.poolAddress,
220
+ fromCoin: p.fromCoinAddress,
221
+ toCoin: p.toCoinAddress,
222
+ extra: p.extra || '0x'
223
+ };
224
+ });
225
+ });
226
+ _context3.next = 12;
227
+ return quoter.batchMultiQuote.staticCall(params.amountInWei, quoteParamsArr, {
228
+ from: this.config.executor
229
+ });
230
+ case 12:
231
+ amountOutListArr = _context3.sent;
232
+ if (amountOutListArr.length) {
233
+ _context3.next = 15;
234
+ break;
235
+ }
236
+ throw new Error('No expect result return from smart contract');
237
+ case 15:
238
+ return _context3.abrupt("return", amountOutListArr.map(function (list) {
239
+ if (!list.length) {
240
+ throw new Error('No expect result return from smart contract');
241
+ }
242
+ return list[list.length - 1];
243
+ }));
244
+ case 16:
245
+ case "end":
246
+ return _context3.stop();
247
+ }
248
+ }, _callee3, this);
249
+ }));
250
+ function batchMultiExpect(_x3) {
251
+ return _batchMultiExpect.apply(this, arguments);
252
+ }
253
+ return batchMultiExpect;
254
+ }()
181
255
  }, {
182
256
  key: "swap",
183
257
  value: function () {
184
- var _swap = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
258
+ var _swap = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
185
259
  var txReq,
186
260
  fromTokenAddress,
187
261
  toTokenAddress,
@@ -201,14 +275,14 @@ var Hermes = /*#__PURE__*/function () {
201
275
  _step,
202
276
  log,
203
277
  parsed,
204
- _args3 = arguments;
205
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
206
- while (1) switch (_context3.prev = _context3.next) {
278
+ _args4 = arguments;
279
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
280
+ while (1) switch (_context4.prev = _context4.next) {
207
281
  case 0:
208
- txReq = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {};
282
+ txReq = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {};
209
283
  this.validateParams(params);
210
284
  if (params.path.length) {
211
- _context3.next = 4;
285
+ _context4.next = 4;
212
286
  break;
213
287
  }
214
288
  throw new Error('Swap path not provided');
@@ -218,7 +292,7 @@ var Hermes = /*#__PURE__*/function () {
218
292
  aggregatorAddress = this.getAggregatorAddress(params.chain);
219
293
  wallet = this.walletMap.get(params.chain);
220
294
  if (wallet) {
221
- _context3.next = 10;
295
+ _context4.next = 10;
222
296
  break;
223
297
  }
224
298
  throw new Error("Wallet not configured for chain: ".concat(params.chain));
@@ -235,106 +309,506 @@ var Hermes = /*#__PURE__*/function () {
235
309
  };
236
310
  });
237
311
  _txReq = txReq, _ignore = _txReq.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq, _excluded);
238
- _context3.prev = 13;
239
- _context3.next = 16;
312
+ _context4.prev = 13;
313
+ _context4.next = 16;
240
314
  return aggregator.swap.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, estimationOverrides);
241
315
  case 16:
242
- estimateGas = _context3.sent;
243
- _context3.next = 23;
316
+ estimateGas = _context4.sent;
317
+ _context4.next = 23;
244
318
  break;
245
319
  case 19:
246
- _context3.prev = 19;
247
- _context3.t0 = _context3["catch"](13);
248
- console.warn('Aggregator estimateGas.swap failed', _context3.t0);
249
- throw _context3.t0;
320
+ _context4.prev = 19;
321
+ _context4.t0 = _context4["catch"](13);
322
+ console.warn('Aggregator estimateGas.swap failed', _context4.t0);
323
+ throw _context4.t0;
250
324
  case 23:
251
325
  txReq = this.resolveGasLimit(txReq, estimateGas);
252
- _context3.next = 26;
326
+ _context4.next = 26;
253
327
  return this.resolveNonce(wallet.provider, wallet.address, txReq);
254
328
  case 26:
255
- txReq = _context3.sent;
256
- _context3.next = 29;
329
+ txReq = _context4.sent;
330
+ _context4.next = 29;
257
331
  return this.resolvePricing(wallet.provider, txReq);
258
332
  case 29:
259
- txReq = _context3.sent;
260
- _context3.next = 32;
333
+ txReq = _context4.sent;
334
+ _context4.next = 32;
261
335
  return aggregator.swap(params.user, params.amountInWei, swapParams, params.minAmountOutList, txReq);
262
336
  case 32:
263
- txResponse = _context3.sent;
264
- _context3.next = 35;
337
+ txResponse = _context4.sent;
338
+ _context4.next = 35;
265
339
  return txResponse.wait();
266
340
  case 35:
267
- receipt = _context3.sent;
341
+ receipt = _context4.sent;
268
342
  iface = new ethers.Interface(AggregatorAbi);
269
343
  amountOut = null;
270
344
  _iterator = _createForOfIteratorHelper(receipt.logs);
271
- _context3.prev = 39;
345
+ _context4.prev = 39;
272
346
  _iterator.s();
273
347
  case 41:
274
348
  if ((_step = _iterator.n()).done) {
275
- _context3.next = 55;
349
+ _context4.next = 55;
276
350
  break;
277
351
  }
278
352
  log = _step.value;
279
353
  if (!(log.address.toLowerCase() === aggregatorAddress.toLowerCase())) {
280
- _context3.next = 53;
354
+ _context4.next = 53;
281
355
  break;
282
356
  }
283
- _context3.prev = 44;
357
+ _context4.prev = 44;
284
358
  parsed = iface.parseLog(log);
285
359
  if (!(parsed && parsed.name === 'Swapped' && parsed.args && parsed.args.amountOut !== undefined)) {
286
- _context3.next = 49;
360
+ _context4.next = 49;
287
361
  break;
288
362
  }
289
363
  amountOut = parsed.args.amountOut;
290
- return _context3.abrupt("break", 55);
364
+ return _context4.abrupt("break", 55);
291
365
  case 49:
292
- _context3.next = 53;
366
+ _context4.next = 53;
293
367
  break;
294
368
  case 51:
295
- _context3.prev = 51;
296
- _context3.t1 = _context3["catch"](44);
369
+ _context4.prev = 51;
370
+ _context4.t1 = _context4["catch"](44);
297
371
  case 53:
298
- _context3.next = 41;
372
+ _context4.next = 41;
299
373
  break;
300
374
  case 55:
301
- _context3.next = 60;
375
+ _context4.next = 60;
302
376
  break;
303
377
  case 57:
304
- _context3.prev = 57;
305
- _context3.t2 = _context3["catch"](39);
306
- _iterator.e(_context3.t2);
378
+ _context4.prev = 57;
379
+ _context4.t2 = _context4["catch"](39);
380
+ _iterator.e(_context4.t2);
307
381
  case 60:
308
- _context3.prev = 60;
382
+ _context4.prev = 60;
309
383
  _iterator.f();
310
- return _context3.finish(60);
384
+ return _context4.finish(60);
311
385
  case 63:
312
386
  if (amountOut) {
313
- _context3.next = 65;
387
+ _context4.next = 65;
314
388
  break;
315
389
  }
316
390
  throw new Error("Swapped event not found: ".concat(receipt.hash));
317
391
  case 65:
318
- return _context3.abrupt("return", {
392
+ return _context4.abrupt("return", {
319
393
  fromToken: fromTokenAddress,
320
394
  toToken: toTokenAddress,
321
395
  amountOut: amountOut,
322
396
  hash: receipt.hash,
323
397
  from: receipt.from,
324
- to: receipt.from,
325
- logs: receipt.logs
398
+ to: receipt.from
326
399
  });
327
400
  case 66:
328
401
  case "end":
329
- return _context3.stop();
402
+ return _context4.stop();
330
403
  }
331
- }, _callee3, this, [[13, 19], [39, 57, 60, 63], [44, 51]]);
404
+ }, _callee4, this, [[13, 19], [39, 57, 60, 63], [44, 51]]);
332
405
  }));
333
- function swap(_x3) {
406
+ function swap(_x4) {
334
407
  return _swap.apply(this, arguments);
335
408
  }
336
409
  return swap;
337
410
  }()
411
+ }, {
412
+ key: "batchMultiSwap",
413
+ value: function () {
414
+ var _batchMultiSwap = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(params) {
415
+ var _receipt$to;
416
+ var txReq,
417
+ provider,
418
+ wallet,
419
+ aggregatorAddress,
420
+ aggregator,
421
+ swapParamsList,
422
+ estimateGas,
423
+ _txReq2,
424
+ _ignore,
425
+ estimationOverrides,
426
+ txResponse,
427
+ receipt,
428
+ iface,
429
+ userList,
430
+ amountInList,
431
+ amountOutList,
432
+ _iterator2,
433
+ _step2,
434
+ log,
435
+ parsed,
436
+ _args5 = arguments;
437
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
438
+ while (1) switch (_context5.prev = _context5.next) {
439
+ case 0:
440
+ txReq = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
441
+ // 最基本的检查
442
+ provider = this.providerMap.get(params.chain);
443
+ wallet = this.walletMap.get(params.chain);
444
+ if (!(!wallet || !provider)) {
445
+ _context5.next = 5;
446
+ break;
447
+ }
448
+ throw new Error("sdk\u4E0D\u652F\u6301\u7684\u94FE\uFF1A ".concat(params.chain));
449
+ case 5:
450
+ if (!(params.amountInWeis.length !== params.paths.length)) {
451
+ _context5.next = 7;
452
+ break;
453
+ }
454
+ throw new Error('传入参数数量不正确');
455
+ case 7:
456
+ aggregatorAddress = this.getAggregatorAddress(params.chain);
457
+ aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
458
+ swapParamsList = params.paths.map(function (path) {
459
+ return path.map(function (pathItem) {
460
+ var _pathItem$extra2;
461
+ return {
462
+ dexType: pathItem.dexType,
463
+ pool: pathItem.poolAddress,
464
+ fromCoin: pathItem.fromCoinAddress,
465
+ toCoin: pathItem.toCoinAddress,
466
+ extra: (_pathItem$extra2 = pathItem.extra) !== null && _pathItem$extra2 !== void 0 ? _pathItem$extra2 : '0x'
467
+ };
468
+ });
469
+ });
470
+ _txReq2 = txReq, _ignore = _txReq2.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq2, _excluded2);
471
+ _context5.prev = 11;
472
+ _context5.next = 14;
473
+ return aggregator.batchMultiSwap.estimateGas(params.users, params.amountInWeis, swapParamsList, params.minAmountOutLists, estimationOverrides);
474
+ case 14:
475
+ estimateGas = _context5.sent;
476
+ _context5.next = 21;
477
+ break;
478
+ case 17:
479
+ _context5.prev = 17;
480
+ _context5.t0 = _context5["catch"](11);
481
+ console.warn('Aggregator estimateGas.swap failed', _context5.t0);
482
+ throw _context5.t0;
483
+ case 21:
484
+ txReq = this.resolveGasLimit(txReq, estimateGas);
485
+ _context5.next = 24;
486
+ return this.resolveNonce(wallet.provider, wallet.address, txReq);
487
+ case 24:
488
+ txReq = _context5.sent;
489
+ _context5.next = 27;
490
+ return this.resolvePricing(wallet.provider, txReq);
491
+ case 27:
492
+ txReq = _context5.sent;
493
+ _context5.next = 30;
494
+ return aggregator.batchMultiSwap(params.users, params.amountInWeis, swapParamsList, params.minAmountOutLists, estimationOverrides);
495
+ case 30:
496
+ txResponse = _context5.sent;
497
+ _context5.next = 33;
498
+ return txResponse.wait();
499
+ case 33:
500
+ receipt = _context5.sent;
501
+ iface = new ethers.Interface(AggregatorAbi);
502
+ userList = null;
503
+ amountInList = null;
504
+ amountOutList = null;
505
+ _iterator2 = _createForOfIteratorHelper(receipt.logs);
506
+ _context5.prev = 39;
507
+ _iterator2.s();
508
+ case 41:
509
+ if ((_step2 = _iterator2.n()).done) {
510
+ _context5.next = 57;
511
+ break;
512
+ }
513
+ log = _step2.value;
514
+ if (!(log.address.toLowerCase() === aggregatorAddress.toLowerCase())) {
515
+ _context5.next = 55;
516
+ break;
517
+ }
518
+ _context5.prev = 44;
519
+ parsed = iface.parseLog(log);
520
+ if (!(parsed && parsed.name === 'MultiSwapped' && parsed.args)) {
521
+ _context5.next = 51;
522
+ break;
523
+ }
524
+ userList = parsed.args.userList;
525
+ amountInList = parsed.args.amountInList;
526
+ amountOutList = parsed.args.amountOutList;
527
+ return _context5.abrupt("break", 57);
528
+ case 51:
529
+ _context5.next = 55;
530
+ break;
531
+ case 53:
532
+ _context5.prev = 53;
533
+ _context5.t1 = _context5["catch"](44);
534
+ case 55:
535
+ _context5.next = 41;
536
+ break;
537
+ case 57:
538
+ _context5.next = 62;
539
+ break;
540
+ case 59:
541
+ _context5.prev = 59;
542
+ _context5.t2 = _context5["catch"](39);
543
+ _iterator2.e(_context5.t2);
544
+ case 62:
545
+ _context5.prev = 62;
546
+ _iterator2.f();
547
+ return _context5.finish(62);
548
+ case 65:
549
+ if (!(!userList || !amountInList || !amountOutList)) {
550
+ _context5.next = 67;
551
+ break;
552
+ }
553
+ throw new Error("MultiSwapped event not found: ".concat(receipt.hash));
554
+ case 67:
555
+ if (!(userList.length !== params.users.length || amountOutList.length !== params.users.length)) {
556
+ _context5.next = 69;
557
+ break;
558
+ }
559
+ throw new Error("MultiSwapped event length mismatch: ".concat(receipt.hash));
560
+ case 69:
561
+ return _context5.abrupt("return", {
562
+ hash: receipt.hash,
563
+ from: receipt.from,
564
+ to: (_receipt$to = receipt.to) !== null && _receipt$to !== void 0 ? _receipt$to : null,
565
+ userResults: params.users.map(function (user, i) {
566
+ var _path$0$fromCoinAddre, _path$, _path$toCoinAddress, _path;
567
+ var path = params.paths[i];
568
+ var fromTokenAddress = (_path$0$fromCoinAddre = path === null || path === void 0 || (_path$ = path[0]) === null || _path$ === void 0 ? void 0 : _path$.fromCoinAddress) !== null && _path$0$fromCoinAddre !== void 0 ? _path$0$fromCoinAddre : '';
569
+ var toTokenAddress = (_path$toCoinAddress = path === null || path === void 0 || (_path = path[path.length - 1]) === null || _path === void 0 ? void 0 : _path.toCoinAddress) !== null && _path$toCoinAddress !== void 0 ? _path$toCoinAddress : '';
570
+ return {
571
+ user: user,
572
+ amountIn: amountInList[i],
573
+ amountOut: amountOutList[i],
574
+ fromToken: fromTokenAddress,
575
+ toToken: toTokenAddress
576
+ };
577
+ })
578
+ });
579
+ case 70:
580
+ case "end":
581
+ return _context5.stop();
582
+ }
583
+ }, _callee5, this, [[11, 17], [39, 59, 62, 65], [44, 53]]);
584
+ }));
585
+ function batchMultiSwap(_x5) {
586
+ return _batchMultiSwap.apply(this, arguments);
587
+ }
588
+ return batchMultiSwap;
589
+ }()
590
+ }, {
591
+ key: "swapByFlashbots",
592
+ value: function () {
593
+ var _swapByFlashbots = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(params) {
594
+ var txReq,
595
+ _targetBlockNumber,
596
+ provider,
597
+ wallet,
598
+ aggregatorAddress,
599
+ aggregator,
600
+ swapParams,
601
+ estimateGas,
602
+ _txReq3,
603
+ _ignore,
604
+ estimationOverrides,
605
+ swapCallData,
606
+ baseTx,
607
+ signTx,
608
+ rawTxs,
609
+ txHashes,
610
+ targetBlock,
611
+ resultPromise,
612
+ _args7 = arguments;
613
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
614
+ while (1) switch (_context7.prev = _context7.next) {
615
+ case 0:
616
+ txReq = _args7.length > 1 && _args7[1] !== undefined ? _args7[1] : {};
617
+ _targetBlockNumber = _args7.length > 2 ? _args7[2] : undefined;
618
+ provider = this.providerMap.get(params.chain);
619
+ wallet = this.walletMap.get(params.chain);
620
+ if (!(!wallet || !provider)) {
621
+ _context7.next = 6;
622
+ break;
623
+ }
624
+ throw new Error("sdk\u4E0D\u652F\u6301\u7684\u94FE\uFF1A ".concat(params.chain));
625
+ case 6:
626
+ aggregatorAddress = this.getAggregatorAddress(params.chain);
627
+ aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
628
+ swapParams = params.path.map(function (pathItem) {
629
+ var _pathItem$extra3;
630
+ return {
631
+ dexType: pathItem.dexType,
632
+ pool: pathItem.poolAddress,
633
+ fromCoin: pathItem.fromCoinAddress,
634
+ toCoin: pathItem.toCoinAddress,
635
+ extra: (_pathItem$extra3 = pathItem.extra) !== null && _pathItem$extra3 !== void 0 ? _pathItem$extra3 : '0x'
636
+ };
637
+ });
638
+ _txReq3 = txReq, _ignore = _txReq3.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq3, _excluded3);
639
+ _context7.prev = 10;
640
+ _context7.next = 13;
641
+ return aggregator.swap.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, estimationOverrides);
642
+ case 13:
643
+ estimateGas = _context7.sent;
644
+ _context7.next = 20;
645
+ break;
646
+ case 16:
647
+ _context7.prev = 16;
648
+ _context7.t0 = _context7["catch"](10);
649
+ console.warn('Aggregator estimateGas.swap failed', _context7.t0);
650
+ throw _context7.t0;
651
+ case 20:
652
+ txReq = this.resolveGasLimit(txReq, estimateGas);
653
+ _context7.next = 23;
654
+ return this.resolveNonce(wallet.provider, wallet.address, txReq);
655
+ case 23:
656
+ txReq = _context7.sent;
657
+ _context7.next = 26;
658
+ return this.resolvePricing(wallet.provider, txReq);
659
+ case 26:
660
+ txReq = _context7.sent;
661
+ this.validateParams(params);
662
+ swapCallData = this.genSwapCalldata(params);
663
+ baseTx = {
664
+ from: wallet.address,
665
+ to: swapCallData.to,
666
+ nonce: txReq.nonce,
667
+ value: ethers.parseEther('0'),
668
+ maxFeePerGas: txReq.maxFeePerGas,
669
+ maxPriorityFeePerGas: txReq.maxPriorityFeePerGas,
670
+ chainId: 1,
671
+ data: swapCallData.data,
672
+ gasLimit: txReq.gasLimit
673
+ };
674
+ _context7.next = 32;
675
+ return wallet.signTransaction(baseTx);
676
+ case 32:
677
+ signTx = _context7.sent;
678
+ rawTxs = [signTx];
679
+ txHashes = rawTxs.map(function (tx) {
680
+ return ethers.keccak256(tx);
681
+ });
682
+ txHashes.forEach(function (hash, i) {
683
+ console.log("Transaction ".concat(i + 1, ": ").concat(hash));
684
+ });
685
+ if (!_targetBlockNumber) {
686
+ _context7.next = 40;
687
+ break;
688
+ }
689
+ targetBlock = _targetBlockNumber;
690
+ _context7.next = 44;
691
+ break;
692
+ case 40:
693
+ _context7.next = 42;
694
+ return provider.getBlockNumber();
695
+ case 42:
696
+ _context7.t1 = _context7.sent;
697
+ targetBlock = _context7.t1 + 1;
698
+ case 44:
699
+ // 注册检查是否上链
700
+ resultPromise = new Promise(function (resolve) {
701
+ var onBlock = /*#__PURE__*/function () {
702
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(blockNumber) {
703
+ var _receipts$find;
704
+ var receipts, receipt, fromTokenAddress, toTokenAddress, iface, amountOut, _iterator3, _step3, log, parsed;
705
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
706
+ while (1) switch (_context6.prev = _context6.next) {
707
+ case 0:
708
+ if (!(blockNumber < targetBlock)) {
709
+ _context6.next = 2;
710
+ break;
711
+ }
712
+ return _context6.abrupt("return");
713
+ case 2:
714
+ provider.off('block', onBlock);
715
+ _context6.next = 5;
716
+ return Promise.all(txHashes.map(function (hash) {
717
+ return provider.getTransactionReceipt(hash);
718
+ }));
719
+ case 5:
720
+ receipts = _context6.sent;
721
+ receipt = (_receipts$find = receipts.find(function (r) {
722
+ return r && r.blockNumber === targetBlock;
723
+ })) !== null && _receipts$find !== void 0 ? _receipts$find : null;
724
+ if (receipt) {
725
+ _context6.next = 10;
726
+ break;
727
+ }
728
+ resolve(null);
729
+ return _context6.abrupt("return");
730
+ case 10:
731
+ fromTokenAddress = params.path[0].fromCoinAddress;
732
+ toTokenAddress = params.path[params.path.length - 1].toCoinAddress;
733
+ iface = new ethers.Interface(AggregatorAbi);
734
+ amountOut = null;
735
+ _iterator3 = _createForOfIteratorHelper(receipt.logs);
736
+ _context6.prev = 15;
737
+ _iterator3.s();
738
+ case 17:
739
+ if ((_step3 = _iterator3.n()).done) {
740
+ _context6.next = 31;
741
+ break;
742
+ }
743
+ log = _step3.value;
744
+ if (!(log.address.toLowerCase() === aggregatorAddress.toLowerCase())) {
745
+ _context6.next = 29;
746
+ break;
747
+ }
748
+ _context6.prev = 20;
749
+ parsed = iface.parseLog(log);
750
+ if (!(parsed && parsed.name === 'Swapped' && parsed.args && parsed.args.amountOut !== undefined)) {
751
+ _context6.next = 25;
752
+ break;
753
+ }
754
+ amountOut = parsed.args.amountOut;
755
+ return _context6.abrupt("break", 31);
756
+ case 25:
757
+ _context6.next = 29;
758
+ break;
759
+ case 27:
760
+ _context6.prev = 27;
761
+ _context6.t0 = _context6["catch"](20);
762
+ case 29:
763
+ _context6.next = 17;
764
+ break;
765
+ case 31:
766
+ _context6.next = 36;
767
+ break;
768
+ case 33:
769
+ _context6.prev = 33;
770
+ _context6.t1 = _context6["catch"](15);
771
+ _iterator3.e(_context6.t1);
772
+ case 36:
773
+ _context6.prev = 36;
774
+ _iterator3.f();
775
+ return _context6.finish(36);
776
+ case 39:
777
+ resolve({
778
+ fromToken: fromTokenAddress,
779
+ toToken: toTokenAddress,
780
+ amountOut: amountOut,
781
+ hash: receipt.hash,
782
+ from: receipt.from,
783
+ to: receipt.to
784
+ });
785
+ case 40:
786
+ case "end":
787
+ return _context6.stop();
788
+ }
789
+ }, _callee6, null, [[15, 33, 36, 39], [20, 27]]);
790
+ }));
791
+ return function onBlock(_x7) {
792
+ return _ref.apply(this, arguments);
793
+ };
794
+ }();
795
+ provider.on('block', onBlock);
796
+ });
797
+ _context7.next = 47;
798
+ return this.sendBundle(rawTxs, targetBlock, this.flashbotSigner);
799
+ case 47:
800
+ return _context7.abrupt("return", resultPromise);
801
+ case 48:
802
+ case "end":
803
+ return _context7.stop();
804
+ }
805
+ }, _callee7, this, [[10, 16]]);
806
+ }));
807
+ function swapByFlashbots(_x6) {
808
+ return _swapByFlashbots.apply(this, arguments);
809
+ }
810
+ return swapByFlashbots;
811
+ }()
338
812
  /**
339
813
  * 生成 swap 的 calldata
340
814
  */
@@ -349,13 +823,13 @@ var Hermes = /*#__PURE__*/function () {
349
823
 
350
824
  // 准备 swap 参数
351
825
  var swapParams = params.path.map(function (pathItem) {
352
- var _pathItem$extra2;
826
+ var _pathItem$extra4;
353
827
  return {
354
828
  dexType: pathItem.dexType,
355
829
  pool: pathItem.poolAddress,
356
830
  fromCoin: pathItem.fromCoinAddress,
357
831
  toCoin: pathItem.toCoinAddress,
358
- extra: (_pathItem$extra2 = pathItem.extra) !== null && _pathItem$extra2 !== void 0 ? _pathItem$extra2 : '0x'
832
+ extra: (_pathItem$extra4 = pathItem.extra) !== null && _pathItem$extra4 !== void 0 ? _pathItem$extra4 : '0x'
359
833
  };
360
834
  });
361
835
  var amountIn = params.amountInWei;
@@ -401,7 +875,7 @@ var Hermes = /*#__PURE__*/function () {
401
875
  }, {
402
876
  key: "bridge",
403
877
  value: function () {
404
- var _bridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
878
+ var _bridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(params) {
405
879
  var _params$extra2;
406
880
  var txReq,
407
881
  aggregatorAddress,
@@ -411,51 +885,51 @@ var Hermes = /*#__PURE__*/function () {
411
885
  aggregator,
412
886
  bridgeArgs,
413
887
  estimateGas,
414
- _txReq2,
888
+ _txReq4,
415
889
  _ignore,
416
890
  estimationOverrides,
417
891
  response,
418
892
  txReceipt,
419
- _args4 = arguments;
420
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
421
- while (1) switch (_context4.prev = _context4.next) {
893
+ _args8 = arguments;
894
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
895
+ while (1) switch (_context8.prev = _context8.next) {
422
896
  case 0:
423
- txReq = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {};
897
+ txReq = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
424
898
  this.validateParams(params);
425
899
 
426
900
  // set up
427
901
  aggregatorAddress = this.getAggregatorAddress(params.chain);
428
902
  wallet = this.walletMap.get(params.chain);
429
903
  if (wallet) {
430
- _context4.next = 6;
904
+ _context8.next = 6;
431
905
  break;
432
906
  }
433
907
  throw new Error("Wallet not configured for chain: ".concat(params.chain));
434
908
  case 6:
435
909
  if (!(params.tokenAddress && params.tokenAddress !== ethers.ZeroAddress)) {
436
- _context4.next = 9;
910
+ _context8.next = 9;
437
911
  break;
438
912
  }
439
- _context4.next = 9;
913
+ _context8.next = 9;
440
914
  return this.checkIsEnoughToken(params.tokenAddress, params.user, params.amountInWei, aggregatorAddress, wallet);
441
915
  case 9:
442
916
  if (!(params.bridgeFee > 0n)) {
443
- _context4.next = 18;
917
+ _context8.next = 18;
444
918
  break;
445
919
  }
446
920
  provider = this.providerMap.get(params.chain);
447
921
  if (provider) {
448
- _context4.next = 13;
922
+ _context8.next = 13;
449
923
  break;
450
924
  }
451
925
  throw new Error("Provider not configured for chain: ".concat(params.chain));
452
926
  case 13:
453
- _context4.next = 15;
927
+ _context8.next = 15;
454
928
  return provider.getBalance(params.user);
455
929
  case 15:
456
- nativeBalance = _context4.sent;
930
+ nativeBalance = _context8.sent;
457
931
  if (!(nativeBalance < params.bridgeFee)) {
458
- _context4.next = 18;
932
+ _context8.next = 18;
459
933
  break;
460
934
  }
461
935
  throw new Error('Insufficient native balance for bridge fee');
@@ -477,54 +951,53 @@ var Hermes = /*#__PURE__*/function () {
477
951
  }
478
952
 
479
953
  // simulate
480
- _txReq2 = txReq, _ignore = _txReq2.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq2, _excluded2);
481
- _context4.prev = 22;
482
- _context4.next = 25;
954
+ _txReq4 = txReq, _ignore = _txReq4.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq4, _excluded4);
955
+ _context8.prev = 22;
956
+ _context8.next = 25;
483
957
  return aggregator.bridge.estimateGas(params.user, bridgeArgs, estimationOverrides);
484
958
  case 25:
485
- estimateGas = _context4.sent;
486
- _context4.next = 32;
959
+ estimateGas = _context8.sent;
960
+ _context8.next = 32;
487
961
  break;
488
962
  case 28:
489
- _context4.prev = 28;
490
- _context4.t0 = _context4["catch"](22);
491
- console.error('Bridge gas estimation reverted', _context4.t0);
492
- throw _context4.t0;
963
+ _context8.prev = 28;
964
+ _context8.t0 = _context8["catch"](22);
965
+ console.error('Bridge gas estimation reverted', _context8.t0);
966
+ throw _context8.t0;
493
967
  case 32:
494
968
  // 构造tx req
495
969
  txReq = this.resolveGasLimit(txReq, estimateGas);
496
- _context4.next = 35;
970
+ _context8.next = 35;
497
971
  return this.resolveNonce(wallet.provider, wallet.address, txReq);
498
972
  case 35:
499
- txReq = _context4.sent;
500
- _context4.next = 38;
973
+ txReq = _context8.sent;
974
+ _context8.next = 38;
501
975
  return this.resolvePricing(wallet.provider, txReq);
502
976
  case 38:
503
- txReq = _context4.sent;
504
- _context4.next = 41;
977
+ txReq = _context8.sent;
978
+ _context8.next = 41;
505
979
  return aggregator.bridge(params.user, bridgeArgs, txReq);
506
980
  case 41:
507
- response = _context4.sent;
508
- _context4.next = 44;
981
+ response = _context8.sent;
982
+ _context8.next = 44;
509
983
  return response.wait();
510
984
  case 44:
511
- txReceipt = _context4.sent;
512
- return _context4.abrupt("return", {
985
+ txReceipt = _context8.sent;
986
+ return _context8.abrupt("return", {
513
987
  fromToken: params.tokenAddress,
514
988
  toToken: params.tokenAddress,
515
989
  amountOut: params.amountInWei,
516
990
  hash: txReceipt.hash,
517
991
  from: txReceipt.from,
518
- to: txReceipt.to,
519
- logs: txReceipt.logs
992
+ to: txReceipt.to
520
993
  });
521
994
  case 46:
522
995
  case "end":
523
- return _context4.stop();
996
+ return _context8.stop();
524
997
  }
525
- }, _callee4, this, [[22, 28]]);
998
+ }, _callee8, this, [[22, 28]]);
526
999
  }));
527
- function bridge(_x4) {
1000
+ function bridge(_x8) {
528
1001
  return _bridge.apply(this, arguments);
529
1002
  }
530
1003
  return bridge;
@@ -532,23 +1005,23 @@ var Hermes = /*#__PURE__*/function () {
532
1005
  }, {
533
1006
  key: "estimateBridgeFee",
534
1007
  value: function () {
535
- var _estimateBridgeFee = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(params) {
1008
+ var _estimateBridgeFee = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(params) {
536
1009
  var _params$extra3;
537
1010
  var wallet, address, quoter, bridgeArgs, bridgeFee;
538
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
539
- while (1) switch (_context5.prev = _context5.next) {
1011
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
1012
+ while (1) switch (_context9.prev = _context9.next) {
540
1013
  case 0:
541
1014
  this.validateParams(params);
542
1015
  wallet = this.walletMap.get(params.chain);
543
1016
  if (wallet) {
544
- _context5.next = 4;
1017
+ _context9.next = 4;
545
1018
  break;
546
1019
  }
547
1020
  throw new Error("Wallet not configured for chain: ".concat(params.chain));
548
1021
  case 4:
549
1022
  address = this.getQuoterAddress(params.chain);
550
1023
  if (address) {
551
- _context5.next = 7;
1024
+ _context9.next = 7;
552
1025
  break;
553
1026
  }
554
1027
  throw new Error("Quoter address not found for chain: ".concat(params.chain));
@@ -563,20 +1036,20 @@ var Hermes = /*#__PURE__*/function () {
563
1036
  destUser: params.destUser,
564
1037
  extra: (_params$extra3 = params.extra) !== null && _params$extra3 !== void 0 ? _params$extra3 : '0x'
565
1038
  };
566
- _context5.next = 11;
1039
+ _context9.next = 11;
567
1040
  return quoter.quoteBridge.staticCall(bridgeArgs, {
568
1041
  from: wallet.address
569
1042
  });
570
1043
  case 11:
571
- bridgeFee = _context5.sent;
572
- return _context5.abrupt("return", bridgeFee);
1044
+ bridgeFee = _context9.sent;
1045
+ return _context9.abrupt("return", bridgeFee);
573
1046
  case 13:
574
1047
  case "end":
575
- return _context5.stop();
1048
+ return _context9.stop();
576
1049
  }
577
- }, _callee5, this);
1050
+ }, _callee9, this);
578
1051
  }));
579
- function estimateBridgeFee(_x5) {
1052
+ function estimateBridgeFee(_x9) {
580
1053
  return _estimateBridgeFee.apply(this, arguments);
581
1054
  }
582
1055
  return estimateBridgeFee;
@@ -584,14 +1057,14 @@ var Hermes = /*#__PURE__*/function () {
584
1057
  }, {
585
1058
  key: "swapAndBridge",
586
1059
  value: function () {
587
- var _swapAndBridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(params) {
1060
+ var _swapAndBridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(params) {
588
1061
  var _params$extra4;
589
1062
  var txReq,
590
1063
  aggregatorAddress,
591
1064
  wallet,
592
1065
  swapParams,
593
1066
  bridgeArgs,
594
- _txReq3,
1067
+ _txReq5,
595
1068
  _ignore,
596
1069
  estimationOverrides,
597
1070
  aggregator,
@@ -600,35 +1073,35 @@ var Hermes = /*#__PURE__*/function () {
600
1073
  txReceipt,
601
1074
  iface,
602
1075
  outputAmountWei,
603
- _iterator2,
604
- _step2,
1076
+ _iterator4,
1077
+ _step4,
605
1078
  log,
606
1079
  parsed,
607
- _args6 = arguments;
608
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
609
- while (1) switch (_context6.prev = _context6.next) {
1080
+ _args10 = arguments;
1081
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
1082
+ while (1) switch (_context10.prev = _context10.next) {
610
1083
  case 0:
611
- txReq = _args6.length > 1 && _args6[1] !== undefined ? _args6[1] : {};
1084
+ txReq = _args10.length > 1 && _args10[1] !== undefined ? _args10[1] : {};
612
1085
  this.validateParams(params);
613
1086
 
614
1087
  // call the aggregator swap and bridge
615
1088
  aggregatorAddress = this.getAggregatorAddress(params.chain);
616
1089
  wallet = this.walletMap.get(params.chain);
617
1090
  if (wallet) {
618
- _context6.next = 6;
1091
+ _context10.next = 6;
619
1092
  break;
620
1093
  }
621
1094
  throw new Error("Wallet not configured for chain: ".concat(params.chain));
622
1095
  case 6:
623
1096
  // 准备合约参数
624
1097
  swapParams = params.path.map(function (pathItem) {
625
- var _pathItem$extra3;
1098
+ var _pathItem$extra5;
626
1099
  return {
627
1100
  dexType: pathItem.dexType,
628
1101
  pool: pathItem.poolAddress,
629
1102
  fromCoin: pathItem.fromCoinAddress,
630
1103
  toCoin: pathItem.toCoinAddress,
631
- extra: (_pathItem$extra3 = pathItem.extra) !== null && _pathItem$extra3 !== void 0 ? _pathItem$extra3 : '0x'
1104
+ extra: (_pathItem$extra5 = pathItem.extra) !== null && _pathItem$extra5 !== void 0 ? _pathItem$extra5 : '0x'
632
1105
  };
633
1106
  });
634
1107
  bridgeArgs = {
@@ -645,96 +1118,95 @@ var Hermes = /*#__PURE__*/function () {
645
1118
  value: params.bridgeFee
646
1119
  });
647
1120
  }
648
- _txReq3 = txReq, _ignore = _txReq3.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq3, _excluded3);
1121
+ _txReq5 = txReq, _ignore = _txReq5.gasLimit, estimationOverrides = _objectWithoutProperties(_txReq5, _excluded5);
649
1122
  aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
650
- _context6.next = 13;
1123
+ _context10.next = 13;
651
1124
  return aggregator.swapAndBridge.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, estimationOverrides);
652
1125
  case 13:
653
- estimateGas = _context6.sent;
1126
+ estimateGas = _context10.sent;
654
1127
  txReq = this.resolveGasLimit(txReq, estimateGas);
655
- _context6.next = 17;
1128
+ _context10.next = 17;
656
1129
  return this.resolveNonce(wallet.provider, wallet.address, txReq);
657
1130
  case 17:
658
- txReq = _context6.sent;
659
- _context6.next = 20;
1131
+ txReq = _context10.sent;
1132
+ _context10.next = 20;
660
1133
  return this.resolvePricing(wallet.provider, txReq);
661
1134
  case 20:
662
- txReq = _context6.sent;
663
- _context6.next = 23;
1135
+ txReq = _context10.sent;
1136
+ _context10.next = 23;
664
1137
  return aggregator.swapAndBridge(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txReq);
665
1138
  case 23:
666
- response = _context6.sent;
667
- _context6.next = 26;
1139
+ response = _context10.sent;
1140
+ _context10.next = 26;
668
1141
  return response.wait();
669
1142
  case 26:
670
- txReceipt = _context6.sent;
1143
+ txReceipt = _context10.sent;
671
1144
  // 解析 SwapAndBridge 事件
672
1145
  iface = new ethers.Interface(AggregatorAbi);
673
1146
  outputAmountWei = null;
674
- _iterator2 = _createForOfIteratorHelper(txReceipt.logs);
675
- _context6.prev = 30;
676
- _iterator2.s();
1147
+ _iterator4 = _createForOfIteratorHelper(txReceipt.logs);
1148
+ _context10.prev = 30;
1149
+ _iterator4.s();
677
1150
  case 32:
678
- if ((_step2 = _iterator2.n()).done) {
679
- _context6.next = 46;
1151
+ if ((_step4 = _iterator4.n()).done) {
1152
+ _context10.next = 46;
680
1153
  break;
681
1154
  }
682
- log = _step2.value;
1155
+ log = _step4.value;
683
1156
  if (!(log.address.toLowerCase() === aggregatorAddress.toLowerCase())) {
684
- _context6.next = 44;
1157
+ _context10.next = 44;
685
1158
  break;
686
1159
  }
687
- _context6.prev = 35;
1160
+ _context10.prev = 35;
688
1161
  parsed = iface.parseLog(log);
689
1162
  if (!(parsed && parsed.name === 'SwapAndBridge' && parsed.args && parsed.args.amountOut !== undefined)) {
690
- _context6.next = 40;
1163
+ _context10.next = 40;
691
1164
  break;
692
1165
  }
693
1166
  outputAmountWei = parsed.args.amountOut;
694
- return _context6.abrupt("break", 46);
1167
+ return _context10.abrupt("break", 46);
695
1168
  case 40:
696
- _context6.next = 44;
1169
+ _context10.next = 44;
697
1170
  break;
698
1171
  case 42:
699
- _context6.prev = 42;
700
- _context6.t0 = _context6["catch"](35);
1172
+ _context10.prev = 42;
1173
+ _context10.t0 = _context10["catch"](35);
701
1174
  case 44:
702
- _context6.next = 32;
1175
+ _context10.next = 32;
703
1176
  break;
704
1177
  case 46:
705
- _context6.next = 51;
1178
+ _context10.next = 51;
706
1179
  break;
707
1180
  case 48:
708
- _context6.prev = 48;
709
- _context6.t1 = _context6["catch"](30);
710
- _iterator2.e(_context6.t1);
1181
+ _context10.prev = 48;
1182
+ _context10.t1 = _context10["catch"](30);
1183
+ _iterator4.e(_context10.t1);
711
1184
  case 51:
712
- _context6.prev = 51;
713
- _iterator2.f();
714
- return _context6.finish(51);
1185
+ _context10.prev = 51;
1186
+ _iterator4.f();
1187
+ return _context10.finish(51);
715
1188
  case 54:
716
1189
  if (outputAmountWei) {
717
- _context6.next = 56;
1190
+ _context10.next = 56;
718
1191
  break;
719
1192
  }
720
1193
  throw new Error("SwapAndBridge event not found: ".concat(txReceipt.hash));
721
1194
  case 56:
722
- return _context6.abrupt("return", {
1195
+ return _context10.abrupt("return", {
723
1196
  fromToken: params.path[0].fromCoinAddress,
724
1197
  toToken: params.path[params.path.length - 1].toCoinAddress,
725
1198
  amountOut: outputAmountWei,
726
1199
  hash: txReceipt.hash,
727
1200
  from: txReceipt.from,
728
- to: txReceipt.to,
729
- logs: txReceipt.logs
1201
+ to: txReceipt.to
730
1202
  });
731
1203
  case 57:
732
1204
  case "end":
733
- return _context6.stop();
1205
+ return _context10.stop();
734
1206
  }
735
- }, _callee6, this, [[30, 48, 51, 54], [35, 42]]);
1207
+ }, _callee10, this, [[30, 48, 51, 54], [35, 42]]);
736
1208
  }));
737
- function swapAndBridge(_x6) {
1209
+ function swapAndBridge(_x10) {
738
1210
  return _swapAndBridge.apply(this, arguments);
739
1211
  }
740
1212
  return swapAndBridge;
@@ -742,11 +1214,11 @@ var Hermes = /*#__PURE__*/function () {
742
1214
  }, {
743
1215
  key: "estimateGas",
744
1216
  value: function () {
745
- var _estimateGas = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(estimateType, params) {
1217
+ var _estimateGas = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(estimateType, params) {
746
1218
  var _params$extra5, _swapAndBridgeParams$;
747
1219
  var aggregatorAddress, wallet, aggregator, bridgeArgs1, txOverrides1, gasInBridge, swapParams1, swapGas, swapAndBridgeParams, swapParams, bridgeArgs, txOverrides, gas;
748
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
749
- while (1) switch (_context7.prev = _context7.next) {
1220
+ return _regeneratorRuntime().wrap(function _callee11$(_context11) {
1221
+ while (1) switch (_context11.prev = _context11.next) {
750
1222
  case 0:
751
1223
  this.validateParams(params);
752
1224
 
@@ -755,17 +1227,17 @@ var Hermes = /*#__PURE__*/function () {
755
1227
  wallet = this.walletMap.get(params.chain);
756
1228
  aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
757
1229
  if (wallet) {
758
- _context7.next = 6;
1230
+ _context11.next = 6;
759
1231
  break;
760
1232
  }
761
1233
  throw new Error("Wallet not configured for chain: ".concat(params.chain));
762
1234
  case 6:
763
- _context7.t0 = estimateType;
764
- _context7.next = _context7.t0 === IEstimateType.BRIDGE ? 9 : _context7.t0 === IEstimateType.SWAP ? 17 : _context7.t0 === IEstimateType.SWAPANDBRIDGE ? 26 : 36;
1235
+ _context11.t0 = estimateType;
1236
+ _context11.next = _context11.t0 === IEstimateType.BRIDGE ? 9 : _context11.t0 === IEstimateType.SWAP ? 17 : _context11.t0 === IEstimateType.SWAPANDBRIDGE ? 26 : 36;
765
1237
  break;
766
1238
  case 9:
767
1239
  if (!(!('bridgeType' in params) || !('destChain' in params))) {
768
- _context7.next = 11;
1240
+ _context11.next = 11;
769
1241
  break;
770
1242
  }
771
1243
  throw new Error('bridge params required');
@@ -783,57 +1255,57 @@ var Hermes = /*#__PURE__*/function () {
783
1255
  from: wallet.address,
784
1256
  value: params.bridgeFee
785
1257
  };
786
- _context7.next = 15;
1258
+ _context11.next = 15;
787
1259
  return aggregator.bridge.estimateGas(params.user, bridgeArgs1, txOverrides1);
788
1260
  case 15:
789
- gasInBridge = _context7.sent;
790
- return _context7.abrupt("return", gasInBridge);
1261
+ gasInBridge = _context11.sent;
1262
+ return _context11.abrupt("return", gasInBridge);
791
1263
  case 17:
792
1264
  if (!(!('path' in params) || !Array.isArray(params.path) || params.path.length === 0)) {
793
- _context7.next = 19;
1265
+ _context11.next = 19;
794
1266
  break;
795
1267
  }
796
1268
  throw new Error('Swap path required for gas estimation');
797
1269
  case 19:
798
1270
  if (!(!('minAmountOutList' in params) || params.minAmountOutList.length === 0)) {
799
- _context7.next = 21;
1271
+ _context11.next = 21;
800
1272
  break;
801
1273
  }
802
1274
  throw new Error('minAmountOutList required for gas estimation');
803
1275
  case 21:
804
1276
  swapParams1 = params.path.map(function (pathItem) {
805
- var _pathItem$extra4;
1277
+ var _pathItem$extra6;
806
1278
  return {
807
1279
  dexType: pathItem.dexType,
808
1280
  pool: pathItem.poolAddress,
809
1281
  fromCoin: pathItem.fromCoinAddress,
810
1282
  toCoin: pathItem.toCoinAddress,
811
- extra: (_pathItem$extra4 = pathItem.extra) !== null && _pathItem$extra4 !== void 0 ? _pathItem$extra4 : '0x'
1283
+ extra: (_pathItem$extra6 = pathItem.extra) !== null && _pathItem$extra6 !== void 0 ? _pathItem$extra6 : '0x'
812
1284
  };
813
1285
  });
814
- _context7.next = 24;
1286
+ _context11.next = 24;
815
1287
  return aggregator.swap.estimateGas(params.user, params.amountInWei, swapParams1, params.minAmountOutList, {
816
1288
  from: wallet.address
817
1289
  });
818
1290
  case 24:
819
- swapGas = _context7.sent;
820
- return _context7.abrupt("return", swapGas);
1291
+ swapGas = _context11.sent;
1292
+ return _context11.abrupt("return", swapGas);
821
1293
  case 26:
822
1294
  if (!(!('path' in params) || !('bridgeType' in params))) {
823
- _context7.next = 28;
1295
+ _context11.next = 28;
824
1296
  break;
825
1297
  }
826
1298
  throw new Error('swapAndBridge params required');
827
1299
  case 28:
828
1300
  swapAndBridgeParams = params;
829
1301
  swapParams = swapAndBridgeParams.path.map(function (pathItem) {
830
- var _pathItem$extra5;
1302
+ var _pathItem$extra7;
831
1303
  return {
832
1304
  dexType: pathItem.dexType,
833
1305
  pool: pathItem.poolAddress,
834
1306
  fromCoin: pathItem.fromCoinAddress,
835
1307
  toCoin: pathItem.toCoinAddress,
836
- extra: (_pathItem$extra5 = pathItem.extra) !== null && _pathItem$extra5 !== void 0 ? _pathItem$extra5 : '0x'
1308
+ extra: (_pathItem$extra7 = pathItem.extra) !== null && _pathItem$extra7 !== void 0 ? _pathItem$extra7 : '0x'
837
1309
  };
838
1310
  });
839
1311
  bridgeArgs = {
@@ -849,20 +1321,20 @@ var Hermes = /*#__PURE__*/function () {
849
1321
  from: wallet.address,
850
1322
  value: swapAndBridgeParams.bridgeFee
851
1323
  };
852
- _context7.next = 34;
1324
+ _context11.next = 34;
853
1325
  return aggregator.swapAndBridge.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txOverrides);
854
1326
  case 34:
855
- gas = _context7.sent;
856
- return _context7.abrupt("return", gas);
1327
+ gas = _context11.sent;
1328
+ return _context11.abrupt("return", gas);
857
1329
  case 36:
858
1330
  throw 'Not support method in sdk';
859
1331
  case 37:
860
1332
  case "end":
861
- return _context7.stop();
1333
+ return _context11.stop();
862
1334
  }
863
- }, _callee7, this);
1335
+ }, _callee11, this);
864
1336
  }));
865
- function estimateGas(_x7, _x8) {
1337
+ function estimateGas(_x11, _x12) {
866
1338
  return _estimateGas.apply(this, arguments);
867
1339
  }
868
1340
  return estimateGas;
@@ -870,68 +1342,68 @@ var Hermes = /*#__PURE__*/function () {
870
1342
  }, {
871
1343
  key: "getAggregatorSupportContracts",
872
1344
  value: function () {
873
- var _getAggregatorSupportContracts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(chain) {
1345
+ var _getAggregatorSupportContracts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(chain) {
874
1346
  var aggregatorAddress, provider, aggregator, addressList, supportContracts;
875
- return _regeneratorRuntime().wrap(function _callee9$(_context9) {
876
- while (1) switch (_context9.prev = _context9.next) {
1347
+ return _regeneratorRuntime().wrap(function _callee13$(_context13) {
1348
+ while (1) switch (_context13.prev = _context13.next) {
877
1349
  case 0:
878
1350
  // 返回传入链支持的dex和桥
879
1351
  // 返回dex type数组
880
1352
  aggregatorAddress = this.getAggregatorAddress(chain);
881
1353
  provider = this.providerMap.get(chain);
882
1354
  if (provider) {
883
- _context9.next = 4;
1355
+ _context13.next = 4;
884
1356
  break;
885
1357
  }
886
1358
  throw new Error("Provider not configured for chain: ".concat(chain));
887
1359
  case 4:
888
1360
  aggregator = new Contract(aggregatorAddress, AggregatorAbi, provider);
889
- _context9.next = 7;
1361
+ _context13.next = 7;
890
1362
  return aggregator.getAddressList();
891
1363
  case 7:
892
- addressList = _context9.sent;
1364
+ addressList = _context13.sent;
893
1365
  if (addressList.length) {
894
- _context9.next = 10;
1366
+ _context13.next = 10;
895
1367
  break;
896
1368
  }
897
- return _context9.abrupt("return", []);
1369
+ return _context13.abrupt("return", []);
898
1370
  case 10:
899
- _context9.next = 12;
1371
+ _context13.next = 12;
900
1372
  return Promise.all(addressList.map( /*#__PURE__*/function () {
901
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(contractAddress, index) {
1373
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(contractAddress, index) {
902
1374
  var _yield$aggregator$con, contractType;
903
- return _regeneratorRuntime().wrap(function _callee8$(_context8) {
904
- while (1) switch (_context8.prev = _context8.next) {
1375
+ return _regeneratorRuntime().wrap(function _callee12$(_context12) {
1376
+ while (1) switch (_context12.prev = _context12.next) {
905
1377
  case 0:
906
- _context8.next = 2;
1378
+ _context12.next = 2;
907
1379
  return aggregator.contractList(index);
908
1380
  case 2:
909
- _yield$aggregator$con = _context8.sent;
1381
+ _yield$aggregator$con = _context12.sent;
910
1382
  contractType = _yield$aggregator$con.contractType;
911
- return _context8.abrupt("return", {
1383
+ return _context12.abrupt("return", {
912
1384
  contractType: contractType,
913
1385
  contractAddress: contractAddress
914
1386
  });
915
1387
  case 5:
916
1388
  case "end":
917
- return _context8.stop();
1389
+ return _context12.stop();
918
1390
  }
919
- }, _callee8);
1391
+ }, _callee12);
920
1392
  }));
921
- return function (_x10, _x11) {
922
- return _ref.apply(this, arguments);
1393
+ return function (_x14, _x15) {
1394
+ return _ref2.apply(this, arguments);
923
1395
  };
924
1396
  }()));
925
1397
  case 12:
926
- supportContracts = _context9.sent;
927
- return _context9.abrupt("return", supportContracts);
1398
+ supportContracts = _context13.sent;
1399
+ return _context13.abrupt("return", supportContracts);
928
1400
  case 14:
929
1401
  case "end":
930
- return _context9.stop();
1402
+ return _context13.stop();
931
1403
  }
932
- }, _callee9, this);
1404
+ }, _callee13, this);
933
1405
  }));
934
- function getAggregatorSupportContracts(_x9) {
1406
+ function getAggregatorSupportContracts(_x13) {
935
1407
  return _getAggregatorSupportContracts.apply(this, arguments);
936
1408
  }
937
1409
  return getAggregatorSupportContracts;
@@ -939,68 +1411,68 @@ var Hermes = /*#__PURE__*/function () {
939
1411
  }, {
940
1412
  key: "getQuoterSupportContracts",
941
1413
  value: function () {
942
- var _getQuoterSupportContracts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(chain) {
1414
+ var _getQuoterSupportContracts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15(chain) {
943
1415
  var quoterAddress, provider, quoter, addressList, supportContracts;
944
- return _regeneratorRuntime().wrap(function _callee11$(_context11) {
945
- while (1) switch (_context11.prev = _context11.next) {
1416
+ return _regeneratorRuntime().wrap(function _callee15$(_context15) {
1417
+ while (1) switch (_context15.prev = _context15.next) {
946
1418
  case 0:
947
1419
  // 返回传入链支持的dex和桥
948
1420
  // 返回dex type数组
949
1421
  quoterAddress = this.getQuoterAddress(chain);
950
1422
  provider = this.providerMap.get(chain);
951
1423
  if (provider) {
952
- _context11.next = 4;
1424
+ _context15.next = 4;
953
1425
  break;
954
1426
  }
955
1427
  throw new Error("Provider not configured for chain: ".concat(chain));
956
1428
  case 4:
957
1429
  quoter = new Contract(quoterAddress, QuoterAbi, provider);
958
- _context11.next = 7;
1430
+ _context15.next = 7;
959
1431
  return quoter.getAddressList();
960
1432
  case 7:
961
- addressList = _context11.sent;
1433
+ addressList = _context15.sent;
962
1434
  if (addressList.length) {
963
- _context11.next = 10;
1435
+ _context15.next = 10;
964
1436
  break;
965
1437
  }
966
- return _context11.abrupt("return", []);
1438
+ return _context15.abrupt("return", []);
967
1439
  case 10:
968
- _context11.next = 12;
1440
+ _context15.next = 12;
969
1441
  return Promise.all(addressList.map( /*#__PURE__*/function () {
970
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(contractAddress, index) {
1442
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14(contractAddress, index) {
971
1443
  var _yield$quoter$contrac, contractType;
972
- return _regeneratorRuntime().wrap(function _callee10$(_context10) {
973
- while (1) switch (_context10.prev = _context10.next) {
1444
+ return _regeneratorRuntime().wrap(function _callee14$(_context14) {
1445
+ while (1) switch (_context14.prev = _context14.next) {
974
1446
  case 0:
975
- _context10.next = 2;
1447
+ _context14.next = 2;
976
1448
  return quoter.contractList(index);
977
1449
  case 2:
978
- _yield$quoter$contrac = _context10.sent;
1450
+ _yield$quoter$contrac = _context14.sent;
979
1451
  contractType = _yield$quoter$contrac.contractType;
980
- return _context10.abrupt("return", {
1452
+ return _context14.abrupt("return", {
981
1453
  contractType: contractType,
982
1454
  contractAddress: contractAddress
983
1455
  });
984
1456
  case 5:
985
1457
  case "end":
986
- return _context10.stop();
1458
+ return _context14.stop();
987
1459
  }
988
- }, _callee10);
1460
+ }, _callee14);
989
1461
  }));
990
- return function (_x13, _x14) {
991
- return _ref2.apply(this, arguments);
1462
+ return function (_x17, _x18) {
1463
+ return _ref3.apply(this, arguments);
992
1464
  };
993
1465
  }()));
994
1466
  case 12:
995
- supportContracts = _context11.sent;
996
- return _context11.abrupt("return", supportContracts);
1467
+ supportContracts = _context15.sent;
1468
+ return _context15.abrupt("return", supportContracts);
997
1469
  case 14:
998
1470
  case "end":
999
- return _context11.stop();
1471
+ return _context15.stop();
1000
1472
  }
1001
- }, _callee11, this);
1473
+ }, _callee15, this);
1002
1474
  }));
1003
- function getQuoterSupportContracts(_x12) {
1475
+ function getQuoterSupportContracts(_x16) {
1004
1476
  return _getQuoterSupportContracts.apply(this, arguments);
1005
1477
  }
1006
1478
  return getQuoterSupportContracts;
@@ -1020,13 +1492,13 @@ var Hermes = /*#__PURE__*/function () {
1020
1492
 
1021
1493
  // 准备 swap 参数
1022
1494
  var swapParams = params.path.map(function (pathItem) {
1023
- var _pathItem$extra6;
1495
+ var _pathItem$extra8;
1024
1496
  return {
1025
1497
  dexType: pathItem.dexType,
1026
1498
  pool: pathItem.poolAddress,
1027
1499
  fromCoin: pathItem.fromCoinAddress,
1028
1500
  toCoin: pathItem.toCoinAddress,
1029
- extra: (_pathItem$extra6 = pathItem.extra) !== null && _pathItem$extra6 !== void 0 ? _pathItem$extra6 : '0x'
1501
+ extra: (_pathItem$extra8 = pathItem.extra) !== null && _pathItem$extra8 !== void 0 ? _pathItem$extra8 : '0x'
1030
1502
  };
1031
1503
  });
1032
1504
 
@@ -1062,34 +1534,116 @@ var Hermes = /*#__PURE__*/function () {
1062
1534
  gasLimit: estimateGas * 130n / 100n // 上浮30%
1063
1535
  });
1064
1536
  }
1537
+ }, {
1538
+ key: "sendBundle",
1539
+ value: function () {
1540
+ var _sendBundle = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17(rawTxs, targetBlock, signer) {
1541
+ var body, signature, headers, BUILDERS, builderEntries, count, promises;
1542
+ return _regeneratorRuntime().wrap(function _callee17$(_context17) {
1543
+ while (1) switch (_context17.prev = _context17.next) {
1544
+ case 0:
1545
+ console.log("\n--- Sending Bundle for Block ".concat(targetBlock, " ---"));
1546
+ body = {
1547
+ jsonrpc: '2.0',
1548
+ id: 1,
1549
+ method: 'eth_sendBundle',
1550
+ params: [{
1551
+ txs: rawTxs,
1552
+ blockNumber: "0x".concat(targetBlock.toString(16))
1553
+ }]
1554
+ };
1555
+ _context17.next = 4;
1556
+ return this.signRequestBody(body, signer);
1557
+ case 4:
1558
+ signature = _context17.sent;
1559
+ headers = {
1560
+ 'Content-Type': 'application/json',
1561
+ 'X-Flashbots-Signature': signature
1562
+ };
1563
+ BUILDERS = {
1564
+ 'beaverbuild.org': 'https://rpc.beaverbuild.org',
1565
+ Titan: 'https://rpc.titanbuilder.xyz',
1566
+ flashbots: 'https://relay.flashbots.net',
1567
+ // For simulation and sending
1568
+ bloXroute: 'rpc-builder.blxrbdn.com'
1569
+ };
1570
+ builderEntries = Object.entries(BUILDERS);
1571
+ count = 0;
1572
+ promises = builderEntries.map( /*#__PURE__*/function () {
1573
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(_ref4) {
1574
+ var _ref6, name, url, response, _error$message, msg;
1575
+ return _regeneratorRuntime().wrap(function _callee16$(_context16) {
1576
+ while (1) switch (_context16.prev = _context16.next) {
1577
+ case 0:
1578
+ _ref6 = _slicedToArray(_ref4, 2), name = _ref6[0], url = _ref6[1];
1579
+ _context16.prev = 1;
1580
+ _context16.next = 4;
1581
+ return axios.post(url, body, {
1582
+ headers: headers
1583
+ });
1584
+ case 4:
1585
+ response = _context16.sent;
1586
+ count++;
1587
+ console.log("[".concat(count, "/").concat(builderEntries.length, "] Sent to ").concat(name, ":"), response.data);
1588
+ _context16.next = 14;
1589
+ break;
1590
+ case 9:
1591
+ _context16.prev = 9;
1592
+ _context16.t0 = _context16["catch"](1);
1593
+ count++;
1594
+ msg = _context16.t0 !== null && _context16.t0 !== void 0 && _context16.t0.response ? _context16.t0.response.data : (_error$message = _context16.t0 === null || _context16.t0 === void 0 ? void 0 : _context16.t0.message) !== null && _error$message !== void 0 ? _error$message : _context16.t0;
1595
+ console.error("[".concat(count, "/").concat(builderEntries.length, "] Error sending to ").concat(name, ":"), msg);
1596
+ case 14:
1597
+ case "end":
1598
+ return _context16.stop();
1599
+ }
1600
+ }, _callee16, null, [[1, 9]]);
1601
+ }));
1602
+ return function (_x22) {
1603
+ return _ref5.apply(this, arguments);
1604
+ };
1605
+ }());
1606
+ _context17.next = 12;
1607
+ return Promise.all(promises);
1608
+ case 12:
1609
+ case "end":
1610
+ return _context17.stop();
1611
+ }
1612
+ }, _callee17, this);
1613
+ }));
1614
+ function sendBundle(_x19, _x20, _x21) {
1615
+ return _sendBundle.apply(this, arguments);
1616
+ }
1617
+ return sendBundle;
1618
+ }()
1065
1619
  }, {
1066
1620
  key: "resolveNonce",
1067
1621
  value: function () {
1068
- var _resolveNonce = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(provider, from, tx) {
1622
+ var _resolveNonce = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18(provider, from, tx) {
1069
1623
  var nonce;
1070
- return _regeneratorRuntime().wrap(function _callee12$(_context12) {
1071
- while (1) switch (_context12.prev = _context12.next) {
1624
+ return _regeneratorRuntime().wrap(function _callee18$(_context18) {
1625
+ while (1) switch (_context18.prev = _context18.next) {
1072
1626
  case 0:
1073
1627
  if (!(tx.nonce != null)) {
1074
- _context12.next = 2;
1628
+ _context18.next = 2;
1075
1629
  break;
1076
1630
  }
1077
- return _context12.abrupt("return", tx);
1631
+ return _context18.abrupt("return", tx);
1078
1632
  case 2:
1079
- _context12.next = 4;
1633
+ _context18.next = 4;
1080
1634
  return provider.getTransactionCount(from, 'latest');
1081
1635
  case 4:
1082
- nonce = _context12.sent;
1083
- return _context12.abrupt("return", _objectSpread(_objectSpread({}, tx), {}, {
1636
+ nonce = _context18.sent;
1637
+ return _context18.abrupt("return", _objectSpread(_objectSpread({}, tx), {}, {
1084
1638
  nonce: nonce
1085
1639
  }));
1086
1640
  case 6:
1087
1641
  case "end":
1088
- return _context12.stop();
1642
+ return _context18.stop();
1089
1643
  }
1090
- }, _callee12);
1644
+ }, _callee18);
1091
1645
  }));
1092
- function resolveNonce(_x15, _x16, _x17) {
1646
+ function resolveNonce(_x23, _x24, _x25) {
1093
1647
  return _resolveNonce.apply(this, arguments);
1094
1648
  }
1095
1649
  return resolveNonce;
@@ -1097,58 +1651,85 @@ var Hermes = /*#__PURE__*/function () {
1097
1651
  }, {
1098
1652
  key: "resolvePricing",
1099
1653
  value: function () {
1100
- var _resolvePricing = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(provider, tx) {
1654
+ var _resolvePricing = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19(provider, tx) {
1101
1655
  var fd;
1102
- return _regeneratorRuntime().wrap(function _callee13$(_context13) {
1103
- while (1) switch (_context13.prev = _context13.next) {
1656
+ return _regeneratorRuntime().wrap(function _callee19$(_context19) {
1657
+ while (1) switch (_context19.prev = _context19.next) {
1104
1658
  case 0:
1105
1659
  if (!(tx.gasPrice != null || tx.maxFeePerGas != null || tx.maxPriorityFeePerGas != null)) {
1106
- _context13.next = 2;
1660
+ _context19.next = 2;
1107
1661
  break;
1108
1662
  }
1109
- return _context13.abrupt("return", this.sanitizePricing(tx));
1663
+ return _context19.abrupt("return", this.sanitizePricing(tx));
1110
1664
  case 2:
1111
- _context13.next = 4;
1665
+ _context19.next = 4;
1112
1666
  return provider.getFeeData();
1113
1667
  case 4:
1114
- fd = _context13.sent;
1668
+ fd = _context19.sent;
1115
1669
  if (!(fd.maxFeePerGas != null && fd.maxPriorityFeePerGas != null)) {
1116
- _context13.next = 7;
1670
+ _context19.next = 7;
1117
1671
  break;
1118
1672
  }
1119
- return _context13.abrupt("return", _objectSpread(_objectSpread({}, tx), {}, {
1673
+ return _context19.abrupt("return", _objectSpread(_objectSpread({}, tx), {}, {
1120
1674
  maxFeePerGas: fd.maxFeePerGas * 150n / 100n,
1121
1675
  // 上浮50%
1122
1676
  maxPriorityFeePerGas: fd.maxPriorityFeePerGas
1123
1677
  }));
1124
1678
  case 7:
1125
1679
  if (!(fd.gasPrice != null)) {
1126
- _context13.next = 9;
1680
+ _context19.next = 9;
1127
1681
  break;
1128
1682
  }
1129
- return _context13.abrupt("return", _objectSpread(_objectSpread({}, tx), {}, {
1683
+ return _context19.abrupt("return", _objectSpread(_objectSpread({}, tx), {}, {
1130
1684
  gasPrice: fd.gasPrice
1131
1685
  }));
1132
1686
  case 9:
1133
- return _context13.abrupt("return", tx);
1687
+ return _context19.abrupt("return", tx);
1134
1688
  case 10:
1135
1689
  case "end":
1136
- return _context13.stop();
1690
+ return _context19.stop();
1137
1691
  }
1138
- }, _callee13, this);
1692
+ }, _callee19, this);
1139
1693
  }));
1140
- function resolvePricing(_x18, _x19) {
1694
+ function resolvePricing(_x26, _x27) {
1141
1695
  return _resolvePricing.apply(this, arguments);
1142
1696
  }
1143
1697
  return resolvePricing;
1144
1698
  }()
1699
+ }, {
1700
+ key: "signRequestBody",
1701
+ value: function () {
1702
+ var _signRequestBody = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20(body, signer) {
1703
+ var bodyJson, encoder, requestBodyHash, signature;
1704
+ return _regeneratorRuntime().wrap(function _callee20$(_context20) {
1705
+ while (1) switch (_context20.prev = _context20.next) {
1706
+ case 0:
1707
+ bodyJson = JSON.stringify(body);
1708
+ encoder = new TextEncoder();
1709
+ requestBodyHash = ethers.keccak256(encoder.encode(bodyJson));
1710
+ _context20.next = 5;
1711
+ return signer.signMessage(requestBodyHash);
1712
+ case 5:
1713
+ signature = _context20.sent;
1714
+ return _context20.abrupt("return", "".concat(signer.address, ":").concat(signature));
1715
+ case 7:
1716
+ case "end":
1717
+ return _context20.stop();
1718
+ }
1719
+ }, _callee20);
1720
+ }));
1721
+ function signRequestBody(_x28, _x29) {
1722
+ return _signRequestBody.apply(this, arguments);
1723
+ }
1724
+ return signRequestBody;
1725
+ }()
1145
1726
  }, {
1146
1727
  key: "sanitizePricing",
1147
1728
  value: function sanitizePricing(tx) {
1148
1729
  // If user supplied any EIP-1559 field, remove legacy gasPrice to avoid mixing.
1149
1730
  if (tx.maxFeePerGas != null || tx.maxPriorityFeePerGas != null) {
1150
1731
  var gasPrice = tx.gasPrice,
1151
- rest = _objectWithoutProperties(tx, _excluded4);
1732
+ rest = _objectWithoutProperties(tx, _excluded6);
1152
1733
  return rest;
1153
1734
  }
1154
1735
  return tx;
@@ -1156,38 +1737,38 @@ var Hermes = /*#__PURE__*/function () {
1156
1737
  }, {
1157
1738
  key: "checkIsEnoughToken",
1158
1739
  value: function () {
1159
- var _checkIsEnoughToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14(fromTokenAddress, userAddress, amountInWei, aggregatorAddress, wallet) {
1740
+ var _checkIsEnoughToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21(fromTokenAddress, userAddress, amountInWei, aggregatorAddress, wallet) {
1160
1741
  var erc20, userBalance, currentAllowance;
1161
- return _regeneratorRuntime().wrap(function _callee14$(_context14) {
1162
- while (1) switch (_context14.prev = _context14.next) {
1742
+ return _regeneratorRuntime().wrap(function _callee21$(_context21) {
1743
+ while (1) switch (_context21.prev = _context21.next) {
1163
1744
  case 0:
1164
1745
  erc20 = new Contract(fromTokenAddress, ['function balanceOf(address) view returns (uint256)', 'function allowance(address, address) view returns (uint256)'], wallet);
1165
- _context14.next = 3;
1746
+ _context21.next = 3;
1166
1747
  return erc20.balanceOf(userAddress);
1167
1748
  case 3:
1168
- userBalance = _context14.sent;
1749
+ userBalance = _context21.sent;
1169
1750
  if (!(userBalance < amountInWei)) {
1170
- _context14.next = 6;
1751
+ _context21.next = 6;
1171
1752
  break;
1172
1753
  }
1173
1754
  throw new Error('Insufficient balance token amount');
1174
1755
  case 6:
1175
- _context14.next = 8;
1756
+ _context21.next = 8;
1176
1757
  return erc20.allowance(userAddress, aggregatorAddress);
1177
1758
  case 8:
1178
- currentAllowance = _context14.sent;
1759
+ currentAllowance = _context21.sent;
1179
1760
  if (!(currentAllowance < amountInWei)) {
1180
- _context14.next = 11;
1761
+ _context21.next = 11;
1181
1762
  break;
1182
1763
  }
1183
1764
  throw new Error('Insufficient allowance token amount');
1184
1765
  case 11:
1185
1766
  case "end":
1186
- return _context14.stop();
1767
+ return _context21.stop();
1187
1768
  }
1188
- }, _callee14);
1769
+ }, _callee21);
1189
1770
  }));
1190
- function checkIsEnoughToken(_x20, _x21, _x22, _x23, _x24) {
1771
+ function checkIsEnoughToken(_x30, _x31, _x32, _x33, _x34) {
1191
1772
  return _checkIsEnoughToken.apply(this, arguments);
1192
1773
  }
1193
1774
  return checkIsEnoughToken;