hermes-swap 0.0.21 → 0.0.23

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.
@@ -62,7 +62,7 @@ var QuoterAbi = [{
62
62
  type: 'uint256[][]',
63
63
  internalType: 'uint256[][]'
64
64
  }],
65
- stateMutability: 'view'
65
+ stateMutability: 'nonpayable'
66
66
  }, {
67
67
  type: 'function',
68
68
  name: 'contractList',
@@ -128,7 +128,7 @@ var QuoterAbi = [{
128
128
  type: 'bool',
129
129
  internalType: 'bool'
130
130
  }],
131
- stateMutability: 'view'
131
+ stateMutability: 'nonpayable'
132
132
  }, {
133
133
  type: 'function',
134
134
  name: 'isWhitelist',
@@ -181,7 +181,7 @@ var QuoterAbi = [{
181
181
  type: 'uint256[]',
182
182
  internalType: 'uint256[]'
183
183
  }],
184
- stateMutability: 'view'
184
+ stateMutability: 'nonpayable'
185
185
  }, {
186
186
  type: 'function',
187
187
  name: 'owner',
@@ -230,47 +230,42 @@ var QuoterAbi = [{
230
230
  type: 'uint256',
231
231
  internalType: 'uint256'
232
232
  }],
233
- stateMutability: 'view'
233
+ stateMutability: 'nonpayable'
234
234
  }, {
235
235
  type: 'function',
236
236
  name: 'quoteBridge',
237
237
  inputs: [{
238
- name: 'bridgeParams',
238
+ name: 'bridgeParam',
239
239
  type: 'tuple',
240
- internalType: 'struct BridgeParams',
240
+ internalType: 'struct BridgeParam',
241
241
  components: [{
242
242
  name: 'bridge',
243
243
  type: 'string',
244
244
  internalType: 'string'
245
245
  }, {
246
- name: 'bridgeParam',
247
- type: 'tuple',
248
- internalType: 'struct BridgeParam',
249
- components: [{
250
- name: 'token',
251
- type: 'address',
252
- internalType: 'address'
253
- }, {
254
- name: 'amount',
255
- type: 'uint256',
256
- internalType: 'uint256'
257
- }, {
258
- name: 'bridgeAddress',
259
- type: 'address',
260
- internalType: 'address'
261
- }, {
262
- name: 'refundAddress',
263
- type: 'address',
264
- internalType: 'address'
265
- }, {
266
- name: 'destinationChain',
267
- type: 'string',
268
- internalType: 'string'
269
- }, {
270
- name: 'adapterParams',
271
- type: 'bytes',
272
- internalType: 'bytes'
273
- }]
246
+ name: 'token',
247
+ type: 'address',
248
+ internalType: 'address'
249
+ }, {
250
+ name: 'amount',
251
+ type: 'uint256',
252
+ internalType: 'uint256'
253
+ }, {
254
+ name: 'bridgeAddress',
255
+ type: 'address',
256
+ internalType: 'address'
257
+ }, {
258
+ name: 'destChain',
259
+ type: 'string',
260
+ internalType: 'string'
261
+ }, {
262
+ name: 'destUser',
263
+ type: 'address',
264
+ internalType: 'address'
265
+ }, {
266
+ name: 'extra',
267
+ type: 'bytes',
268
+ internalType: 'bytes'
274
269
  }]
275
270
  }],
276
271
  outputs: [{
@@ -278,7 +273,7 @@ var QuoterAbi = [{
278
273
  type: 'uint256',
279
274
  internalType: 'uint256'
280
275
  }],
281
- stateMutability: 'view'
276
+ stateMutability: 'nonpayable'
282
277
  }, {
283
278
  type: 'function',
284
279
  name: 'removeContract',
@@ -377,5 +372,29 @@ var QuoterAbi = [{
377
372
  internalType: 'address'
378
373
  }],
379
374
  anonymous: false
375
+ }, {
376
+ type: 'error',
377
+ name: 'NotExecutor',
378
+ inputs: [{
379
+ name: 'caller',
380
+ type: 'address',
381
+ internalType: 'address'
382
+ }, {
383
+ name: 'executor',
384
+ type: 'address',
385
+ internalType: 'address'
386
+ }]
387
+ }, {
388
+ type: 'error',
389
+ name: 'NotOwner',
390
+ inputs: [{
391
+ name: 'caller',
392
+ type: 'address',
393
+ internalType: 'address'
394
+ }, {
395
+ name: 'owner',
396
+ type: 'address',
397
+ internalType: 'address'
398
+ }]
380
399
  }];
381
400
  export default QuoterAbi;
@@ -12,7 +12,9 @@ declare class Hermes {
12
12
  expect(params: IExpectParams): Promise<bigint>;
13
13
  swap(params: ISwapParams): Promise<IReceipt>;
14
14
  bridge(params: IBridgeParams): Promise<IReceipt>;
15
+ estimateBridgeFee(params: IBridgeParams): Promise<bigint>;
15
16
  swapAndBridge(params: ISwapAndBridgeParams): Promise<IReceipt>;
17
+ private checkIsEnoughToken;
16
18
  private validateParams;
17
19
  private getQuoterAddress;
18
20
  private getAggregatorAddress;
package/dist/esm/index.js CHANGED
@@ -43,7 +43,6 @@ var Hermes = /*#__PURE__*/function () {
43
43
  this.walletMap.set(chainName, new ethers.Wallet(rpc.privateKey, provider));
44
44
  }
45
45
  }
46
- console.log(Object.entries(this.config.rpc), this.walletMap);
47
46
  for (var _i2 = 0, _Object$entries2 = Object.entries(this.config.quoterAddress); _i2 < _Object$entries2.length; _i2++) {
48
47
  var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
49
48
  _chainName = _Object$entries2$_i[0],
@@ -247,7 +246,11 @@ var Hermes = /*#__PURE__*/function () {
247
246
  return _context2.abrupt("return", {
248
247
  fromToken: fromTokenAddress,
249
248
  toToken: toTokenAddress,
250
- amountOut: amountOut
249
+ amountOut: amountOut,
250
+ hash: receipt.hash,
251
+ from: receipt.from,
252
+ to: receipt.from,
253
+ logs: receipt.logs
251
254
  });
252
255
  case 76:
253
256
  case "end":
@@ -264,18 +267,104 @@ var Hermes = /*#__PURE__*/function () {
264
267
  key: "bridge",
265
268
  value: function () {
266
269
  var _bridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
267
- var receipt;
270
+ var _params$extra;
271
+ var aggregatorAddress, wallet, provider, nativeBalance, aggregator, bridgeArgs, txOverrides, txResponse, receipt;
268
272
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
269
273
  while (1) switch (_context3.prev = _context3.next) {
270
274
  case 0:
271
275
  this.validateParams(params);
272
- receipt = {};
276
+
277
+ // set up
278
+ aggregatorAddress = this.getAggregatorAddress(params.chain);
279
+ wallet = this.walletMap.get(params.chain);
280
+ if (wallet) {
281
+ _context3.next = 5;
282
+ break;
283
+ }
284
+ throw new Error("Wallet not configured for chain: ".concat(params.chain));
285
+ case 5:
286
+ if (!(params.tokenAddress && params.tokenAddress !== ethers.ZeroAddress)) {
287
+ _context3.next = 8;
288
+ break;
289
+ }
290
+ _context3.next = 8;
291
+ return this.checkIsEnoughToken(params.tokenAddress, params.user, params.amountInWei, aggregatorAddress, wallet);
292
+ case 8:
293
+ if (!(params.bridgeFee > 0n)) {
294
+ _context3.next = 17;
295
+ break;
296
+ }
297
+ provider = this.providerMap.get(params.chain);
298
+ if (provider) {
299
+ _context3.next = 12;
300
+ break;
301
+ }
302
+ throw new Error("Provider not configured for chain: ".concat(params.chain));
303
+ case 12:
304
+ _context3.next = 14;
305
+ return provider.getBalance(params.user);
306
+ case 14:
307
+ nativeBalance = _context3.sent;
308
+ if (!(nativeBalance < params.bridgeFee)) {
309
+ _context3.next = 17;
310
+ break;
311
+ }
312
+ throw new Error('Insufficient native balance for bridge fee');
313
+ case 17:
314
+ aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet); // simulate
315
+ bridgeArgs = {
316
+ bridge: params.bridgeType,
317
+ token: params.tokenAddress,
318
+ amount: params.amountInWei,
319
+ bridgeAddress: params.bridgeAddress,
320
+ destChain: params.destChain,
321
+ destUser: params.destUser,
322
+ extra: (_params$extra = params.extra) !== null && _params$extra !== void 0 ? _params$extra : '0x'
323
+ };
324
+ txOverrides = {
325
+ from: wallet.address,
326
+ value: params.bridgeFee
327
+ }; // simulate
328
+ _context3.prev = 20;
329
+ _context3.next = 23;
330
+ return aggregator.bridge.estimateGas(params.user, bridgeArgs, txOverrides);
331
+ case 23:
332
+ _context3.next = 29;
333
+ break;
334
+ case 25:
335
+ _context3.prev = 25;
336
+ _context3.t0 = _context3["catch"](20);
337
+ console.error('Bridge gas estimation reverted', _context3.t0);
338
+ throw _context3.t0;
339
+ case 29:
340
+ _context3.prev = 29;
341
+ _context3.next = 32;
342
+ return aggregator.bridge(params.user, bridgeArgs, txOverrides);
343
+ case 32:
344
+ txResponse = _context3.sent;
345
+ _context3.next = 39;
346
+ break;
347
+ case 35:
348
+ _context3.prev = 35;
349
+ _context3.t1 = _context3["catch"](29);
350
+ console.error('Aggregator swap transaction failed', _context3.t1);
351
+ throw _context3.t1;
352
+ case 39:
353
+ receipt = {
354
+ fromToken: params.tokenAddress,
355
+ toToken: params.tokenAddress,
356
+ amountOut: params.amountInWei,
357
+ hash: txResponse.hash,
358
+ from: txResponse.from,
359
+ to: txResponse.to,
360
+ logs: txResponse.logs
361
+ };
273
362
  return _context3.abrupt("return", Promise.resolve(receipt));
274
- case 3:
363
+ case 41:
275
364
  case "end":
276
365
  return _context3.stop();
277
366
  }
278
- }, _callee3, this);
367
+ }, _callee3, this, [[20, 25], [29, 35]]);
279
368
  }));
