hermes-swap 0.0.22 → 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
@@ -267,26 +267,104 @@ var Hermes = /*#__PURE__*/function () {
267
267
  key: "bridge",
268
268
  value: function () {
269
269
  var _bridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
270
- var receipt;
270
+ var _params$extra;
271
+ var aggregatorAddress, wallet, provider, nativeBalance, aggregator, bridgeArgs, txOverrides, txResponse, receipt;
271
272
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
272
273
  while (1) switch (_context3.prev = _context3.next) {
273
274
  case 0:
274
275
  this.validateParams(params);
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:
275
353
  receipt = {
276
354
  fromToken: params.tokenAddress,
277
355
  toToken: params.tokenAddress,
278
356
  amountOut: params.amountInWei,
279
- hash: '',
280
- from: '',
281
- to: '',
282
- logs: []
357
+ hash: txResponse.hash,
358
+ from: txResponse.from,
359
+ to: txResponse.to,
360
+ logs: txResponse.logs
283
361
  };
284
362
  return _context3.abrupt("return", Promise.resolve(receipt));
285
- case 3:
363
+ case 41:
286
364
  case "end":
287
365
  return _context3.stop();
288
366
  }
289
- }, _callee3, this);
367
+ }, _callee3, this, [[20, 25], [29, 35]]);
290
368
  }));
291
369
  function bridge(_x3) {
292
370
  return _bridge.apply(this, arguments);
@@ -294,12 +372,64 @@ var Hermes = /*#__PURE__*/function () {
294
372
  return bridge;
295
373
  }()
296
374
  }, {
297
- key: "swapAndBridge",
375
+ key: "estimateBridgeFee",
298
376
  value: function () {
299
- var _swapAndBridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
300
- var receipt;
377
+ var _estimateBridgeFee = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
378
+ var _params$extra2;
379
+ var wallet, address, quoter, bridgeArgs, bridgeFee;
301
380
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
302
381
  while (1) switch (_context4.prev = _context4.next) {
382
+ case 0:
383
+ this.validateParams(params);
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:
416
+ case "end":
417
+ return _context4.stop();
418
+ }
419
+ }, _callee4, this);
420
+ }));
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) {
303
433
  case 0:
304
434
  this.validateParams(params);
305
435
  receipt = {
@@ -311,18 +441,58 @@ var Hermes = /*#__PURE__*/function () {
311
441
  to: '',
312
442
  logs: []
313
443
  };
314
- return _context4.abrupt("return", Promise.resolve(receipt));
444
+ return _context5.abrupt("return", Promise.resolve(receipt));
315
445
  case 3:
316
446
  case "end":
317
- return _context4.stop();
447
+ return _context5.stop();
318
448
  }
319
- }, _callee4, this);
449
+ }, _callee5, this);
320
450
  }));
321
- function swapAndBridge(_x4) {
451
+ function swapAndBridge(_x5) {
322
452
  return _swapAndBridge.apply(this, arguments);
323
453
  }
324
454
  return swapAndBridge;
325
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
+ }()
326
496
  }, {
327
497
  key: "validateParams",
328
498
  value: function validateParams(params) {
@@ -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,6 +38,8 @@ 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 {
@@ -81,7 +85,10 @@ export interface ILog {
81
85
  args: any[];
82
86
  }
83
87
  export declare enum DexType {
84
- FX = "f(x)"
88
+ FX = "f(x)",
89
+ UNISWAPV2 = "uniswapv2",
90
+ CURVE128 = "curve128",
91
+ CURVE256 = "curve256"
85
92
  }
86
93
  export declare enum ChainNameEnum {
87
94
  ETH = "ETH",// Ethereum Mainnet
@@ -255,6 +262,12 @@ export declare const AddressConst: {
255
262
  xeth: {
256
263
  eth: string;
257
264
  };
265
+ arb: {
266
+ arb: string;
267
+ };
268
+ usdc: {
269
+ arb: string;
270
+ };
258
271
  };
259
272
  export interface IExpectPayload {
260
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.22",
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",