hermes-swap 0.0.23 → 0.0.24

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
@@ -79,8 +79,11 @@ var Hermes = class {
79
79
  toCoin: p.toCoinAddress,
80
80
  extra: p.extra || "0x"
81
81
  }));
82
- const amountOutList = await quoter.multiQuote(params.amountInWei, quoteParams);
83
- return amountOutList.length ? amountOutList[amountOutList.length - 1] : BigInt(0);
82
+ const amountOutList = await quoter.multiQuote.staticCall(params.amountInWei, quoteParams, { from: wallet.address });
83
+ if (!amountOutList.length) {
84
+ throw new Error("No expect result return from smart contract");
85
+ }
86
+ return amountOutList[amountOutList.length - 1];
84
87
  }
85
88
  async swap(params) {
86
89
  this.validateParams(params);
@@ -108,13 +111,11 @@ var Hermes = class {
108
111
  throw new Error("Insufficient balance for swap");
109
112
  }
110
113
  const currentAllowance = await erc20.allowance(params.user, aggregatorAddress);
111
- console.log(currentAllowance);
112
114
  if (currentAllowance < params.amountInWei) {
113
115
  throw new Error("Insufficient allowance token amount for swap");
114
116
  }
115
117
  try {
116
118
  const gas = await aggregator.getFunction("swap").estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, { from: wallet.address });
117
- console.log(`Estimated gas for swap: ${BigInt(gas).toString()}`);
118
119
  } catch (error) {
119
120
  console.warn("Aggregator estimateGas.swap failed", error);
120
121
  throw error;
@@ -127,7 +128,6 @@ var Hermes = class {
127
128
  throw error;
128
129
  }
129
130
  const receipt = await txResponse.wait();
130
- console.log(`Swap transaction mined in block ${receipt.blockNumber} (tx: ${receipt.hash})`);
131
131
  const iface = new import_ethers.ethers.Interface(import_aggregator.default);
132
132
  let amountOut = params.amountInWei;
133
133
  for (const log of receipt.logs) {
@@ -235,14 +235,57 @@ var Hermes = class {
235
235
  }
236
236
  async swapAndBridge(params) {
237
237
  this.validateParams(params);
238
+ const aggregatorAddress = this.getAggregatorAddress(params.chain);
239
+ const wallet = this.walletMap.get(params.chain);
240
+ if (!wallet) {
241
+ throw new Error(`Wallet not configured for chain: ${params.chain}`);
242
+ }
243
+ const fromCoinAddress = params.path[0].fromCoinAddress;
244
+ if (fromCoinAddress && fromCoinAddress !== import_ethers.ethers.ZeroAddress) {
245
+ await this.checkIsEnoughToken(fromCoinAddress, params.user, params.amountInWei, aggregatorAddress, wallet);
246
+ }
247
+ const swapParams = params.path.map((pathItem) => ({
248
+ dexType: pathItem.dexType,
249
+ pool: pathItem.poolAddress,
250
+ fromCoin: pathItem.fromCoinAddress,
251
+ toCoin: pathItem.toCoinAddress,
252
+ extra: pathItem.extra ?? "0x"
253
+ }));
254
+ const bridgeArgs = {
255
+ bridge: params.bridgeType,
256
+ token: params.tokenAddress,
257
+ amount: params.amountInWei,
258
+ bridgeAddress: params.bridgeAddress,
259
+ destChain: params.destChain,
260
+ destUser: params.destUser,
261
+ extra: params.extra ?? "0x"
262
+ };
263
+ const txOverrides = {
264
+ from: wallet.address,
265
+ value: params.bridgeFee
266
+ };
267
+ const aggregator = new import_ethers2.Contract(aggregatorAddress, import_aggregator.default, wallet);
268
+ try {
269
+ await aggregator.swapAndBridge.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txOverrides);
270
+ } catch (error) {
271
+ console.error("Bridge gas estimation reverted", error);
272
+ throw error;
273
+ }
274
+ let txResponse;
275
+ try {
276
+ txResponse = await aggregator.swapAndBridge(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txOverrides);
277
+ } catch (error) {
278
+ console.error("Aggregator swap and bridge transaction failed", error);
279
+ throw error;
280
+ }
238
281
  const receipt = {
239
282
  fromToken: params.path[0].fromCoinAddress,
240
283
  toToken: params.path[params.path.length - 1].toCoinAddress,
241
284
  amountOut: params.minAmountOutList[params.minAmountOutList.length - 1],
242
- hash: "",
243
- from: "",
244
- to: "",
245
- logs: []
285
+ hash: txResponse.hash,
286
+ from: txResponse.from,
287
+ to: txResponse.to,
288
+ logs: txResponse.logs
246
289
  };
247
290
  return Promise.resolve(receipt);
248
291
  }
@@ -90,6 +90,10 @@ export declare enum DexType {
90
90
  CURVE128 = "curve128",
91
91
  CURVE256 = "curve256"
92
92
  }
93
+ export declare enum BridgeType {
94
+ LAYERZEROV1 = "layerzero_v1",
95
+ LAYERZEROV2 = "layerzero_v2"
96
+ }
93
97
  export declare enum ChainNameEnum {
94
98
  ETH = "ETH",// Ethereum Mainnet
95
99
  ARB = "ARB",// Arbitrum One
@@ -252,15 +256,18 @@ export interface IRpcConfig {
252
256
  export declare const AddressConst: {
253
257
  weth: {
254
258
  eth: string;
259
+ arb: string;
255
260
  };
256
261
  usdt: {
257
262
  eth: string;
258
263
  };
259
264
  feth: {
260
265
  eth: string;
266
+ arb: string;
261
267
  };
262
268
  xeth: {
263
269
  eth: string;
270
+ arb: string;
264
271
  };
265
272
  arb: {
266
273
  arb: string;
package/dist/cjs/types.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var types_exports = {};
21
21
  __export(types_exports, {
22
22
  AddressConst: () => AddressConst,
23
+ BridgeType: () => BridgeType,
23
24
  ChainNameEnum: () => ChainNameEnum,
24
25
  DexType: () => DexType
25
26
  });
@@ -31,6 +32,11 @@ var DexType = /* @__PURE__ */ ((DexType2) => {
31
32
  DexType2["CURVE256"] = "curve256";
32
33
  return DexType2;
33
34
  })(DexType || {});
35
+ var BridgeType = /* @__PURE__ */ ((BridgeType2) => {
36
+ BridgeType2["LAYERZEROV1"] = "layerzero_v1";
37
+ BridgeType2["LAYERZEROV2"] = "layerzero_v2";
38
+ return BridgeType2;
39
+ })(BridgeType || {});
34
40
  var ChainNameEnum = /* @__PURE__ */ ((ChainNameEnum2) => {
35
41
  ChainNameEnum2["ETH"] = "ETH";
36
42
  ChainNameEnum2["ARB"] = "ARB";
@@ -189,17 +195,20 @@ var ChainNameEnum = /* @__PURE__ */ ((ChainNameEnum2) => {
189
195
  })(ChainNameEnum || {});
190
196
  var AddressConst = {
191
197
  weth: {
192
- eth: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
198
+ eth: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
199
+ arb: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"
193
200
  },
194
201
  usdt: {
195
202
  eth: ""
196
203
  },
197
204
  // fx
198
205
  feth: {
199
- eth: "0x53805A76E1f5ebbFE7115F16f9c87C2f7e633726"
206
+ eth: "0x53805A76E1f5ebbFE7115F16f9c87C2f7e633726",
207
+ arb: "0xc608Dfb90A430Df79a8a1eDBC8be7f1A0Eb4E763"
200
208
  },
201
209
  xeth: {
202
- eth: "0xe063F04f280c60aECa68b38341C2eEcBeC703ae2"
210
+ eth: "0xe063F04f280c60aECa68b38341C2eEcBeC703ae2",
211
+ arb: "0x55380fe7A1910dFf29A47B622057ab4139DA42C5"
203
212
  },
204
213
  arb: {
205
214
  arb: "0x912CE59144191C1204E64559FE8253a0e49E6548"
@@ -211,6 +220,7 @@ var AddressConst = {
211
220
  // Annotate the CommonJS export names for ESM import in node:
212
221
  0 && (module.exports = {
213
222
  AddressConst,
223
+ BridgeType,
214
224
  ChainNameEnum,
215
225
  DexType
216
226
  });
package/dist/esm/index.js CHANGED
@@ -90,11 +90,19 @@ var Hermes = /*#__PURE__*/function () {
90
90
  };
91
91
  });
92
92
  _context.next = 10;
93
- return quoter.multiQuote(params.amountInWei, quoteParams);
93
+ return quoter.multiQuote.staticCall(params.amountInWei, quoteParams, {
94
+ from: wallet.address
95
+ });
94
96
  case 10:
95
97
  amountOutList = _context.sent;
96
- return _context.abrupt("return", amountOutList.length ? amountOutList[amountOutList.length - 1] : BigInt(0));
97
- case 12:
98
+ if (amountOutList.length) {
99
+ _context.next = 13;
100
+ break;
101
+ }
102
+ throw new Error('No expect result return from smart contract');
103
+ case 13:
104
+ return _context.abrupt("return", amountOutList[amountOutList.length - 1]);
105
+ case 14:
98
106
  case "end":
99
107
  return _context.stop();
100
108
  }
@@ -156,93 +164,90 @@ var Hermes = /*#__PURE__*/function () {
156
164
  return erc20.allowance(params.user, aggregatorAddress);
157
165
  case 19:
158
166
  currentAllowance = _context2.sent;
159
- console.log(currentAllowance);
160
167
  if (!(currentAllowance < params.amountInWei)) {
161
- _context2.next = 23;
168
+ _context2.next = 22;
162
169
  break;
163
170
  }
164
171
  throw new Error('Insufficient allowance token amount for swap');
165
- case 23:
166
- _context2.prev = 23;
167
- _context2.next = 26;
172
+ case 22:
173
+ _context2.prev = 22;
174
+ _context2.next = 25;
168
175
  return aggregator.getFunction('swap').estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, {
169
176
  from: wallet.address
170
177
  });
171
- case 26:
178
+ case 25:
172
179
  gas = _context2.sent;
173
- console.log("Estimated gas for swap: ".concat(BigInt(gas).toString()));
174
- _context2.next = 34;
180
+ _context2.next = 32;
175
181
  break;
176
- case 30:
177
- _context2.prev = 30;
178
- _context2.t0 = _context2["catch"](23);
182
+ case 28:
183
+ _context2.prev = 28;
184
+ _context2.t0 = _context2["catch"](22);
179
185
  console.warn('Aggregator estimateGas.swap failed', _context2.t0);
180
186
  throw _context2.t0;
181
- case 34:
182
- _context2.prev = 34;
183
- _context2.next = 37;
187
+ case 32:
188
+ _context2.prev = 32;
189
+ _context2.next = 35;
184
190
  return aggregator.getFunction('swap')(params.user, params.amountInWei, swapParams, params.minAmountOutList, {
185
191
  from: wallet.address
186
192
  });
187
- case 37:
193
+ case 35:
188
194
  txResponse = _context2.sent;
189
- _context2.next = 44;
195
+ _context2.next = 42;
190
196
  break;
191
- case 40:
192
- _context2.prev = 40;
193
- _context2.t1 = _context2["catch"](34);
197
+ case 38:
198
+ _context2.prev = 38;
199
+ _context2.t1 = _context2["catch"](32);
194
200
  console.error('Aggregator swap transaction failed', _context2.t1);
195
201
  throw _context2.t1;
196
- case 44:
197
- _context2.next = 46;
202
+ case 42:
203
+ _context2.next = 44;
198
204
  return txResponse.wait();
199
- case 46:
205
+ case 44:
200
206
  receipt = _context2.sent;
201
- console.log("Swap transaction mined in block ".concat(receipt.blockNumber, " (tx: ").concat(receipt.hash, ")"));
202
207
  iface = new ethers.Interface(AggregatorAbi);
203
208
  amountOut = params.amountInWei;
204
209
  _iterator = _createForOfIteratorHelper(receipt.logs);
205
- _context2.prev = 51;
210
+ _context2.prev = 48;
206
211
  _iterator.s();
207
- case 53:
212
+ case 50:
208
213
  if ((_step = _iterator.n()).done) {
209
- _context2.next = 67;
214
+ _context2.next = 64;
210
215
  break;
211
216
  }
212
217
  log = _step.value;
213
218
  if (!(log.address.toLowerCase() === aggregatorAddress.toLowerCase())) {
214
- _context2.next = 65;
219
+ _context2.next = 62;
215
220
  break;
216
221
  }
217
- _context2.prev = 56;
222
+ _context2.prev = 53;
218
223
  parsed = iface.parseLog(log);
219
224
  if (!(parsed && parsed.name === 'Swapped' && parsed.args && parsed.args.amountOut !== undefined)) {
220
- _context2.next = 61;
225
+ _context2.next = 58;
221
226
  break;
222
227
  }
223
228
  amountOut = parsed.args.amountOut;
224
- return _context2.abrupt("break", 67);
225
- case 61:
226
- _context2.next = 65;
229
+ return _context2.abrupt("break", 64);
230
+ case 58:
231
+ _context2.next = 62;
227
232
  break;
228
- case 63:
229
- _context2.prev = 63;
230
- _context2.t2 = _context2["catch"](56);
231
- case 65:
232
- _context2.next = 53;
233
+ case 60:
234
+ _context2.prev = 60;
235
+ _context2.t2 = _context2["catch"](53);
236
+ case 62:
237
+ _context2.next = 50;
233
238
  break;
234
- case 67:
235
- _context2.next = 72;
239
+ case 64:
240
+ _context2.next = 69;
236
241
  break;
242
+ case 66:
243
+ _context2.prev = 66;
244
+ _context2.t3 = _context2["catch"](48);
245
+ _iterator.e(_context2.t3);
237
246
  case 69:
238
247
  _context2.prev = 69;
239
- _context2.t3 = _context2["catch"](51);
240
- _iterator.e(_context2.t3);
241
- case 72:
242
- _context2.prev = 72;
243
248
  _iterator.f();
244
- return _context2.finish(72);
245
- case 75:
249
+ return _context2.finish(69);
250
+ case 72:
246
251
  return _context2.abrupt("return", {
247
252
  fromToken: fromTokenAddress,
248
253
  toToken: toTokenAddress,
@@ -252,11 +257,11 @@ var Hermes = /*#__PURE__*/function () {
252
257
  to: receipt.from,
253
258
  logs: receipt.logs
254
259
  });
255
- case 76:
260
+ case 73:
256
261
  case "end":
257
262
  return _context2.stop();
258
263
  }
259
- }, _callee2, this, [[23, 30], [34, 40], [51, 69, 72, 75], [56, 63]]);
264
+ }, _callee2, this, [[22, 28], [32, 38], [48, 66, 69, 72], [53, 60]]);
260
265
  }));