280
369
  function bridge(_x3) {
281
370
  return _bridge.apply(this, arguments);
@@ -283,27 +372,127 @@ var Hermes = /*#__PURE__*/function () {
283
372
  return bridge;
284
373
  }()
285
374
  }, {
286
- key: "swapAndBridge",
375
+ key: "estimateBridgeFee",
287
376
  value: function () {
288
- var _swapAndBridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
289
- var receipt;
377
+ var _estimateBridgeFee = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
378
+ var _params$extra2;
379
+ var wallet, address, quoter, bridgeArgs, bridgeFee;
290
380
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
291
381
  while (1) switch (_context4.prev = _context4.next) {
292
382
  case 0:
293
383
  this.validateParams(params);
294
- receipt = {};
295
- return _context4.abrupt("return", Promise.resolve(receipt));
296
- case 3:
384
+ wallet = this.walletMap.get(params.chain);
385
+ if (wallet) {
386
+ _context4.next = 4;
387
+ break;
388
+ }
389
+ throw new Error("Wallet not configured for chain: ".concat(params.chain));
390
+ case 4:
391
+ address = this.getQuoterAddress(params.chain);
392
+ if (address) {
393
+ _context4.next = 7;
394
+ break;
395
+ }
396
+ throw new Error("Quoter address not found for chain: ".concat(params.chain));
397
+ case 7:
398
+ quoter = new Contract(address, QuoterAbi, wallet);
399
+ bridgeArgs = {
400
+ bridge: params.bridgeType,
401
+ token: params.tokenAddress,
402
+ amount: params.amountInWei,
403
+ bridgeAddress: params.bridgeAddress,
404
+ destChain: params.destChain,
405
+ destUser: params.destUser,
406
+ extra: (_params$extra2 = params.extra) !== null && _params$extra2 !== void 0 ? _params$extra2 : '0x'
407
+ };
408
+ _context4.next = 11;
409
+ return quoter.quoteBridge.staticCall(bridgeArgs, {
410
+ from: wallet.address
411
+ });
412
+ case 11:
413
+ bridgeFee = _context4.sent;
414
+ return _context4.abrupt("return", bridgeFee);
415
+ case 13:
297
416
  case "end":
298
417
  return _context4.stop();
299
418
  }
300
419
  }, _callee4, this);
