hermes-swap 0.0.24 → 0.0.25
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.d.ts +17 -2
- package/dist/cjs/index.js +58 -0
- package/dist/esm/index.d.ts +17 -2
- package/dist/esm/index.js +83 -4
- package/package.json +1 -1
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, IExpectPayload, IRouterPath } from './types.js';
|
|
2
|
-
import { ChainNameEnum, AddressConst, DexType } from './types.js';
|
|
2
|
+
import { ChainNameEnum, AddressConst, DexType, BridgeType } from './types.js';
|
|
3
3
|
export type { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, IExpectPayload, IRouterPath };
|
|
4
|
-
export { ChainNameEnum, AddressConst, DexType };
|
|
4
|
+
export { ChainNameEnum, AddressConst, DexType, BridgeType };
|
|
5
5
|
declare class Hermes {
|
|
6
6
|
private config;
|
|
7
7
|
private providerMap;
|
|
@@ -11,9 +11,24 @@ declare class Hermes {
|
|
|
11
11
|
constructor(config: IConfig);
|
|
12
12
|
expect(params: IExpectParams): Promise<bigint>;
|
|
13
13
|
swap(params: ISwapParams): Promise<IReceipt>;
|
|
14
|
+
/**
|
|
15
|
+
* 生成 swap 的 calldata
|
|
16
|
+
*/
|
|
17
|
+
genSwapCalldata(params: ISwapParams): {
|
|
18
|
+
to: string;
|
|
19
|
+
data: string;
|
|
20
|
+
};
|
|
14
21
|
bridge(params: IBridgeParams): Promise<IReceipt>;
|
|
15
22
|
estimateBridgeFee(params: IBridgeParams): Promise<bigint>;
|
|
16
23
|
swapAndBridge(params: ISwapAndBridgeParams): Promise<IReceipt>;
|
|
24
|
+
/**
|
|
25
|
+
* 生成 swapAndBridge 的 calldata
|
|
26
|
+
*/
|
|
27
|
+
genSwapAndBridgeCalldata(params: ISwapAndBridgeParams): {
|
|
28
|
+
to: string;
|
|
29
|
+
data: string;
|
|
30
|
+
value: bigint;
|
|
31
|
+
};
|
|
17
32
|
private checkIsEnoughToken;
|
|
18
33
|
private validateParams;
|
|
19
34
|
private getQuoterAddress;
|
package/dist/cjs/index.js
CHANGED
|
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
var src_exports = {};
|
|
31
31
|
__export(src_exports, {
|
|
32
32
|
AddressConst: () => import_types.AddressConst,
|
|
33
|
+
BridgeType: () => import_types.BridgeType,
|
|
33
34
|
ChainNameEnum: () => import_types.ChainNameEnum,
|
|
34
35
|
DexType: () => import_types.DexType,
|
|
35
36
|
Hermes: () => Hermes,
|
|
@@ -152,6 +153,29 @@ var Hermes = class {
|
|
|
152
153
|
logs: receipt.logs
|
|
153
154
|
};
|
|
154
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* 生成 swap 的 calldata
|
|
158
|
+
*/
|
|
159
|
+
genSwapCalldata(params) {
|
|
160
|
+
this.validateParams(params);
|
|
161
|
+
if (!params.path || params.path.length === 0) {
|
|
162
|
+
throw new Error("Swap path not provided");
|
|
163
|
+
}
|
|
164
|
+
const aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
165
|
+
const swapParams = params.path.map((pathItem) => ({
|
|
166
|
+
dexType: pathItem.dexType,
|
|
167
|
+
pool: pathItem.poolAddress,
|
|
168
|
+
fromCoin: pathItem.fromCoinAddress,
|
|
169
|
+
toCoin: pathItem.toCoinAddress,
|
|
170
|
+
extra: pathItem.extra ?? "0x"
|
|
171
|
+
}));
|
|
172
|
+
const iface = new import_ethers.ethers.Interface(import_aggregator.default);
|
|
173
|
+
const calldata = iface.encodeFunctionData("swap", [params.user, params.amountInWei, swapParams, params.minAmountOutList]);
|
|
174
|
+
return {
|
|
175
|
+
to: aggregatorAddress,
|
|
176
|
+
data: calldata
|
|
177
|
+
};
|
|
178
|
+
}
|
|
155
179
|
async bridge(params) {
|
|
156
180
|
this.validateParams(params);
|
|
157
181
|
const aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
@@ -289,6 +313,39 @@ var Hermes = class {
|
|
|
289
313
|
};
|
|
290
314
|
return Promise.resolve(receipt);
|
|
291
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* 生成 swapAndBridge 的 calldata
|
|
318
|
+
*/
|
|
319
|
+
genSwapAndBridgeCalldata(params) {
|
|
320
|
+
this.validateParams(params);
|
|
321
|
+
if (!params.path || params.path.length === 0) {
|
|
322
|
+
throw new Error("Swap path not provided");
|
|
323
|
+
}
|
|
324
|
+
const aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
325
|
+
const swapParams = params.path.map((pathItem) => ({
|
|
326
|
+
dexType: pathItem.dexType,
|
|
327
|
+
pool: pathItem.poolAddress,
|
|
328
|
+
fromCoin: pathItem.fromCoinAddress,
|
|
329
|
+
toCoin: pathItem.toCoinAddress,
|
|
330
|
+
extra: pathItem.extra ?? "0x"
|
|
331
|
+
}));
|
|
332
|
+
const bridgeParam = {
|
|
333
|
+
bridge: params.bridgeType,
|
|
334
|
+
token: params.tokenAddress,
|
|
335
|
+
amount: params.amountInWei,
|
|
336
|
+
bridgeAddress: params.bridgeAddress,
|
|
337
|
+
destChain: params.destChain,
|
|
338
|
+
destUser: params.destUser,
|
|
339
|
+
extra: params.extra ?? "0x"
|
|
340
|
+
};
|
|
341
|
+
const iface = new import_ethers.ethers.Interface(import_aggregator.default);
|
|
342
|
+
const calldata = iface.encodeFunctionData("swapAndBridge", [params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeParam]);
|
|
343
|
+
return {
|
|
344
|
+
to: aggregatorAddress,
|
|
345
|
+
data: calldata,
|
|
346
|
+
value: params.bridgeFee
|
|
347
|
+
};
|
|
348
|
+
}
|
|
292
349
|
async checkIsEnoughToken(fromTokenAddress, userAddress, amountInWei, aggregatorAddress, wallet) {
|
|
293
350
|
const erc20 = new import_ethers2.Contract(fromTokenAddress, ["function balanceOf(address) view returns (uint256)", "function allowance(address, address) view returns (uint256)"], wallet);
|
|
294
351
|
const userBalance = await erc20.balanceOf(userAddress);
|
|
@@ -327,6 +384,7 @@ var src_default = Hermes;
|
|
|
327
384
|
// Annotate the CommonJS export names for ESM import in node:
|
|
328
385
|
0 && (module.exports = {
|
|
329
386
|
AddressConst,
|
|
387
|
+
BridgeType,
|
|
330
388
|
ChainNameEnum,
|
|
331
389
|
DexType,
|
|
332
390
|
Hermes
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, IExpectPayload, IRouterPath } from './types.js';
|
|
2
|
-
import { ChainNameEnum, AddressConst, DexType } from './types.js';
|
|
2
|
+
import { ChainNameEnum, AddressConst, DexType, BridgeType } from './types.js';
|
|
3
3
|
export type { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, IExpectPayload, IRouterPath };
|
|
4
|
-
export { ChainNameEnum, AddressConst, DexType };
|
|
4
|
+
export { ChainNameEnum, AddressConst, DexType, BridgeType };
|
|
5
5
|
declare class Hermes {
|
|
6
6
|
private config;
|
|
7
7
|
private providerMap;
|
|
@@ -11,9 +11,24 @@ declare class Hermes {
|
|
|
11
11
|
constructor(config: IConfig);
|
|
12
12
|
expect(params: IExpectParams): Promise<bigint>;
|
|
13
13
|
swap(params: ISwapParams): Promise<IReceipt>;
|
|
14
|
+
/**
|
|
15
|
+
* 生成 swap 的 calldata
|
|
16
|
+
*/
|
|
17
|
+
genSwapCalldata(params: ISwapParams): {
|
|
18
|
+
to: string;
|
|
19
|
+
data: string;
|
|
20
|
+
};
|
|
14
21
|
bridge(params: IBridgeParams): Promise<IReceipt>;
|
|
15
22
|
estimateBridgeFee(params: IBridgeParams): Promise<bigint>;
|
|
16
23
|
swapAndBridge(params: ISwapAndBridgeParams): Promise<IReceipt>;
|
|
24
|
+
/**
|
|
25
|
+
* 生成 swapAndBridge 的 calldata
|
|
26
|
+
*/
|
|
27
|
+
genSwapAndBridgeCalldata(params: ISwapAndBridgeParams): {
|
|
28
|
+
to: string;
|
|
29
|
+
data: string;
|
|
30
|
+
value: bigint;
|
|
31
|
+
};
|
|
17
32
|
private checkIsEnoughToken;
|
|
18
33
|
private validateParams;
|
|
19
34
|
private getQuoterAddress;
|
package/dist/esm/index.js
CHANGED
|
@@ -15,7 +15,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
15
15
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
16
16
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
17
17
|
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); }
|
|
18
|
-
import { ChainNameEnum, AddressConst, DexType } from "./types.js";
|
|
18
|
+
import { ChainNameEnum, AddressConst, DexType, BridgeType } from "./types.js";
|
|
19
19
|
import { ethers } from 'ethers';
|
|
20
20
|
import { Contract } from 'ethers';
|
|
21
21
|
import QuoterAbi from "./abis/quoter.js";
|
|
@@ -23,7 +23,7 @@ import AggregatorAbi from "./abis/aggregator.js";
|
|
|
23
23
|
|
|
24
24
|
// 导出所有类型定义,方便用户使用
|
|
25
25
|
|
|
26
|
-
export { ChainNameEnum, AddressConst, DexType };
|
|
26
|
+
export { ChainNameEnum, AddressConst, DexType, BridgeType };
|
|
27
27
|
var Hermes = /*#__PURE__*/function () {
|
|
28
28
|
function Hermes(config) {
|
|
29
29
|
_classCallCheck(this, Hermes);
|
|
@@ -268,6 +268,39 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
268
268
|
}
|
|
269
269
|
return swap;
|
|
270
270
|
}()
|
|
271
|
+
/**
|
|
272
|
+
* 生成 swap 的 calldata
|
|
273
|
+
*/
|
|
274
|
+
}, {
|
|
275
|
+
key: "genSwapCalldata",
|
|
276
|
+
value: function genSwapCalldata(params) {
|
|
277
|
+
this.validateParams(params);
|
|
278
|
+
if (!params.path || params.path.length === 0) {
|
|
279
|
+
throw new Error('Swap path not provided');
|
|
280
|
+
}
|
|
281
|
+
var aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
282
|
+
|
|
283
|
+
// 准备 swap 参数
|
|
284
|
+
var swapParams = params.path.map(function (pathItem) {
|
|
285
|
+
var _pathItem$extra2;
|
|
286
|
+
return {
|
|
287
|
+
dexType: pathItem.dexType,
|
|
288
|
+
pool: pathItem.poolAddress,
|
|
289
|
+
fromCoin: pathItem.fromCoinAddress,
|
|
290
|
+
toCoin: pathItem.toCoinAddress,
|
|
291
|
+
extra: (_pathItem$extra2 = pathItem.extra) !== null && _pathItem$extra2 !== void 0 ? _pathItem$extra2 : '0x'
|
|
292
|
+
};
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// 使用 ethers Interface 编码 calldata
|
|
296
|
+
// 参数顺序: user, amountIn, swapParams, minAmountOutList
|
|
297
|
+
var iface = new ethers.Interface(AggregatorAbi);
|
|
298
|
+
var calldata = iface.encodeFunctionData('swap', [params.user, params.amountInWei, swapParams, params.minAmountOutList]);
|
|
299
|
+
return {
|
|
300
|
+
to: aggregatorAddress,
|
|
301
|
+
data: calldata
|
|
302
|
+
};
|
|
303
|
+
}
|
|
271
304
|
}, {
|
|
272
305
|
key: "bridge",
|
|
273
306
|
value: function () {
|
|
@@ -458,13 +491,13 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
458
491
|
case 9:
|
|
459
492
|
// 准备合约参数
|
|
460
493
|
swapParams = params.path.map(function (pathItem) {
|
|
461
|
-
var _pathItem$
|
|
494
|
+
var _pathItem$extra3;
|
|
462
495
|
return {
|
|
463
496
|
dexType: pathItem.dexType,
|
|
464
497
|
pool: pathItem.poolAddress,
|
|
465
498
|
fromCoin: pathItem.fromCoinAddress,
|
|
466
499
|
toCoin: pathItem.toCoinAddress,
|
|
467
|
-
extra: (_pathItem$
|
|
500
|
+
extra: (_pathItem$extra3 = pathItem.extra) !== null && _pathItem$extra3 !== void 0 ? _pathItem$extra3 : '0x'
|
|
468
501
|
};
|
|
469
502
|
});
|
|
470
503
|
bridgeArgs = {
|
|
@@ -527,6 +560,52 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
527
560
|
}
|
|
528
561
|
return swapAndBridge;
|
|
529
562
|
}()
|
|
563
|
+
/**
|
|
564
|
+
* 生成 swapAndBridge 的 calldata
|
|
565
|
+
*/
|
|
566
|
+
}, {
|
|
567
|
+
key: "genSwapAndBridgeCalldata",
|
|
568
|
+
value: function genSwapAndBridgeCalldata(params) {
|
|
569
|
+
var _params$extra4;
|
|
570
|
+
this.validateParams(params);
|
|
571
|
+
if (!params.path || params.path.length === 0) {
|
|
572
|
+
throw new Error('Swap path not provided');
|
|
573
|
+
}
|
|
574
|
+
var aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
575
|
+
|
|
576
|
+
// 准备 swap 参数
|
|
577
|
+
var swapParams = params.path.map(function (pathItem) {
|
|
578
|
+
var _pathItem$extra4;
|
|
579
|
+
return {
|
|
580
|
+
dexType: pathItem.dexType,
|
|
581
|
+
pool: pathItem.poolAddress,
|
|
582
|
+
fromCoin: pathItem.fromCoinAddress,
|
|
583
|
+
toCoin: pathItem.toCoinAddress,
|
|
584
|
+
extra: (_pathItem$extra4 = pathItem.extra) !== null && _pathItem$extra4 !== void 0 ? _pathItem$extra4 : '0x'
|
|
585
|
+
};
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
// 准备 bridge 参数(对应合约中的 BridgeParam struct)
|
|
589
|
+
var bridgeParam = {
|
|
590
|
+
bridge: params.bridgeType,
|
|
591
|
+
token: params.tokenAddress,
|
|
592
|
+
amount: params.amountInWei,
|
|
593
|
+
bridgeAddress: params.bridgeAddress,
|
|
594
|
+
destChain: params.destChain,
|
|
595
|
+
destUser: params.destUser,
|
|
596
|
+
extra: (_params$extra4 = params.extra) !== null && _params$extra4 !== void 0 ? _params$extra4 : '0x'
|
|
597
|
+
};
|
|
598
|
+
|
|
599
|
+
// 使用 ethers Interface 编码 calldata
|
|
600
|
+
// 参数顺序: user, amountIn, swapParams, minAmountOutList, bridgeParam
|
|
601
|
+
var iface = new ethers.Interface(AggregatorAbi);
|
|
602
|
+
var calldata = iface.encodeFunctionData('swapAndBridge', [params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeParam]);
|
|
603
|
+
return {
|
|
604
|
+
to: aggregatorAddress,
|
|
605
|
+
data: calldata,
|
|
606
|
+
value: params.bridgeFee
|
|
607
|
+
};
|
|
608
|
+
}
|
|
530
609
|
}, {
|
|
531
610
|
key: "checkIsEnoughToken",
|
|
532
611
|
value: function () {
|