hermes-swap 0.0.15 → 0.0.17
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/README.md +101 -12
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/index.js +41 -18
- package/dist/cjs/types.d.ts +0 -1
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +24 -27
- package/dist/esm/types.d.ts +0 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,26 +1,115 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Hermes Swap
|
|
2
2
|
|
|
3
|
-
[](https://npmjs.com/package/hermes)
|
|
4
|
-
[](https://npmjs.com/package/hermes)
|
|
3
|
+
[](https://npmjs.com/package/hermes-swap)
|
|
4
|
+
[](https://npmjs.com/package/hermes-swap)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
一个用于去中心化交易所 (DEX) 兑换和跨链桥接的 TypeScript 工具库。
|
|
7
|
+
|
|
8
|
+
## 特性
|
|
9
|
+
|
|
10
|
+
- ✅ 完整的 TypeScript 支持
|
|
11
|
+
- ✅ 支持多链 DEX 兑换
|
|
12
|
+
- ✅ 支持多跳路由兑换
|
|
13
|
+
- ✅ 提供兑换预期查询
|
|
14
|
+
- 🚧 跨链桥接功能(开发中)
|
|
15
|
+
- 🚧 兑换+桥接组合功能(开发中)
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
7
18
|
|
|
8
19
|
```bash
|
|
9
|
-
|
|
20
|
+
npm install hermes-swap
|
|
21
|
+
# 或
|
|
22
|
+
yarn add hermes-swap
|
|
10
23
|
```
|
|
11
24
|
|
|
25
|
+
## 快速开始
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import Hermes, { IConfig, ChainNameEnum } from 'hermes-swap';
|
|
29
|
+
|
|
30
|
+
// 配置
|
|
31
|
+
const config: IConfig = {
|
|
32
|
+
rpc: {
|
|
33
|
+
[ChainNameEnum.ETH]: {
|
|
34
|
+
url: 'https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY',
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
quoterAddress: {
|
|
38
|
+
[ChainNameEnum.ETH]: '0xQuoterAddress...'
|
|
39
|
+
},
|
|
40
|
+
aggregatorAddress: {
|
|
41
|
+
[ChainNameEnum.ETH]: '0xAggregatorAddress...'
|
|
42
|
+
},
|
|
43
|
+
executorMap: {
|
|
44
|
+
[ChainNameEnum.ETH]: 'executor-private-key'
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// 初始化
|
|
49
|
+
const hermes = new Hermes(config);
|
|
50
|
+
|
|
51
|
+
// 查询预期输出
|
|
52
|
+
const expectedAmount = await hermes.expect({
|
|
53
|
+
chain: ChainNameEnum.ETH,
|
|
54
|
+
amountInWei: BigInt('1000000000000000000'),
|
|
55
|
+
path: [/* 路由路径 */]
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// 执行兑换
|
|
59
|
+
const receipt = await hermes.swap({
|
|
60
|
+
user: '0xUserAddress...',
|
|
61
|
+
chain: ChainNameEnum.ETH,
|
|
62
|
+
amountInWei: BigInt('1000000000000000000'),
|
|
63
|
+
path: [/* 路由路径 */],
|
|
64
|
+
minAmountOutList: [/* 最小输出量 */]
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 详细文档
|
|
69
|
+
|
|
70
|
+
查看 [USAGE.md](./USAGE.md) 了解完整的使用指南和示例。
|
|
71
|
+
|
|
72
|
+
## 导出内容
|
|
73
|
+
|
|
74
|
+
### 主类
|
|
75
|
+
- `Hermes` - 主类(默认导出和命名导出)
|
|
76
|
+
|
|
77
|
+
### 类型定义
|
|
78
|
+
- `IConfig`, `ISwapParams`, `IExpectParams`, `IBridgeParams`, `ISwapAndBridgeParams`
|
|
79
|
+
- `IReceipt`, `IRouterPath`, `IExpectPayload`
|
|
80
|
+
|
|
81
|
+
### 枚举
|
|
82
|
+
- `ChainNameEnum` - 支持的区块链网络
|
|
83
|
+
- `DexType` - DEX 类型
|
|
84
|
+
|
|
85
|
+
### 常量
|
|
86
|
+
- `AddressConst` - 常用代币地址
|
|
87
|
+
|
|
88
|
+
## 开发
|
|
89
|
+
|
|
12
90
|
```bash
|
|
13
|
-
|
|
14
|
-
|
|
91
|
+
# 安装依赖
|
|
92
|
+
yarn install
|
|
93
|
+
|
|
94
|
+
# 开发模式
|
|
95
|
+
npm run dev
|
|
96
|
+
|
|
97
|
+
# 构建
|
|
98
|
+
npm run build
|
|
99
|
+
|
|
100
|
+
# 测试
|
|
101
|
+
npm test
|
|
102
|
+
|
|
103
|
+
# 测试覆盖率
|
|
104
|
+
npm run test:coverage
|
|
15
105
|
```
|
|
16
106
|
|
|
17
|
-
##
|
|
107
|
+
## 测试
|
|
18
108
|
|
|
19
|
-
|
|
109
|
+
```bash
|
|
110
|
+
npm test
|
|
111
|
+
```
|
|
20
112
|
|
|
21
113
|
## LICENSE
|
|
22
114
|
|
|
23
115
|
MIT
|
|
24
|
-
# hermes
|
|
25
|
-
|
|
26
|
-
测试: npm test
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig } from './types';
|
|
1
|
+
import { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, ChainNameEnum, AddressConst, IExpectPayload, DexType, IRouterPath } from './types';
|
|
2
|
+
export { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, ChainNameEnum, AddressConst, IExpectPayload, DexType, IRouterPath };
|
|
2
3
|
declare class Hermes {
|
|
3
4
|
private config;
|
|
4
5
|
private providerMap;
|
|
5
6
|
private walletMap;
|
|
6
7
|
private quoterAddressMap;
|
|
7
8
|
private aggregatorAddressMap;
|
|
8
|
-
private executorMap;
|
|
9
9
|
constructor(config: IConfig);
|
|
10
10
|
expect(params: IExpectParams): Promise<bigint>;
|
|
11
11
|
swap(params: ISwapParams): Promise<IReceipt>;
|
|
@@ -16,3 +16,4 @@ declare class Hermes {
|
|
|
16
16
|
private getAggregatorAddress;
|
|
17
17
|
}
|
|
18
18
|
export default Hermes;
|
|
19
|
+
export { Hermes };
|
package/dist/cjs/index.js
CHANGED
|
@@ -29,9 +29,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
// src/index.ts
|
|
30
30
|
var src_exports = {};
|
|
31
31
|
__export(src_exports, {
|
|
32
|
+
AddressConst: () => import_types.AddressConst,
|
|
33
|
+
ChainNameEnum: () => import_types.ChainNameEnum,
|
|
34
|
+
DexType: () => import_types.DexType,
|
|
35
|
+
Hermes: () => Hermes,
|
|
36
|
+
IBridgeParams: () => import_types.IBridgeParams,
|
|
37
|
+
IConfig: () => import_types.IConfig,
|
|
38
|
+
IExpectParams: () => import_types.IExpectParams,
|
|
39
|
+
IExpectPayload: () => import_types.IExpectPayload,
|
|
40
|
+
IReceipt: () => import_types.IReceipt,
|
|
41
|
+
IRouterPath: () => import_types.IRouterPath,
|
|
42
|
+
ISwapAndBridgeParams: () => import_types.ISwapAndBridgeParams,
|
|
43
|
+
ISwapParams: () => import_types.ISwapParams,
|
|
32
44
|
default: () => src_default
|
|
33
45
|
});
|
|
34
46
|
module.exports = __toCommonJS(src_exports);
|
|
47
|
+
var import_types = require("./types");
|
|
35
48
|
var import_ethers = require("ethers");
|
|
36
49
|
var import_ethers2 = require("ethers");
|
|
37
50
|
var import_quoter = __toESM(require("./abis/quoter"));
|
|
@@ -42,37 +55,32 @@ var Hermes = class {
|
|
|
42
55
|
this.walletMap = /* @__PURE__ */ new Map();
|
|
43
56
|
this.quoterAddressMap = /* @__PURE__ */ new Map();
|
|
44
57
|
this.aggregatorAddressMap = /* @__PURE__ */ new Map();
|
|
45
|
-
this.executorMap = /* @__PURE__ */ new Map();
|
|
46
58
|
this.config = config;
|
|
47
59
|
for (const [chainName, rpc] of Object.entries(this.config.rpc)) {
|
|
48
60
|
const provider = new import_ethers.ethers.JsonRpcProvider(rpc.url);
|
|
49
61
|
this.providerMap.set(chainName, provider);
|
|
50
62
|
if (rpc.privateKey) {
|
|
51
|
-
this.walletMap.set(chainName, new import_ethers.ethers.Wallet(rpc.privateKey));
|
|
63
|
+
this.walletMap.set(chainName, new import_ethers.ethers.Wallet(rpc.privateKey, provider));
|
|
52
64
|
}
|
|
53
65
|
}
|
|
66
|
+
console.log(Object.entries(this.config.rpc), this.walletMap);
|
|
54
67
|
for (const [chainName, quoterAddress] of Object.entries(this.config.quoterAddress)) {
|
|
55
68
|
this.quoterAddressMap.set(chainName, quoterAddress);
|
|
56
69
|
}
|
|
57
70
|
for (const [chainName, aggregatorAddress] of Object.entries(this.config.aggregatorAddress)) {
|
|
58
71
|
this.aggregatorAddressMap.set(chainName, aggregatorAddress);
|
|
59
72
|
}
|
|
60
|
-
for (const [chainName, executorPrivateKey] of Object.entries(this.config.executorMap)) {
|
|
61
|
-
const provider = this.providerMap.get(chainName);
|
|
62
|
-
const executor = new import_ethers.ethers.Wallet(executorPrivateKey, provider);
|
|
63
|
-
this.executorMap.set(chainName, executor);
|
|
64
|
-
}
|
|
65
73
|
}
|
|
66
74
|
async expect(params) {
|
|
67
|
-
const
|
|
68
|
-
if (!
|
|
69
|
-
throw new Error(`
|
|
75
|
+
const wallet = this.walletMap.get(params.chain);
|
|
76
|
+
if (!wallet) {
|
|
77
|
+
throw new Error(`Wallet not configured for chain: ${params.chain}`);
|
|
70
78
|
}
|
|
71
79
|
const address = this.getQuoterAddress(params.chain);
|
|
72
80
|
if (!address) {
|
|
73
81
|
throw new Error(`Quoter address not found for chain: ${params.chain}`);
|
|
74
82
|
}
|
|
75
|
-
const quoter = new import_ethers2.Contract(address, import_quoter.default,
|
|
83
|
+
const quoter = new import_ethers2.Contract(address, import_quoter.default, wallet);
|
|
76
84
|
const quoteParams = params.path.map((p) => ({
|
|
77
85
|
dexType: p.dexType,
|
|
78
86
|
pool: p.poolAddress,
|
|
@@ -91,11 +99,11 @@ var Hermes = class {
|
|
|
91
99
|
const fromTokenAddress = params.path[0].fromCoinAddress;
|
|
92
100
|
const toTokenAddress = params.path[params.path.length - 1].toCoinAddress;
|
|
93
101
|
const aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
94
|
-
const
|
|
95
|
-
if (!
|
|
96
|
-
throw new Error(`
|
|
102
|
+
const wallet = this.walletMap.get(params.chain);
|
|
103
|
+
if (!wallet) {
|
|
104
|
+
throw new Error(`Wallet not configured for chain: ${params.chain}`);
|
|
97
105
|
}
|
|
98
|
-
const aggregator = new import_ethers2.Contract(aggregatorAddress, import_aggregator.default,
|
|
106
|
+
const aggregator = new import_ethers2.Contract(aggregatorAddress, import_aggregator.default, wallet);
|
|
99
107
|
const swapParams = params.path.map((pathItem) => ({
|
|
100
108
|
dexType: pathItem.dexType,
|
|
101
109
|
pool: pathItem.poolAddress,
|
|
@@ -103,7 +111,7 @@ var Hermes = class {
|
|
|
103
111
|
toCoin: pathItem.toCoinAddress,
|
|
104
112
|
extra: pathItem.extra ?? "0x"
|
|
105
113
|
}));
|
|
106
|
-
const erc20 = new import_ethers2.Contract(fromTokenAddress, ["function balanceOf(address) view returns (uint256)", "function allowance(address, address) view returns (uint256)"],
|
|
114
|
+
const erc20 = new import_ethers2.Contract(fromTokenAddress, ["function balanceOf(address) view returns (uint256)", "function allowance(address, address) view returns (uint256)"], wallet);
|
|
107
115
|
const userBalance = await erc20.balanceOf(params.user);
|
|
108
116
|
if (userBalance < params.amountInWei) {
|
|
109
117
|
throw new Error("Insufficient balance for swap");
|
|
@@ -114,7 +122,7 @@ var Hermes = class {
|
|
|
114
122
|
throw new Error("Insufficient allowance token amount for swap");
|
|
115
123
|
}
|
|
116
124
|
try {
|
|
117
|
-
const gas = await aggregator.getFunction("swap").estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, { from:
|
|
125
|
+
const gas = await aggregator.getFunction("swap").estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, { from: wallet.address });
|
|
118
126
|
console.log(`Estimated gas for swap: ${BigInt(gas).toString()}`);
|
|
119
127
|
} catch (error) {
|
|
120
128
|
console.warn("Aggregator estimateGas.swap failed", error);
|
|
@@ -122,7 +130,7 @@ var Hermes = class {
|
|
|
122
130
|
}
|
|
123
131
|
let txResponse;
|
|
124
132
|
try {
|
|
125
|
-
txResponse = await aggregator.getFunction("swap")(params.user, params.amountInWei, swapParams, params.minAmountOutList, { from:
|
|
133
|
+
txResponse = await aggregator.getFunction("swap")(params.user, params.amountInWei, swapParams, params.minAmountOutList, { from: wallet.address });
|
|
126
134
|
} catch (error) {
|
|
127
135
|
console.error("Aggregator swap transaction failed", error);
|
|
128
136
|
throw error;
|
|
@@ -182,3 +190,18 @@ var Hermes = class {
|
|
|
182
190
|
}
|
|
183
191
|
};
|
|
184
192
|
var src_default = Hermes;
|
|
193
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
194
|
+
0 && (module.exports = {
|
|
195
|
+
AddressConst,
|
|
196
|
+
ChainNameEnum,
|
|
197
|
+
DexType,
|
|
198
|
+
Hermes,
|
|
199
|
+
IBridgeParams,
|
|
200
|
+
IConfig,
|
|
201
|
+
IExpectParams,
|
|
202
|
+
IExpectPayload,
|
|
203
|
+
IReceipt,
|
|
204
|
+
IRouterPath,
|
|
205
|
+
ISwapAndBridgeParams,
|
|
206
|
+
ISwapParams
|
|
207
|
+
});
|
package/dist/cjs/types.d.ts
CHANGED
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig } from './types';
|
|
1
|
+
import { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, ChainNameEnum, AddressConst, IExpectPayload, DexType, IRouterPath } from './types';
|
|
2
|
+
export { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, ChainNameEnum, AddressConst, IExpectPayload, DexType, IRouterPath };
|
|
2
3
|
declare class Hermes {
|
|
3
4
|
private config;
|
|
4
5
|
private providerMap;
|
|
5
6
|
private walletMap;
|
|
6
7
|
private quoterAddressMap;
|
|
7
8
|
private aggregatorAddressMap;
|
|
8
|
-
private executorMap;
|
|
9
9
|
constructor(config: IConfig);
|
|
10
10
|
expect(params: IExpectParams): Promise<bigint>;
|
|
11
11
|
swap(params: ISwapParams): Promise<IReceipt>;
|
|
@@ -16,3 +16,4 @@ declare class Hermes {
|
|
|
16
16
|
private getAggregatorAddress;
|
|
17
17
|
}
|
|
18
18
|
export default Hermes;
|
|
19
|
+
export { Hermes };
|
package/dist/esm/index.js
CHANGED
|
@@ -15,10 +15,14 @@ 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 { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, ChainNameEnum, AddressConst, IExpectPayload, DexType, IRouterPath } from "./types";
|
|
18
19
|
import { ethers } from 'ethers';
|
|
19
20
|
import { Contract } from 'ethers';
|
|
20
21
|
import QuoterAbi from "./abis/quoter";
|
|
21
22
|
import AggregatorAbi from "./abis/aggregator";
|
|
23
|
+
|
|
24
|
+
// 导出所有类型定义,方便用户使用
|
|
25
|
+
export { IExpectParams, ISwapParams, IBridgeParams, ISwapAndBridgeParams, IReceipt, IConfig, ChainNameEnum, AddressConst, IExpectPayload, DexType, IRouterPath };
|
|
22
26
|
var Hermes = /*#__PURE__*/function () {
|
|
23
27
|
function Hermes(config) {
|
|
24
28
|
_classCallCheck(this, Hermes);
|
|
@@ -27,7 +31,6 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
27
31
|
_defineProperty(this, "walletMap", new Map());
|
|
28
32
|
_defineProperty(this, "quoterAddressMap", new Map());
|
|
29
33
|
_defineProperty(this, "aggregatorAddressMap", new Map());
|
|
30
|
-
_defineProperty(this, "executorMap", new Map());
|
|
31
34
|
this.config = config;
|
|
32
35
|
for (var _i = 0, _Object$entries = Object.entries(this.config.rpc); _i < _Object$entries.length; _i++) {
|
|
33
36
|
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
@@ -36,9 +39,10 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
36
39
|
var provider = new ethers.JsonRpcProvider(rpc.url);
|
|
37
40
|
this.providerMap.set(chainName, provider);
|
|
38
41
|
if (rpc.privateKey) {
|
|
39
|
-
this.walletMap.set(chainName, new ethers.Wallet(rpc.privateKey));
|
|
42
|
+
this.walletMap.set(chainName, new ethers.Wallet(rpc.privateKey, provider));
|
|
40
43
|
}
|
|
41
44
|
}
|
|
45
|
+
console.log(Object.entries(this.config.rpc), this.walletMap);
|
|
42
46
|
for (var _i2 = 0, _Object$entries2 = Object.entries(this.config.quoterAddress); _i2 < _Object$entries2.length; _i2++) {
|
|
43
47
|
var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
|
|
44
48
|
_chainName = _Object$entries2$_i[0],
|
|
@@ -51,32 +55,22 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
51
55
|
aggregatorAddress = _Object$entries3$_i[1];
|
|
52
56
|
this.aggregatorAddressMap.set(_chainName2, aggregatorAddress);
|
|
53
57
|
}
|
|
54
|
-
|
|
55
|
-
// load executor
|
|
56
|
-
for (var _i4 = 0, _Object$entries4 = Object.entries(this.config.executorMap); _i4 < _Object$entries4.length; _i4++) {
|
|
57
|
-
var _Object$entries4$_i = _slicedToArray(_Object$entries4[_i4], 2),
|
|
58
|
-
_chainName3 = _Object$entries4$_i[0],
|
|
59
|
-
executorPrivateKey = _Object$entries4$_i[1];
|
|
60
|
-
var _provider = this.providerMap.get(_chainName3);
|
|
61
|
-
var executor = new ethers.Wallet(executorPrivateKey, _provider);
|
|
62
|
-
this.executorMap.set(_chainName3, executor);
|
|
63
|
-
}
|
|
64
58
|
}
|
|
65
59
|
_createClass(Hermes, [{
|
|
66
60
|
key: "expect",
|
|
67
61
|
value: function () {
|
|
68
62
|
var _expect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
|
|
69
|
-
var
|
|
63
|
+
var wallet, address, quoter, quoteParams, amountOutList;
|
|
70
64
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
71
65
|
while (1) switch (_context.prev = _context.next) {
|
|
72
66
|
case 0:
|
|
73
67
|
// 调用合约
|
|
74
|
-
|
|
75
|
-
if (
|
|
68
|
+
wallet = this.walletMap.get(params.chain);
|
|
69
|
+
if (wallet) {
|
|
76
70
|
_context.next = 3;
|
|
77
71
|
break;
|
|
78
72
|
}
|
|
79
|
-
throw new Error("
|
|
73
|
+
throw new Error("Wallet not configured for chain: ".concat(params.chain));
|
|
80
74
|
case 3:
|
|
81
75
|
address = this.getQuoterAddress(params.chain);
|
|
82
76
|
if (address) {
|
|
@@ -85,7 +79,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
85
79
|
}
|
|
86
80
|
throw new Error("Quoter address not found for chain: ".concat(params.chain));
|
|
87
81
|
case 6:
|
|
88
|
-
quoter = new Contract(address, QuoterAbi,
|
|
82
|
+
quoter = new Contract(address, QuoterAbi, wallet); // 转换字段名以匹配合约 ABI
|
|
89
83
|
quoteParams = params.path.map(function (p) {
|
|
90
84
|
return {
|
|
91
85
|
dexType: p.dexType,
|
|
@@ -115,7 +109,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
115
109
|
key: "swap",
|
|
116
110
|
value: function () {
|
|
117
111
|
var _swap = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(params) {
|
|
118
|
-
var fromTokenAddress, toTokenAddress, aggregatorAddress,
|
|
112
|
+
var fromTokenAddress, toTokenAddress, aggregatorAddress, wallet, aggregator, swapParams, erc20, userBalance, currentAllowance, gas, txResponse, receipt, iface, amountOut, _iterator, _step, log, parsed;
|
|
119
113
|
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
120
114
|
while (1) switch (_context2.prev = _context2.next) {
|
|
121
115
|
case 0:
|
|
@@ -129,14 +123,14 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
129
123
|
fromTokenAddress = params.path[0].fromCoinAddress;
|
|
130
124
|
toTokenAddress = params.path[params.path.length - 1].toCoinAddress;
|
|
131
125
|
aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
132
|
-
|
|
133
|
-
if (
|
|
126
|
+
wallet = this.walletMap.get(params.chain);
|
|
127
|
+
if (wallet) {
|
|
134
128
|
_context2.next = 9;
|
|
135
129
|
break;
|
|
136
130
|
}
|
|
137
|
-
throw new Error("
|
|
131
|
+
throw new Error("Wallet not configured for chain: ".concat(params.chain));
|
|
138
132
|
case 9:
|
|
139
|
-
aggregator = new Contract(aggregatorAddress, AggregatorAbi,
|
|
133
|
+
aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
|
|
140
134
|
swapParams = params.path.map(function (pathItem) {
|
|
141
135
|
var _pathItem$extra;
|
|
142
136
|
return {
|
|
@@ -147,7 +141,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
147
141
|
extra: (_pathItem$extra = pathItem.extra) !== null && _pathItem$extra !== void 0 ? _pathItem$extra : '0x'
|
|
148
142
|
};
|
|
149
143
|
});
|
|
150
|
-
erc20 = new Contract(fromTokenAddress, ['function balanceOf(address) view returns (uint256)', 'function allowance(address, address) view returns (uint256)'],
|
|
144
|
+
erc20 = new Contract(fromTokenAddress, ['function balanceOf(address) view returns (uint256)', 'function allowance(address, address) view returns (uint256)'], wallet);
|
|
151
145
|
_context2.next = 14;
|
|
152
146
|
return erc20.balanceOf(params.user);
|
|
153
147
|
case 14:
|
|
@@ -172,7 +166,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
172
166
|
_context2.prev = 23;
|
|
173
167
|
_context2.next = 26;
|
|
174
168
|
return aggregator.getFunction('swap').estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, {
|
|
175
|
-
from:
|
|
169
|
+
from: wallet.address
|
|
176
170
|
});
|
|
177
171
|
case 26:
|
|
178
172
|
gas = _context2.sent;
|
|
@@ -188,7 +182,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
188
182
|
_context2.prev = 34;
|
|
189
183
|
_context2.next = 37;
|
|
190
184
|
return aggregator.getFunction('swap')(params.user, params.amountInWei, swapParams, params.minAmountOutList, {
|
|
191
|
-
from:
|
|
185
|
+
from: wallet.address
|
|
192
186
|
});
|
|
193
187
|
case 37:
|
|
194
188
|
txResponse = _context2.sent;
|
|
@@ -338,5 +332,8 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
338
332
|
}
|
|
339
333
|
}]);
|
|
340
334
|
return Hermes;
|
|
341
|
-
}();
|
|
342
|
-
export default Hermes;
|
|
335
|
+
}(); // 默认导出 Hermes 类
|
|
336
|
+
export default Hermes;
|
|
337
|
+
|
|
338
|
+
// 同时提供命名导出,方便不同使用场景
|
|
339
|
+
export { Hermes };
|
package/dist/esm/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hermes-swap",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17",
|
|
4
4
|
"description": "A TypeScript utility library for swap and bridge",
|
|
5
|
-
"main": "dist/
|
|
6
|
-
"types": "dist/
|
|
5
|
+
"main": "dist/esm/index.js",
|
|
6
|
+
"types": "dist/esm/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"dev": "father dev",
|
|
9
9
|
"build": "father build",
|