301
420
  }));
302
- function swapAndBridge(_x4) {
421
+ function estimateBridgeFee(_x4) {
422
+ return _estimateBridgeFee.apply(this, arguments);
423
+ }
424
+ return estimateBridgeFee;
425
+ }()
426
+ }, {
427
+ key: "swapAndBridge",
428
+ value: function () {
429
+ var _swapAndBridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(params) {
430
+ var receipt;
431
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
432
+ while (1) switch (_context5.prev = _context5.next) {
433
+ case 0:
434
+ this.validateParams(params);
435
+ receipt = {
436
+ fromToken: params.path[0].fromCoinAddress,
437
+ toToken: params.path[params.path.length - 1].toCoinAddress,
438
+ amountOut: params.minAmountOutList[params.minAmountOutList.length - 1],
439
+ hash: '',
440
+ from: '',
441
+ to: '',
442
+ logs: []
443
+ };
444
+ return _context5.abrupt("return", Promise.resolve(receipt));
445
+ case 3:
446
+ case "end":
447
+ return _context5.stop();
448
+ }
449
+ }, _callee5, this);
450
+ }));
451
+ function swapAndBridge(_x5) {
303
452
  return _swapAndBridge.apply(this, arguments);
304
453
  }
305
454
  return swapAndBridge;
