hermes-swap 0.0.24 → 0.0.26
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 +61 -0
- package/dist/cjs/types.d.ts +5 -0
- package/dist/esm/index.d.ts +17 -2
- package/dist/esm/index.js +86 -4
- package/dist/esm/types.d.ts +5 -0
- 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,30 @@ 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 amountIn = params.amountInWei;
|
|
173
|
+
const iface = new import_ethers.ethers.Interface(import_aggregator.default);
|
|
174
|
+
const calldata = iface.encodeFunctionData("swap", [params.user, amountIn, swapParams, params.minAmountOutList]);
|
|
175
|
+
return {
|
|
176
|
+
to: aggregatorAddress,
|
|
177
|
+
data: calldata
|
|
178
|
+
};
|
|
179
|
+
}
|
|
155
180
|
async bridge(params) {
|
|
156
181
|
this.validateParams(params);
|
|
157
182
|
const aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
@@ -289,6 +314,41 @@ var Hermes = class {
|
|
|
289
314
|
};
|
|
290
315
|
return Promise.resolve(receipt);
|
|
291
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* 生成 swapAndBridge 的 calldata
|
|
319
|
+
*/
|
|
320
|
+
genSwapAndBridgeCalldata(params) {
|
|
321
|
+
this.validateParams(params);
|
|
322
|
+
if (!params.path || params.path.length === 0) {
|
|
323
|
+
throw new Error("Swap path not provided");
|
|
324
|
+
}
|
|
325
|
+
const aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
326
|
+
const swapParams = params.path.map((pathItem) => ({
|
|
327
|
+
dexType: pathItem.dexType,
|
|
328
|
+
pool: pathItem.poolAddress,
|
|
329
|
+
fromCoin: pathItem.fromCoinAddress,
|
|
330
|
+
toCoin: pathItem.toCoinAddress,
|
|
331
|
+
extra: pathItem.extra ?? "0x"
|
|
332
|
+
}));
|
|
333
|
+
const bridgeParams = {
|
|
334
|
+
bridge: params.bridgeType,
|
|
335
|
+
bridgeParam: {
|
|
336
|
+
token: params.tokenAddress,
|
|
337
|
+
amount: params.amountInWei,
|
|
338
|
+
bridgeAddress: params.bridgeAddress,
|
|
339
|
+
refundAddress: params.destUser,
|
|
340
|
+
destinationChain: params.destChain,
|
|
341
|
+
adapterParams: params.extra ?? "0x"
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
const iface = new import_ethers.ethers.Interface(import_aggregator.default);
|
|
345
|
+
const calldata = iface.encodeFunctionData("swapAndBridge", [params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeParams]);
|
|
346
|
+
return {
|
|
347
|
+
to: aggregatorAddress,
|
|
348
|
+
data: calldata,
|
|
349
|
+
value: params.bridgeFee
|
|
350
|
+
};
|
|
351
|
+
}
|
|
292
352
|
async checkIsEnoughToken(fromTokenAddress, userAddress, amountInWei, aggregatorAddress, wallet) {
|
|
293
353
|
const erc20 = new import_ethers2.Contract(fromTokenAddress, ["function balanceOf(address) view returns (uint256)", "function allowance(address, address) view returns (uint256)"], wallet);
|
|
294
354
|
const userBalance = await erc20.balanceOf(userAddress);
|
|
@@ -327,6 +387,7 @@ var src_default = Hermes;
|
|
|
327
387
|
// Annotate the CommonJS export names for ESM import in node:
|
|
328
388
|
0 && (module.exports = {
|
|
329
389
|
AddressConst,
|
|
390
|
+
BridgeType,
|
|
330
391
|
ChainNameEnum,
|
|
331
392
|
DexType,
|
|
332
393
|
Hermes
|
package/dist/cjs/types.d.ts
CHANGED
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,40 @@ 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
|
+
var amountIn = params.amountInWei;
|
|
295
|
+
|
|
296
|
+
// 使用 ethers Interface 编码 calldata
|
|
297
|
+
// 参数顺序: user, amountIn, swapParams, minAmountOutList
|
|
298
|
+
var iface = new ethers.Interface(AggregatorAbi);
|
|
299
|
+
var calldata = iface.encodeFunctionData('swap', [params.user, amountIn, swapParams, params.minAmountOutList]);
|
|
300
|
+
return {
|
|
301
|
+
to: aggregatorAddress,
|
|
302
|
+
data: calldata
|
|
303
|
+
};
|
|
304
|
+
}
|
|
271
305
|
}, {
|
|
272
306
|
key: "bridge",
|
|
273
307
|
value: function () {
|
|
@@ -458,13 +492,13 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
458
492
|
case 9:
|
|
459
493
|
// 准备合约参数
|
|
460
494
|
swapParams = params.path.map(function (pathItem) {
|
|
461
|
-
var _pathItem$
|
|
495
|
+
var _pathItem$extra3;
|
|
462
496
|
return {
|
|
463
497
|
dexType: pathItem.dexType,
|
|
464
498
|
pool: pathItem.poolAddress,
|
|
465
499
|
fromCoin: pathItem.fromCoinAddress,
|
|
466
500
|
toCoin: pathItem.toCoinAddress,
|
|
467
|
-
extra: (_pathItem$
|
|
501
|
+
extra: (_pathItem$extra3 = pathItem.extra) !== null && _pathItem$extra3 !== void 0 ? _pathItem$extra3 : '0x'
|
|
468
502
|
};
|
|
469
503
|
});
|
|
470
504
|
bridgeArgs = {
|
|
@@ -527,6 +561,54 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
527
561
|
}
|
|
528
562
|
return swapAndBridge;
|
|
529
563
|
}()
|
|
564
|
+
/**
|
|
565
|
+
* 生成 swapAndBridge 的 calldata
|
|
566
|
+
*/
|
|
567
|
+
}, {
|
|
568
|
+
key: "genSwapAndBridgeCalldata",
|
|
569
|
+
value: function genSwapAndBridgeCalldata(params) {
|
|
570
|
+
var _params$extra4;
|
|
571
|
+
this.validateParams(params);
|
|
572
|
+
if (!params.path || params.path.length === 0) {
|
|
573
|
+
throw new Error('Swap path not provided');
|
|
574
|
+
}
|
|
575
|
+
var aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
576
|
+
|
|
577
|
+
// 准备 swap 参数
|
|
578
|
+
var swapParams = params.path.map(function (pathItem) {
|
|
579
|
+
var _pathItem$extra4;
|
|
580
|
+
return {
|
|
581
|
+
dexType: pathItem.dexType,
|
|
582
|
+
pool: pathItem.poolAddress,
|
|
583
|
+
fromCoin: pathItem.fromCoinAddress,
|
|
584
|
+
toCoin: pathItem.toCoinAddress,
|
|
585
|
+
extra: (_pathItem$extra4 = pathItem.extra) !== null && _pathItem$extra4 !== void 0 ? _pathItem$extra4 : '0x'
|
|
586
|
+
};
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
// 准备 bridge 参数(对应合约中的 BridgeParam struct)
|
|
590
|
+
var bridgeParams = {
|
|
591
|
+
bridge: params.bridgeType,
|
|
592
|
+
bridgeParam: {
|
|
593
|
+
token: params.tokenAddress,
|
|
594
|
+
amount: params.amountInWei,
|
|
595
|
+
bridgeAddress: params.bridgeAddress,
|
|
596
|
+
refundAddress: params.destUser,
|
|
597
|
+
destinationChain: params.destChain,
|
|
598
|
+
adapterParams: (_params$extra4 = params.extra) !== null && _params$extra4 !== void 0 ? _params$extra4 : '0x'
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
// 使用 ethers Interface 编码 calldata
|
|
603
|
+
// 参数顺序: user, amountIn, swapParams, minAmountOutList, bridgeParam
|
|
604
|
+
var iface = new ethers.Interface(AggregatorAbi);
|
|
605
|
+
var calldata = iface.encodeFunctionData('swapAndBridge', [params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeParams]);
|
|
606
|
+
return {
|
|
607
|
+
to: aggregatorAddress,
|
|
608
|
+
data: calldata,
|
|
609
|
+
value: params.bridgeFee
|
|
610
|
+
};
|
|
611
|
+
}
|
|
530
612
|
}, {
|
|
531
613
|
key: "checkIsEnoughToken",
|
|
532
614
|
value: function () {
|
package/dist/esm/types.d.ts
CHANGED