261
266
  function swap(_x2) {
262
267
  return _swap.apply(this, arguments);
@@ -427,26 +432,95 @@ var Hermes = /*#__PURE__*/function () {
427
432
  key: "swapAndBridge",
428
433
  value: function () {
429
434
  var _swapAndBridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(params) {
430
- var receipt;
435
+ var _params$extra3;
436
+ var aggregatorAddress, wallet, fromCoinAddress, swapParams, bridgeArgs, txOverrides, aggregator, txResponse, receipt;
431
437
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
432
438
  while (1) switch (_context5.prev = _context5.next) {
433
439
  case 0:
434
440
  this.validateParams(params);
441
+
442
+ // call the aggregator swap and bridge
443
+ aggregatorAddress = this.getAggregatorAddress(params.chain);
444
+ wallet = this.walletMap.get(params.chain);
445
+ if (wallet) {
446
+ _context5.next = 5;
447
+ break;
448
+ }
449
+ throw new Error("Wallet not configured for chain: ".concat(params.chain));
450
+ case 5:
451
+ fromCoinAddress = params.path[0].fromCoinAddress;
452
+ if (!(fromCoinAddress && fromCoinAddress !== ethers.ZeroAddress)) {
453
+ _context5.next = 9;
454
+ break;
455
+ }
456
+ _context5.next = 9;
457
+ return this.checkIsEnoughToken(fromCoinAddress, params.user, params.amountInWei, aggregatorAddress, wallet);
458
+ case 9:
459
+ // 准备合约参数
460
+ swapParams = params.path.map(function (pathItem) {
461
+ var _pathItem$extra2;
462
+ return {
463
+ dexType: pathItem.dexType,
464
+ pool: pathItem.poolAddress,
465
+ fromCoin: pathItem.fromCoinAddress,
466
+ toCoin: pathItem.toCoinAddress,
467
+ extra: (_pathItem$extra2 = pathItem.extra) !== null && _pathItem$extra2 !== void 0 ? _pathItem$extra2 : '0x'
468
+ };
469
+ });
470
+ bridgeArgs = {
471
+ bridge: params.bridgeType,
472
+ token: params.tokenAddress,
473
+ amount: params.amountInWei,
474
+ bridgeAddress: params.bridgeAddress,
475
+ destChain: params.destChain,
476
+ destUser: params.destUser,
477
+ extra: (_params$extra3 = params.extra) !== null && _params$extra3 !== void 0 ? _params$extra3 : '0x'
478
+ };
479
+ txOverrides = {
480
+ from: wallet.address,
481
+ value: params.bridgeFee
482
+ }; // simulate
483
+ aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
484
+ _context5.prev = 13;
485
+ _context5.next = 16;
486
+ return aggregator.swapAndBridge.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txOverrides);
487
+ case 16:
488
+ _context5.next = 22;
489
+ break;
490
+ case 18:
491
+ _context5.prev = 18;
492
+ _context5.t0 = _context5["catch"](13);
493
+ console.error('Bridge gas estimation reverted', _context5.t0);
494
+ throw _context5.t0;
495
+ case 22:
496
+ _context5.prev = 22;
497
+ _context5.next = 25;
498
+ return aggregator.swapAndBridge(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txOverrides);
499
+ case 25:
500
+ txResponse = _context5.sent;
501
+ _context5.next = 32;
502
+ break;
503
+ case 28:
504
+ _context5.prev = 28;
505
+ _context5.t1 = _context5["catch"](22);
506
+ console.error('Aggregator swap and bridge transaction failed', _context5.t1);
507
+ throw _context5.t1;
508
+ case 32:
435
509
  receipt = {
436
510
  fromToken: params.path[0].fromCoinAddress,
437
511
  toToken: params.path[params.path.length - 1].toCoinAddress,
438
512
  amountOut: params.minAmountOutList[params.minAmountOutList.length - 1],
439
- hash: '',
440
- from: '',
441
- to: '',
442
- logs: []
513
+ hash: txResponse.hash,
514
+ from: txResponse.from,
515
+ to: txResponse.to,
516
+ logs: txResponse.logs
443
517
  };
444
518
  return _context5.abrupt("return", Promise.resolve(receipt));
445
- case 3:
519
+ case 34:
446
520
  case "end":
447
521
  return _context5.stop();
448
522
  }
449
- }, _callee5, this);
523
+ }, _callee5, this, [[13, 18], [22, 28]]);
450
524
  }));
