hermes-swap 0.0.12 → 0.0.13

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/esm/index.js CHANGED
@@ -14,7 +14,6 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
14
14
  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; }
15
15
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
16
16
  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); }
17
- import { ChainNameEnum, AddressConst } from "./types";
18
17
  import { ethers } from 'ethers';
19
18
  import { Contract } from 'ethers';
20
19
  import QuoterAbi from "./abis/quoter";
@@ -65,117 +64,41 @@ var Hermes = /*#__PURE__*/function () {
65
64
  key: "expect",
66
65
  value: function () {
67
66
  var _expect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(params) {
68
- var tokenPair, address, provider, quoteParams, executorWallet, quoter, amountOutList;
67
+ var executorWallet, address, quoter, quoteParams, amountOutList;
69
68
  return _regeneratorRuntime().wrap(function _callee$(_context) {
70
69
  while (1) switch (_context.prev = _context.next) {
71
70
  case 0:
72
- // 直接调用fx合约: fx合约地址: 0xF9C0c2afCF3CB0B29c32dc2B3Bc8feA039206bdf
73
- // this.validateParams(params);
74
-
75
- // const tokenPair = this.getTokenPair(params.chain, params.fromToken, params.toToken);
76
- // const provider = this.providerMap.get(params.chain);
77
- // if (!provider) {
78
- // throw new Error(`Provider not found for chain: ${params.chain}`);
79
- // }
80
-
81
- // if (!tokenPair.path.length) {
82
- // throw new Error(`Quote path not configured for chain: ${params.chain} from: ${params.fromToken} to: ${params.toToken}`);
83
- // }
84
-
85
- // const fxPath = tokenPair.path[0];
86
- // const fxAbi: ethers.InterfaceAbi = [
87
- // {
88
- // inputs: [
89
- // {
90
- // components: [
91
- // { internalType: 'uint256', name: 'amountIn', type: 'uint256' },
92
- // { internalType: 'address', name: 'pool', type: 'address' },
93
- // { internalType: 'address', name: 'fromCoin', type: 'address' },
94
- // { internalType: 'address', name: 'toCoin', type: 'address' },
95
- // { internalType: 'bytes', name: 'extra', type: 'bytes' },
96
- // ],
97
- // internalType: 'struct QuoteParam',
98
- // name: 'param',
99
- // type: 'tuple',
100
- // },
101
- // ],
102
- // name: 'quote',
103
- // outputs: [
104
- // { internalType: 'uint256', name: 'amountOut', type: 'uint256' },
105
- // ],
106
- // stateMutability: 'view',
107
- // type: 'function',
108
- // },
109
- // ];
110
-
111
- // const fxContract = new Contract('0xF9C0c2afCF3CB0B29c32dc2B3Bc8feA039206bdf', fxAbi, provider);
112
- // const amountOut = await fxContract.quote({
113
- // amountIn: params.amount,
114
- // pool: fxPath.pool,
115
- // fromCoin: fxPath.fromCoin,
116
- // toCoin: fxPath.toCoin,
117
- // extra: params.extra ?? fxPath.extra ?? '0x',
118
- // });
119
- // return amountOut as bigint;
120
-
121
- // 校验
122
- this.validateParams(params);
123
-
124
- // 找池子: weth => usdt
125
- tokenPair = this.getTokenPair(params.chain, params.fromToken, params.toToken);
126
- if (tokenPair) {
127
- _context.next = 4;
71
+ // 调用合约
72
+ executorWallet = this.executorMap.get(params.chain);
73
+ if (executorWallet) {
74
+ _context.next = 3;
128
75
  break;
129
76
  }
130
- throw new Error("Token pair not found for chain: ".concat(params.chain));
131
- case 4:
77
+ throw new Error("Executor wallet not configured for chain: ".concat(params.chain));
78
+ case 3:
132
79
  address = this.getQuoterAddress(params.chain);
133
- console.log("quoter address: ", address);
134
- provider = this.providerMap.get(params.chain);
135
- if (provider) {
136
- _context.next = 9;
80
+ if (address) {
81
+ _context.next = 6;
137
82
  break;
138
83
  }
139
- throw new Error("Provider not found for chain: ".concat(params.chain));
140
- case 9:
141
- if (tokenPair.path.length) {
142
- _context.next = 11;
143
- break;
144
- }
145
- throw new Error("Quote path not configured for chain: ".concat(params.chain, " from: ").concat(params.fromToken, " to: ").concat(params.toToken));
146
- case 11:
147
- console.log("dex type: ", params.dexType);
148
- quoteParams = tokenPair.path.map(function (pathItem) {
149
- var _params$dexType, _ref, _pathItem$extra;
84
+ throw new Error("Quoter address not found for chain: ".concat(params.chain));
85
+ case 6:
86
+ quoter = new Contract(address, QuoterAbi, executorWallet); // 转换字段名以匹配合约 ABI
87
+ quoteParams = params.path.map(function (p) {
150
88
  return {
151
- dexType: (_params$dexType = params.dexType) !== null && _params$dexType !== void 0 ? _params$dexType : pathItem.dexType,
152
- pool: pathItem.pool,
153
- fromCoin: pathItem.fromCoin,
154
- toCoin: pathItem.toCoin,
155
- extra: (_ref = (_pathItem$extra = pathItem.extra) !== null && _pathItem$extra !== void 0 ? _pathItem$extra : params.extra) !== null && _ref !== void 0 ? _ref : '0x'
89
+ dexType: p.dexType,
90
+ pool: p.poolAddress,
91
+ fromCoin: p.fromCoinAddress,
92
+ toCoin: p.toCoinAddress,
93
+ extra: p.extra || '0x'
156
94
  };
157
- }); // 调用合约
158
- executorWallet = this.executorMap.get(params.chain);
159
- if (executorWallet) {
160
- _context.next = 16;
161
- break;
162
- }
163
- throw new Error("Executor wallet not configured for chain: ".concat(params.chain));
164
- case 16:
165
- console.log("wallet address: ", executorWallet.address);
166
- quoter = new Contract(address, QuoterAbi, executorWallet);
167
- _context.t0 = console;
168
- _context.next = 21;
169
- return quoter.executor();
170
- case 21:
171
- _context.t1 = _context.sent;
172
- _context.t0.log.call(_context.t0, "executor address: ", _context.t1);
173
- _context.next = 25;
174
- return quoter.multiQuote(params.amount, quoteParams);
175
- case 25:
95
+ });
96
+ _context.next = 10;
97
+ return quoter.multiQuote(params.amountInWei, quoteParams);
98
+ case 10:
176
99
  amountOutList = _context.sent;
177
100
  return _context.abrupt("return", amountOutList.length ? amountOutList[amountOutList.length - 1] : BigInt(0));
178
- case 27:
101
+ case 12:
179
102
  case "end":
180
103
  return _context.stop();
181
104
  }
@@ -188,33 +111,70 @@ var Hermes = /*#__PURE__*/function () {
188
111
  }()
189
112
  }, {
190
113
  key: "swap",
191
- value: function swap(params) {
192
- this.validateParams(params);
193
- var receipt = {
194
- fromToken: params.fromToken,
195
- toToken: params.toToken,
196
- amount: params.amount
197
- };
198
- return Promise.resolve(receipt);
199
- }
114
+ value: function () {
115
+ var _swap = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(params) {
116
+ var receipt;
117
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
118
+ while (1) switch (_context2.prev = _context2.next) {
119
+ case 0:
120
+ this.validateParams(params);
121
+ receipt = {};
122
+ return _context2.abrupt("return", Promise.resolve(receipt));
123
+ case 3:
124
+ case "end":
125
+ return _context2.stop();
126
+ }
127
+ }, _callee2, this);
128
+ }));
129
+ function swap(_x2) {
130
+ return _swap.apply(this, arguments);
131
+ }
132
+ return swap;
133
+ }()
200
134
  }, {
201
135
  key: "bridge",
202
- value: function bridge(params) {
203
- this.validateParams(params);
204
- var receipt = {
205
- fromToken: params.fromToken,
206
- toToken: params.toToken,
207
- amount: params.amount
208
- };
209
- return Promise.resolve(receipt);
210
- }
136
+ value: function () {
137
+ var _bridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
138
+ var receipt;
139
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
140
+ while (1) switch (_context3.prev = _context3.next) {
141
+ case 0:
142
+ this.validateParams(params);
143
+ receipt = {};
144
+ return _context3.abrupt("return", Promise.resolve(receipt));
145
+ case 3:
146
+ case "end":
147
+ return _context3.stop();
148
+ }
149
+ }, _callee3, this);
150
+ }));
151
+ function bridge(_x3) {
152
+ return _bridge.apply(this, arguments);
153
+ }
154
+ return bridge;
155
+ }()
211
156
  }, {
212
157
  key: "swapAndBridge",
213
- value: function swapAndBridge(params) {
214
- this.validateParams(params);
215
- var receipt = {};
216
- return Promise.resolve(receipt);
217
- }
158
+ value: function () {
159
+ var _swapAndBridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
160
+ var receipt;
161
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
162
+ while (1) switch (_context4.prev = _context4.next) {
163
+ case 0:
164
+ this.validateParams(params);
165
+ receipt = {};
166
+ return _context4.abrupt("return", Promise.resolve(receipt));
167
+ case 3:
168
+ case "end":
169
+ return _context4.stop();
170
+ }
171
+ }, _callee4, this);
172
+ }));
173
+ function swapAndBridge(_x4) {
174
+ return _swapAndBridge.apply(this, arguments);
175
+ }
176
+ return swapAndBridge;
177
+ }()
218
178
  }, {
219
179
  key: "validateParams",
220
180
  value: function validateParams(params) {
@@ -226,43 +186,6 @@ var Hermes = /*#__PURE__*/function () {
226
186
  throw new Error('Provider not found');
227
187
  }
228
188
  }
229
- }, {
230
- key: "getAllTokenPair",
231
- value: function getAllTokenPair() {
232
- return _defineProperty({}, ChainNameEnum.ETH, [{
233
- from: AddressConst.weth.eth,
234
- to: AddressConst.usdt.eth,
235
- path: []
236
- },
237
- // fx
238
- {
239
- from: AddressConst.weth.eth,
240
- to: AddressConst.feth.eth,
241
- path: [{
242
- dexType: 'f(x)',
243
- pool: '0xe7b9c7c9cA85340b8c06fb805f7775e3015108dB',
244
- fromCoin: AddressConst.weth.eth,
245
- toCoin: AddressConst.feth.eth,
246
- extra: '0x'
247
- }]
248
- }]);
249
- }
250
- }, {
251
- key: "getTokenPair",
252
- value: function getTokenPair(chain, from, to) {
253
- var tokenPairMap = this.getAllTokenPair();
254
- var tokenPairList = tokenPairMap[chain];
255
- if (!tokenPairList) {
256
- throw new Error("Token pair not found for chain: ".concat(chain));
257
- }
258
- var tokenPair = tokenPairList.find(function (item) {
259
- return item.from.toLocaleLowerCase() === from.toLocaleLowerCase() && item.to.toLocaleLowerCase() === to.toLocaleLowerCase();
260
- });
261
- if (!tokenPair) {
262
- throw new Error("Token pair not found for chain: ".concat(chain, " from: ").concat(from, " to: ").concat(to));
263
- }
264
- return tokenPair;
265
- }
266
189
  }, {
267
190
  key: "getQuoterAddress",
268
191
  value: function getQuoterAddress(chain) {
@@ -7,31 +7,37 @@ export interface IConfig {
7
7
  }
8
8
  export interface IExpectParams {
9
9
  chain: ChainNameEnum;
10
- dexType?: string;
11
- fromToken: string;
12
- toToken: string;
13
- amount: bigint;
14
- extra?: BytesLike;
10
+ amountInWei: bigint;
11
+ path: IPath[];
15
12
  }
16
13
  export interface ISwapParams {
17
- chain: ChainNameEnum;
18
14
  user: string;
19
- fromToken: string;
20
- toToken: string;
21
- amount: bigint;
15
+ chain: ChainNameEnum;
16
+ amountInWei: bigint;
17
+ path: IPath[];
18
+ minAmountOutList: bigint[];
22
19
  }
23
20
  export interface IBridgeParams {
24
21
  chain: ChainNameEnum;
25
22
  user: string;
26
- fromToken: string;
27
- toToken: string;
28
- amount: bigint;
23
+ amountInWei: bigint;
24
+ bridgeType: string;
25
+ bridgeAddress: string;
26
+ tokenAddress: string;
27
+ destChain: string;
28
+ extra?: BytesLike;
29
29
  }
30
30
  export interface ISwapAndBridgeParams {
31
31
  chain: ChainNameEnum;
32
32
  user: string;
33
- swapParams: ISwapParams;
34
- bridgeParams: IBridgeParams;
33
+ amountInWei: bigint;
34
+ path: IPath[];
35
+ minAmountOutList: bigint[];
36
+ bridgeType: string;
37
+ bridgeAddress: string;
38
+ tokenAddress: string;
39
+ destChain: string;
40
+ extra?: BytesLike;
35
41
  }
36
42
  export interface IReceipt {
37
43
  fromToken?: string;
@@ -216,3 +222,10 @@ export declare const AddressConst: {
216
222
  };
217
223
  export interface IExpectPayload {
218
224
  }
225
+ export interface IPath {
226
+ dexType: string;
227
+ poolAddress: string;
228
+ fromCoinAddress: string;
229
+ toCoinAddress: string;
230
+ extra?: BytesLike;
231
+ }
package/dist/esm/types.js CHANGED
@@ -160,16 +160,16 @@ export var ChainNameEnum = /*#__PURE__*/function (ChainNameEnum) {
160
160
  }({}); // BEVM
161
161
  export var AddressConst = {
162
162
  weth: {
163
- eth: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
163
+ eth: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
164
164
  },
165
165
  usdt: {
166
- eth: ""
166
+ eth: ''
167
167
  },
168
168
  // fx
169
169
  feth: {
170
- eth: "0x53805A76E1f5ebbFE7115F16f9c87C2f7e633726"
170
+ eth: '0x53805A76E1f5ebbFE7115F16f9c87C2f7e633726'
171
171
  },
172
172
  xeth: {
173
- eth: "0xe063F04f280c60aECa68b38341C2eEcBeC703ae2"
173
+ eth: '0xe063F04f280c60aECa68b38341C2eEcBeC703ae2'
174
174
  }
175
175
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-swap",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "A TypeScript utility library for swap and bridge",
5
5
  "main": "dist/cjs/index.js",
6
6
  "types": "dist/cjs/index.d.ts",
@@ -42,4 +42,4 @@
42
42
  "dotenv": "^17.2.3",
43
43
  "ethers": "^6.15.0"
44
44
  }
45
- }
45
+ }