306
455
  }()
456
+ }, {
457
+ key: "checkIsEnoughToken",
458
+ value: function () {
459
+ var _checkIsEnoughToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(fromTokenAddress, userAddress, amountInWei, aggregatorAddress, wallet) {
460
+ var erc20, userBalance, currentAllowance;
461
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
462
+ while (1) switch (_context6.prev = _context6.next) {
463
+ case 0:
464
+ erc20 = new Contract(fromTokenAddress, ['function balanceOf(address) view returns (uint256)', 'function allowance(address, address) view returns (uint256)'], wallet);
465
+ _context6.next = 3;
466
+ return erc20.balanceOf(userAddress);
467
+ case 3:
468
+ userBalance = _context6.sent;
469
+ if (!(userBalance < amountInWei)) {
470
+ _context6.next = 6;
471
+ break;
472
+ }
473
+ throw new Error('Insufficient balance token amount');
474
+ case 6:
475
+ _context6.next = 8;
476
+ return erc20.allowance(userAddress, aggregatorAddress);
477
+ case 8:
478
+ currentAllowance = _context6.sent;
479
+ console.log(currentAllowance);
480
+ if (!(currentAllowance < amountInWei)) {
481
+ _context6.next = 12;
482
+ break;
483
+ }
484
+ throw new Error('Insufficient allowance token amount');
485
+ case 12:
486
+ case "end":
487
+ return _context6.stop();
488
+ }
489
+ }, _callee6);
490
+ }));
491
+ function checkIsEnoughToken(_x6, _x7, _x8, _x9, _x10) {
492
+ return _checkIsEnoughToken.apply(this, arguments);
493
+ }
494
+ return checkIsEnoughToken;
495
+ }()
307
496
  }, {
308
497
  key: "validateParams",
309
498
  value: function validateParams(params) {
@@ -1,4 +1,4 @@
1
- import type { BytesLike } from 'ethers';
1
+ import type { BytesLike, JsonRpcProvider } from 'ethers';
2
2
  export interface IConfig {
3
3
  rpc: Record<string, IRpcConfig>;
4
4
  quoterAddress: Record<string, string>;
@@ -24,6 +24,8 @@ export interface IBridgeParams {
24
24
  bridgeAddress: string;
25
25
  tokenAddress: string;
26
26
  destChain: string;
27
+ destUser: string;
28
+ bridgeFee: bigint;
27
29
  extra?: BytesLike;
28
30
  }
29
31
  export interface ISwapAndBridgeParams {
@@ -36,15 +38,57 @@ export interface ISwapAndBridgeParams {
36
38
  bridgeAddress: string;
37
39
  tokenAddress: string;
38
40
  destChain: string;
41
+ destUser: string;
42
+ bridgeFee: bigint;
39
43
  extra?: BytesLike;
40
44
  }
41
45
  export interface IReceipt {
42
- fromToken?: string;
43
- toToken?: string;
44
- amountOut?: bigint;
46
+ fromToken: string;
47
+ toToken: string;
48
+ amountOut: bigint;
49
+ hash: string;
50
+ from: string;
51
+ to: string;
52
+ logs: ILog[];
53
+ }
54
+ export interface IEthersReceipt {
55
+ provider: JsonRpcProvider;
56
+ to: string;
57
+ from: string;
58
+ contractAddress: string | null;
59
+ hash: string;
60
+ index: number;
61
+ blockHash: string;
62
+ blockNumber: number;
63
+ logsBloom: string;
64
+ gasUsed: bigint;
65
+ blobGasUsed: null;
66
+ cumulativeGasUsed: bigint;
67
+ gasPrice: bigint;
68
+ blobGasPrice: null;
69
+ type: number;
70
+ status: number;
71
+ root: undefined;
72
+ logs: ILog[];
73
+ }
74
+ export interface ILog {
75
+ provider: JsonRpcProvider;
76
+ transactionHash: string;
77
+ blockHash: string;
78
+ blockNumber: number;
79
+ removed: undefined;
80
+ address: string;
81
+ data: string;
82
+ topics: string[];
83
+ index: number;
84
+ transactionIndex: number;
85
+ args: any[];
45
86
  }
46
87
  export declare enum DexType {
47
- FX = "f(x)"
88
+ FX = "f(x)",
89
+ UNISWAPV2 = "uniswapv2",
90
+ CURVE128 = "curve128",
91
+ CURVE256 = "curve256"
48
92
  }
49
93
  export declare enum ChainNameEnum {
50
94
  ETH = "ETH",// Ethereum Mainnet
@@ -218,6 +262,12 @@ export declare const AddressConst: {
218
262
  xeth: {
219
263
  eth: string;
220
264
  };
265
+ arb: {
266
+ arb: string;
267
+ };
268
+ usdc: {
269
+ arb: string;
270
+ };
221
271
  };
222
272
  export interface IExpectPayload {
223
273
  }
package/dist/esm/types.js CHANGED
@@ -1,5 +1,8 @@
1
1
  export var DexType = /*#__PURE__*/function (DexType) {
2
2
  DexType["FX"] = "f(x)";
3
+ DexType["UNISWAPV2"] = "uniswapv2";
4
+ DexType["CURVE128"] = "curve128";
5
+ DexType["CURVE256"] = "curve256";
3
6
  return DexType;
4
7
  }({});
5
8
  export var ChainNameEnum = /*#__PURE__*/function (ChainNameEnum) {
@@ -171,5 +174,11 @@ export var AddressConst = {
171
174
  },
172
175
  xeth: {
173
176
  eth: '0xe063F04f280c60aECa68b38341C2eEcBeC703ae2'
177
+ },
178
+ arb: {
179
+ arb: '0x912CE59144191C1204E64559FE8253a0e49E6548'
180
+ },
181
+ usdc: {
182
+ arb: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831'
174
183
  }
175
184
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-swap",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "A TypeScript utility library for swap and bridge",
5
5
  "type": "module",
6
6
  "main": "dist/esm/index.js",