451
525
  function swapAndBridge(_x5) {
452
526
  return _swapAndBridge.apply(this, arguments);
@@ -90,6 +90,10 @@ export declare enum DexType {
90
90
  CURVE128 = "curve128",
91
91
  CURVE256 = "curve256"
92
92
  }
93
+ export declare enum BridgeType {
94
+ LAYERZEROV1 = "layerzero_v1",
95
+ LAYERZEROV2 = "layerzero_v2"
96
+ }
93
97
  export declare enum ChainNameEnum {
94
98
  ETH = "ETH",// Ethereum Mainnet
95
99
  ARB = "ARB",// Arbitrum One
@@ -252,15 +256,18 @@ export interface IRpcConfig {
252
256
  export declare const AddressConst: {
253
257
  weth: {
254
258
  eth: string;
259
+ arb: string;
255
260
  };
256
261
  usdt: {
257
262
  eth: string;
258
263
  };
259
264
  feth: {
260
265
  eth: string;
266
+ arb: string;
261
267
  };
262
268
  xeth: {
263
269
  eth: string;
270
+ arb: string;
264
271
  };
265
272
  arb: {
266
273
  arb: string;
package/dist/esm/types.js CHANGED
@@ -5,6 +5,11 @@ export var DexType = /*#__PURE__*/function (DexType) {
5
5
  DexType["CURVE256"] = "curve256";
6
6
  return DexType;
7
7
  }({});
8
+ export var BridgeType = /*#__PURE__*/function (BridgeType) {
9
+ BridgeType["LAYERZEROV1"] = "layerzero_v1";
10
+ BridgeType["LAYERZEROV2"] = "layerzero_v2";
11
+ return BridgeType;
12
+ }({});
8
13
  export var ChainNameEnum = /*#__PURE__*/function (ChainNameEnum) {
9
14
  ChainNameEnum["ETH"] = "ETH";
10
15
  ChainNameEnum["ARB"] = "ARB";
@@ -163,17 +168,20 @@ export var ChainNameEnum = /*#__PURE__*/function (ChainNameEnum) {
163
168
  }({}); // BEVM
164
169
  export var AddressConst = {
165
170
  weth: {
166
- eth: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
171
+ eth: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
172
+ arb: '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1'
167
173
  },
168
174
  usdt: {
169
175
  eth: ''
170
176
  },
171
177
  // fx
172
178
  feth: {
173
- eth: '0x53805A76E1f5ebbFE7115F16f9c87C2f7e633726'
179
+ eth: '0x53805A76E1f5ebbFE7115F16f9c87C2f7e633726',
180
+ arb: '0xc608Dfb90A430Df79a8a1eDBC8be7f1A0Eb4E763'
174
181
  },
175
182
  xeth: {
176
- eth: '0xe063F04f280c60aECa68b38341C2eEcBeC703ae2'
183
+ eth: '0xe063F04f280c60aECa68b38341C2eEcBeC703ae2',
184
+ arb: '0x55380fe7A1910dFf29A47B622057ab4139DA42C5'
177
185
  },
178
186
  arb: {
179
187
  arb: '0x912CE59144191C1204E64559FE8253a0e49E6548'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-swap",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "A TypeScript utility library for swap and bridge",
5
5
  "type": "module",
6
6
  "main": "dist/esm/index.js",