hermes-swap 0.0.16 → 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/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +13 -18
- package/dist/cjs/types.d.ts +0 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +15 -25
- package/dist/esm/types.d.ts +0 -1
- package/package.json +1 -1
package/dist/cjs/index.d.ts
CHANGED
package/dist/cjs/index.js
CHANGED
|
@@ -55,37 +55,32 @@ var Hermes = class {
|
|
|
55
55
|
this.walletMap = /* @__PURE__ */ new Map();
|
|
56
56
|
this.quoterAddressMap = /* @__PURE__ */ new Map();
|
|
57
57
|
this.aggregatorAddressMap = /* @__PURE__ */ new Map();
|
|
58
|
-
this.executorMap = /* @__PURE__ */ new Map();
|
|
59
58
|
this.config = config;
|
|
60
59
|
for (const [chainName, rpc] of Object.entries(this.config.rpc)) {
|
|
61
60
|
const provider = new import_ethers.ethers.JsonRpcProvider(rpc.url);
|
|
62
61
|
this.providerMap.set(chainName, provider);
|
|
63
62
|
if (rpc.privateKey) {
|
|
64
|
-
this.walletMap.set(chainName, new import_ethers.ethers.Wallet(rpc.privateKey));
|
|
63
|
+
this.walletMap.set(chainName, new import_ethers.ethers.Wallet(rpc.privateKey, provider));
|
|
65
64
|
}
|
|
66
65
|
}
|
|
66
|
+
console.log(Object.entries(this.config.rpc), this.walletMap);
|
|
67
67
|
for (const [chainName, quoterAddress] of Object.entries(this.config.quoterAddress)) {
|
|
68
68
|
this.quoterAddressMap.set(chainName, quoterAddress);
|
|
69
69
|
}
|
|
70
70
|
for (const [chainName, aggregatorAddress] of Object.entries(this.config.aggregatorAddress)) {
|
|
71
71
|
this.aggregatorAddressMap.set(chainName, aggregatorAddress);
|
|
72
72
|
}
|
|
73
|
-
for (const [chainName, executorPrivateKey] of Object.entries(this.config.executorMap)) {
|
|
74
|
-
const provider = this.providerMap.get(chainName);
|
|
75
|
-
const executor = new import_ethers.ethers.Wallet(executorPrivateKey, provider);
|
|
76
|
-
this.executorMap.set(chainName, executor);
|
|
77
|
-
}
|
|
78
73
|
}
|
|
79
74
|
async expect(params) {
|
|
80
|
-
const
|
|
81
|
-
if (!
|
|
82
|
-
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}`);
|
|
83
78
|
}
|
|
84
79
|
const address = this.getQuoterAddress(params.chain);
|
|
85
80
|
if (!address) {
|
|
86
81
|
throw new Error(`Quoter address not found for chain: ${params.chain}`);
|
|
87
82
|
}
|
|
88
|
-
const quoter = new import_ethers2.Contract(address, import_quoter.default,
|
|
83
|
+
const quoter = new import_ethers2.Contract(address, import_quoter.default, wallet);
|
|
89
84
|
const quoteParams = params.path.map((p) => ({
|
|
90
85
|
dexType: p.dexType,
|
|
91
86
|
pool: p.poolAddress,
|
|
@@ -104,11 +99,11 @@ var Hermes = class {
|
|
|
104
99
|
const fromTokenAddress = params.path[0].fromCoinAddress;
|
|
105
100
|
const toTokenAddress = params.path[params.path.length - 1].toCoinAddress;
|
|
106
101
|
const aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
107
|
-
const
|
|
108
|
-
if (!
|
|
109
|
-
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}`);
|
|
110
105
|
}
|
|
111
|
-
const aggregator = new import_ethers2.Contract(aggregatorAddress, import_aggregator.default,
|
|
106
|
+
const aggregator = new import_ethers2.Contract(aggregatorAddress, import_aggregator.default, wallet);
|
|
112
107
|
const swapParams = params.path.map((pathItem) => ({
|
|
113
108
|
dexType: pathItem.dexType,
|
|
114
109
|
pool: pathItem.poolAddress,
|
|
@@ -116,7 +111,7 @@ var Hermes = class {
|
|
|
116
111
|
toCoin: pathItem.toCoinAddress,
|
|
117
112
|
extra: pathItem.extra ?? "0x"
|
|
118
113
|
}));
|
|
119
|
-
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);
|
|
120
115
|
const userBalance = await erc20.balanceOf(params.user);
|
|
121
116
|
if (userBalance < params.amountInWei) {
|
|
122
117
|
throw new Error("Insufficient balance for swap");
|
|
@@ -127,7 +122,7 @@ var Hermes = class {
|
|
|
127
122
|
throw new Error("Insufficient allowance token amount for swap");
|
|
128
123
|
}
|
|
129
124
|
try {
|
|
130
|
-
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 });
|
|
131
126
|
console.log(`Estimated gas for swap: ${BigInt(gas).toString()}`);
|
|
132
127
|
} catch (error) {
|
|
133
128
|
console.warn("Aggregator estimateGas.swap failed", error);
|
|
@@ -135,7 +130,7 @@ var Hermes = class {
|
|
|
135
130
|
}
|
|
136
131
|
let txResponse;
|
|
137
132
|
try {
|
|
138
|
-
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 });
|
|
139
134
|
} catch (error) {
|
|
140
135
|
console.error("Aggregator swap transaction failed", error);
|
|
141
136
|
throw error;
|
package/dist/cjs/types.d.ts
CHANGED
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -31,7 +31,6 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
31
31
|
_defineProperty(this, "walletMap", new Map());
|
|
32
32
|
_defineProperty(this, "quoterAddressMap", new Map());
|
|
33
33
|
_defineProperty(this, "aggregatorAddressMap", new Map());
|
|
34
|
-
_defineProperty(this, "executorMap", new Map());
|
|
35
34
|
this.config = config;
|
|
36
35
|
for (var _i = 0, _Object$entries = Object.entries(this.config.rpc); _i < _Object$entries.length; _i++) {
|
|
37
36
|
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
@@ -40,9 +39,10 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
40
39
|
var provider = new ethers.JsonRpcProvider(rpc.url);
|
|
41
40
|
this.providerMap.set(chainName, provider);
|
|
42
41
|
if (rpc.privateKey) {
|
|
43
|
-
this.walletMap.set(chainName, new ethers.Wallet(rpc.privateKey));
|
|
42
|
+
this.walletMap.set(chainName, new ethers.Wallet(rpc.privateKey, provider));
|
|
44
43
|
}
|
|
45
44
|
}
|
|
45
|
+
console.log(Object.entries(this.config.rpc), this.walletMap);
|
|
46
46
|
for (var _i2 = 0, _Object$entries2 = Object.entries(this.config.quoterAddress); _i2 < _Object$entries2.length; _i2++) {
|
|
47
47
|
var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
|
|
48
48
|
_chainName = _Object$entries2$_i[0],
|
|
@@ -55,32 +55,22 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
55
55
|
aggregatorAddress = _Object$entries3$_i[1];
|
|
56
56
|
this.aggregatorAddressMap.set(_chainName2, aggregatorAddress);
|
|
57
57
|
}
|
|
58
|
-
|
|
59
|
-
// load executor
|
|
60
|
-
for (var _i4 = 0, _Object$entries4 = Object.entries(this.config.executorMap); _i4 < _Object$entries4.length; _i4++) {
|
|
61
|
-
var _Object$entries4$_i = _slicedToArray(_Object$entries4[_i4], 2),
|
|
62
|
-
_chainName3 = _Object$entries4$_i[0],
|
|
63
|
-
executorPrivateKey = _Object$entries4$_i[1];
|
|
64
|
-
var _provider = this.providerMap.get(_chainName3);
|
|
65
|
-
var executor = new ethers.Wallet(executorPrivateKey, _provider);
|
|
66
|
-
this.executorMap.set(_chainName3, executor);
|
|
67
|
-
}
|
|
68
58
|
}
|
|
69
59
|
_createClass(Hermes, [{
|
|
70
60
|
key: "expect",
|
|
71
61
|
value: function () {
|
|
72
62
|
var _expect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
|
|
73
|
-
var
|
|
63
|
+
var wallet, address, quoter, quoteParams, amountOutList;
|
|
74
64
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
75
65
|
while (1) switch (_context.prev = _context.next) {
|
|
76
66
|
case 0:
|
|
77
67
|
// 调用合约
|
|
78
|
-
|
|
79
|
-
if (
|
|
68
|
+
wallet = this.walletMap.get(params.chain);
|
|
69
|
+
if (wallet) {
|
|
80
70
|
_context.next = 3;
|
|
81
71
|
break;
|
|
82
72
|
}
|
|
83
|
-
throw new Error("
|
|
73
|
+
throw new Error("Wallet not configured for chain: ".concat(params.chain));
|
|
84
74
|
case 3:
|
|
85
75
|
address = this.getQuoterAddress(params.chain);
|
|
86
76
|
if (address) {
|
|
@@ -89,7 +79,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
89
79
|
}
|
|
90
80
|
throw new Error("Quoter address not found for chain: ".concat(params.chain));
|
|
91
81
|
case 6:
|
|
92
|
-
quoter = new Contract(address, QuoterAbi,
|
|
82
|
+
quoter = new Contract(address, QuoterAbi, wallet); // 转换字段名以匹配合约 ABI
|
|
93
83
|
quoteParams = params.path.map(function (p) {
|
|
94
84
|
return {
|
|
95
85
|
dexType: p.dexType,
|
|
@@ -119,7 +109,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
119
109
|
key: "swap",
|
|
120
110
|
value: function () {
|
|
121
111
|
var _swap = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(params) {
|
|
122
|
-
var fromTokenAddress, toTokenAddress, aggregatorAddress,
|
|
112
|
+
var fromTokenAddress, toTokenAddress, aggregatorAddress, wallet, aggregator, swapParams, erc20, userBalance, currentAllowance, gas, txResponse, receipt, iface, amountOut, _iterator, _step, log, parsed;
|
|
123
113
|
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
124
114
|
while (1) switch (_context2.prev = _context2.next) {
|
|
125
115
|
case 0:
|
|
@@ -133,14 +123,14 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
133
123
|
fromTokenAddress = params.path[0].fromCoinAddress;
|
|
134
124
|
toTokenAddress = params.path[params.path.length - 1].toCoinAddress;
|
|
135
125
|
aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
136
|
-
|
|
137
|
-
if (
|
|
126
|
+
wallet = this.walletMap.get(params.chain);
|
|
127
|
+
if (wallet) {
|
|
138
128
|
_context2.next = 9;
|
|
139
129
|
break;
|
|
140
130
|
}
|
|
141
|
-
throw new Error("
|
|
131
|
+
throw new Error("Wallet not configured for chain: ".concat(params.chain));
|
|
142
132
|
case 9:
|
|
143
|
-
aggregator = new Contract(aggregatorAddress, AggregatorAbi,
|
|
133
|
+
aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
|
|
144
134
|
swapParams = params.path.map(function (pathItem) {
|
|
145
135
|
var _pathItem$extra;
|
|
146
136
|
return {
|
|
@@ -151,7 +141,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
151
141
|
extra: (_pathItem$extra = pathItem.extra) !== null && _pathItem$extra !== void 0 ? _pathItem$extra : '0x'
|
|
152
142
|
};
|
|
153
143
|
});
|
|
154
|
-
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);
|
|
155
145
|
_context2.next = 14;
|
|
156
146
|
return erc20.balanceOf(params.user);
|
|
157
147
|
case 14:
|
|
@@ -176,7 +166,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
176
166
|
_context2.prev = 23;
|
|
177
167
|
_context2.next = 26;
|
|
178
168
|
return aggregator.getFunction('swap').estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, {
|
|
179
|
-
from:
|
|
169
|
+
from: wallet.address
|
|
180
170
|
});
|
|
181
171
|
case 26:
|
|
182
172
|
gas = _context2.sent;
|
|
@@ -192,7 +182,7 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
192
182
|
_context2.prev = 34;
|
|
193
183
|
_context2.next = 37;
|
|
194
184
|
return aggregator.getFunction('swap')(params.user, params.amountInWei, swapParams, params.minAmountOutList, {
|
|
195
|
-
from:
|
|
185
|
+
from: wallet.address
|
|
196
186
|
});
|
|
197
187
|
case 37:
|
|
198
188
|
txResponse = _context2.sent;
|
package/dist/esm/types.d.ts
CHANGED