hermes-swap 0.0.22 → 0.0.24
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/abis/aggregator.d.ts +264 -235
- package/dist/cjs/abis/aggregator.js +154 -603
- package/dist/cjs/abis/quoter.d.ts +5 -24
- package/dist/cjs/abis/quoter.js +67 -42
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +136 -13
- package/dist/cjs/types.d.ts +21 -1
- package/dist/cjs/types.js +22 -3
- package/dist/esm/abis/aggregator.d.ts +264 -235
- package/dist/esm/abis/aggregator.js +264 -235
- package/dist/esm/abis/quoter.d.ts +5 -24
- package/dist/esm/abis/quoter.js +54 -35
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +313 -69
- package/dist/esm/types.d.ts +21 -1
- package/dist/esm/types.js +20 -3
- package/package.json +1 -1
package/dist/esm/abis/quoter.js
CHANGED
|
@@ -62,7 +62,7 @@ var QuoterAbi = [{
|
|
|
62
62
|
type: 'uint256[][]',
|
|
63
63
|
internalType: 'uint256[][]'
|
|
64
64
|
}],
|
|
65
|
-
stateMutability: '
|
|
65
|
+
stateMutability: 'nonpayable'
|
|
66
66
|
}, {
|
|
67
67
|
type: 'function',
|
|
68
68
|
name: 'contractList',
|
|
@@ -128,7 +128,7 @@ var QuoterAbi = [{
|
|
|
128
128
|
type: 'bool',
|
|
129
129
|
internalType: 'bool'
|
|
130
130
|
}],
|
|
131
|
-
stateMutability: '
|
|
131
|
+
stateMutability: 'nonpayable'
|
|
132
132
|
}, {
|
|
133
133
|
type: 'function',
|
|
134
134
|
name: 'isWhitelist',
|
|
@@ -181,7 +181,7 @@ var QuoterAbi = [{
|
|
|
181
181
|
type: 'uint256[]',
|
|
182
182
|
internalType: 'uint256[]'
|
|
183
183
|
}],
|
|
184
|
-
stateMutability: '
|
|
184
|
+
stateMutability: 'nonpayable'
|
|
185
185
|
}, {
|
|
186
186
|
type: 'function',
|
|
187
187
|
name: 'owner',
|
|
@@ -230,47 +230,42 @@ var QuoterAbi = [{
|
|
|
230
230
|
type: 'uint256',
|
|
231
231
|
internalType: 'uint256'
|
|
232
232
|
}],
|
|
233
|
-
stateMutability: '
|
|
233
|
+
stateMutability: 'nonpayable'
|
|
234
234
|
}, {
|
|
235
235
|
type: 'function',
|
|
236
236
|
name: 'quoteBridge',
|
|
237
237
|
inputs: [{
|
|
238
|
-
name: '
|
|
238
|
+
name: 'bridgeParam',
|
|
239
239
|
type: 'tuple',
|
|
240
|
-
internalType: 'struct
|
|
240
|
+
internalType: 'struct BridgeParam',
|
|
241
241
|
components: [{
|
|
242
242
|
name: 'bridge',
|
|
243
243
|
type: 'string',
|
|
244
244
|
internalType: 'string'
|
|
245
245
|
}, {
|
|
246
|
-
name: '
|
|
247
|
-
type: '
|
|
248
|
-
internalType: '
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
}, {
|
|
270
|
-
name: 'adapterParams',
|
|
271
|
-
type: 'bytes',
|
|
272
|
-
internalType: 'bytes'
|
|
273
|
-
}]
|
|
246
|
+
name: 'token',
|
|
247
|
+
type: 'address',
|
|
248
|
+
internalType: 'address'
|
|
249
|
+
}, {
|
|
250
|
+
name: 'amount',
|
|
251
|
+
type: 'uint256',
|
|
252
|
+
internalType: 'uint256'
|
|
253
|
+
}, {
|
|
254
|
+
name: 'bridgeAddress',
|
|
255
|
+
type: 'address',
|
|
256
|
+
internalType: 'address'
|
|
257
|
+
}, {
|
|
258
|
+
name: 'destChain',
|
|
259
|
+
type: 'string',
|
|
260
|
+
internalType: 'string'
|
|
261
|
+
}, {
|
|
262
|
+
name: 'destUser',
|
|
263
|
+
type: 'address',
|
|
264
|
+
internalType: 'address'
|
|
265
|
+
}, {
|
|
266
|
+
name: 'extra',
|
|
267
|
+
type: 'bytes',
|
|
268
|
+
internalType: 'bytes'
|
|
274
269
|
}]
|
|
275
270
|
}],
|
|
276
271
|
outputs: [{
|
|
@@ -278,7 +273,7 @@ var QuoterAbi = [{
|
|
|
278
273
|
type: 'uint256',
|
|
279
274
|
internalType: 'uint256'
|
|
280
275
|
}],
|
|
281
|
-
stateMutability: '
|
|
276
|
+
stateMutability: 'nonpayable'
|
|
282
277
|
}, {
|
|
283
278
|
type: 'function',
|
|
284
279
|
name: 'removeContract',
|
|
@@ -377,5 +372,29 @@ var QuoterAbi = [{
|
|
|
377
372
|
internalType: 'address'
|
|
378
373
|
}],
|
|
379
374
|
anonymous: false
|
|
375
|
+
}, {
|
|
376
|
+
type: 'error',
|
|
377
|
+
name: 'NotExecutor',
|
|
378
|
+
inputs: [{
|
|
379
|
+
name: 'caller',
|
|
380
|
+
type: 'address',
|
|
381
|
+
internalType: 'address'
|
|
382
|
+
}, {
|
|
383
|
+
name: 'executor',
|
|
384
|
+
type: 'address',
|
|
385
|
+
internalType: 'address'
|
|
386
|
+
}]
|
|
387
|
+
}, {
|
|
388
|
+
type: 'error',
|
|
389
|
+
name: 'NotOwner',
|
|
390
|
+
inputs: [{
|
|
391
|
+
name: 'caller',
|
|
392
|
+
type: 'address',
|
|
393
|
+
internalType: 'address'
|
|
394
|
+
}, {
|
|
395
|
+
name: 'owner',
|
|
396
|
+
type: 'address',
|
|
397
|
+
internalType: 'address'
|
|
398
|
+
}]
|
|
380
399
|
}];
|
|
381
400
|
export default QuoterAbi;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -12,7 +12,9 @@ declare class Hermes {
|
|
|
12
12
|
expect(params: IExpectParams): Promise<bigint>;
|
|
13
13
|
swap(params: ISwapParams): Promise<IReceipt>;
|
|
14
14
|
bridge(params: IBridgeParams): Promise<IReceipt>;
|
|
15
|
+
estimateBridgeFee(params: IBridgeParams): Promise<bigint>;
|
|
15
16
|
swapAndBridge(params: ISwapAndBridgeParams): Promise<IReceipt>;
|
|
17
|
+
private checkIsEnoughToken;
|
|
16
18
|
private validateParams;
|
|
17
19
|
private getQuoterAddress;
|
|
18
20
|
private getAggregatorAddress;
|
package/dist/esm/index.js
CHANGED
|
@@ -90,11 +90,19 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
90
90
|
};
|
|
91
91
|
});
|
|
92
92
|
_context.next = 10;
|
|
93
|
-
return quoter.multiQuote(params.amountInWei, quoteParams
|
|
93
|
+
return quoter.multiQuote.staticCall(params.amountInWei, quoteParams, {
|
|
94
|
+
from: wallet.address
|
|
95
|
+
});
|
|
94
96
|
case 10:
|
|
95
97
|
amountOutList = _context.sent;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
+
if (amountOutList.length) {
|
|
99
|
+
_context.next = 13;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
throw new Error('No expect result return from smart contract');
|
|
103
|
+
case 13:
|
|
104
|
+
return _context.abrupt("return", amountOutList[amountOutList.length - 1]);
|
|
105
|
+
case 14:
|
|
98
106
|
case "end":
|
|
99
107
|
return _context.stop();
|
|
100
108
|
}
|
|
@@ -156,93 +164,90 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
156
164
|
return erc20.allowance(params.user, aggregatorAddress);
|
|
157
165
|
case 19:
|
|
158
166
|
currentAllowance = _context2.sent;
|
|
159
|
-
console.log(currentAllowance);
|
|
160
167
|
if (!(currentAllowance < params.amountInWei)) {
|
|
161
|
-
_context2.next =
|
|
168
|
+
_context2.next = 22;
|
|
162
169
|
break;
|
|
163
170
|
}
|
|
164
171
|
throw new Error('Insufficient allowance token amount for swap');
|
|
165
|
-
case
|
|
166
|
-
_context2.prev =
|
|
167
|
-
_context2.next =
|
|
172
|
+
case 22:
|
|
173
|
+
_context2.prev = 22;
|
|
174
|
+
_context2.next = 25;
|
|
168
175
|
return aggregator.getFunction('swap').estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, {
|
|
169
176
|
from: wallet.address
|
|
170
177
|
});
|
|
171
|
-
case
|
|
178
|
+
case 25:
|
|
172
179
|
gas = _context2.sent;
|
|
173
|
-
|
|
174
|
-
_context2.next = 34;
|
|
180
|
+
_context2.next = 32;
|
|
175
181
|
break;
|
|
176
|
-
case
|
|
177
|
-
_context2.prev =
|
|
178
|
-
_context2.t0 = _context2["catch"](
|
|
182
|
+
case 28:
|
|
183
|
+
_context2.prev = 28;
|
|
184
|
+
_context2.t0 = _context2["catch"](22);
|
|
179
185
|
console.warn('Aggregator estimateGas.swap failed', _context2.t0);
|
|
180
186
|
throw _context2.t0;
|
|
181
|
-
case
|
|
182
|
-
_context2.prev =
|
|
183
|
-
_context2.next =
|
|
187
|
+
case 32:
|
|
188
|
+
_context2.prev = 32;
|
|
189
|
+
_context2.next = 35;
|
|
184
190
|
return aggregator.getFunction('swap')(params.user, params.amountInWei, swapParams, params.minAmountOutList, {
|
|
185
191
|
from: wallet.address
|
|
186
192
|
});
|
|
187
|
-
case
|
|
193
|
+
case 35:
|
|
188
194
|
txResponse = _context2.sent;
|
|
189
|
-
_context2.next =
|
|
195
|
+
_context2.next = 42;
|
|
190
196
|
break;
|
|
191
|
-
case
|
|
192
|
-
_context2.prev =
|
|
193
|
-
_context2.t1 = _context2["catch"](
|
|
197
|
+
case 38:
|
|
198
|
+
_context2.prev = 38;
|
|
199
|
+
_context2.t1 = _context2["catch"](32);
|
|
194
200
|
console.error('Aggregator swap transaction failed', _context2.t1);
|
|
195
201
|
throw _context2.t1;
|
|
196
|
-
case
|
|
197
|
-
_context2.next =
|
|
202
|
+
case 42:
|
|
203
|
+
_context2.next = 44;
|
|
198
204
|
return txResponse.wait();
|
|
199
|
-
case
|
|
205
|
+
case 44:
|
|
200
206
|
receipt = _context2.sent;
|
|
201
|
-
console.log("Swap transaction mined in block ".concat(receipt.blockNumber, " (tx: ").concat(receipt.hash, ")"));
|
|
202
207
|
iface = new ethers.Interface(AggregatorAbi);
|
|
203
208
|
amountOut = params.amountInWei;
|
|
204
209
|
_iterator = _createForOfIteratorHelper(receipt.logs);
|
|
205
|
-
_context2.prev =
|
|
210
|
+
_context2.prev = 48;
|
|
206
211
|
_iterator.s();
|
|
207
|
-
case
|
|
212
|
+
case 50:
|
|
208
213
|
if ((_step = _iterator.n()).done) {
|
|
209
|
-
_context2.next =
|
|
214
|
+
_context2.next = 64;
|
|
210
215
|
break;
|
|
211
216
|
}
|
|
212
217
|
log = _step.value;
|
|
213
218
|
if (!(log.address.toLowerCase() === aggregatorAddress.toLowerCase())) {
|
|
214
|
-
_context2.next =
|
|
219
|
+
_context2.next = 62;
|
|
215
220
|
break;
|
|
216
221
|
}
|
|
217
|
-
_context2.prev =
|
|
222
|
+
_context2.prev = 53;
|
|
218
223
|
parsed = iface.parseLog(log);
|
|
219
224
|
if (!(parsed && parsed.name === 'Swapped' && parsed.args && parsed.args.amountOut !== undefined)) {
|
|
220
|
-
_context2.next =
|
|
225
|
+
_context2.next = 58;
|
|
221
226
|
break;
|
|
222
227
|
}
|
|
223
228
|
amountOut = parsed.args.amountOut;
|
|
224
|
-
return _context2.abrupt("break",
|
|
225
|
-
case
|
|
226
|
-
_context2.next =
|
|
229
|
+
return _context2.abrupt("break", 64);
|
|
230
|
+
case 58:
|
|
231
|
+
_context2.next = 62;
|
|
227
232
|
break;
|
|
228
|
-
case
|
|
229
|
-
_context2.prev =
|
|
230
|
-
_context2.t2 = _context2["catch"](
|
|
231
|
-
case
|
|
232
|
-
_context2.next =
|
|
233
|
+
case 60:
|
|
234
|
+
_context2.prev = 60;
|
|
235
|
+
_context2.t2 = _context2["catch"](53);
|
|
236
|
+
case 62:
|
|
237
|
+
_context2.next = 50;
|
|
233
238
|
break;
|
|
234
|
-
case
|
|
235
|
-
_context2.next =
|
|
239
|
+
case 64:
|
|
240
|
+
_context2.next = 69;
|
|
236
241
|
break;
|
|
242
|
+
case 66:
|
|
243
|
+
_context2.prev = 66;
|
|
244
|
+
_context2.t3 = _context2["catch"](48);
|
|
245
|
+
_iterator.e(_context2.t3);
|
|
237
246
|
case 69:
|
|
238
247
|
_context2.prev = 69;
|
|
239
|
-
_context2.t3 = _context2["catch"](51);
|
|
240
|
-
_iterator.e(_context2.t3);
|
|
241
|
-
case 72:
|
|
242
|
-
_context2.prev = 72;
|
|
243
248
|
_iterator.f();
|
|
244
|
-
return _context2.finish(
|
|
245
|
-
case
|
|
249
|
+
return _context2.finish(69);
|
|
250
|
+
case 72:
|
|
246
251
|
return _context2.abrupt("return", {
|
|
247
252
|
fromToken: fromTokenAddress,
|
|
248
253
|
toToken: toTokenAddress,
|
|
@@ -252,11 +257,11 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
252
257
|
to: receipt.from,
|
|
253
258
|
logs: receipt.logs
|
|
254
259
|
});
|
|
255
|
-
case
|
|
260
|
+
case 73:
|
|
256
261
|
case "end":
|
|
257
262
|
return _context2.stop();
|
|
258
263
|
}
|
|
259
|
-
}, _callee2, this, [[
|
|
264
|
+
}, _callee2, this, [[22, 28], [32, 38], [48, 66, 69, 72], [53, 60]]);
|
|
260
265
|
}));
|
|
261
266
|
function swap(_x2) {
|
|
262
267
|
return _swap.apply(this, arguments);
|
|
@@ -267,26 +272,104 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
267
272
|
key: "bridge",
|
|
268
273
|
value: function () {
|
|
269
274
|
var _bridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(params) {
|
|
270
|
-
var
|
|
275
|
+
var _params$extra;
|
|
276
|
+
var aggregatorAddress, wallet, provider, nativeBalance, aggregator, bridgeArgs, txOverrides, txResponse, receipt;
|
|
271
277
|
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
272
278
|
while (1) switch (_context3.prev = _context3.next) {
|
|
273
279
|
case 0:
|
|
274
280
|
this.validateParams(params);
|
|
281
|
+
|
|
282
|
+
// set up
|
|
283
|
+
aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
284
|
+
wallet = this.walletMap.get(params.chain);
|
|
285
|
+
if (wallet) {
|
|
286
|
+
_context3.next = 5;
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
throw new Error("Wallet not configured for chain: ".concat(params.chain));
|
|
290
|
+
case 5:
|
|
291
|
+
if (!(params.tokenAddress && params.tokenAddress !== ethers.ZeroAddress)) {
|
|
292
|
+
_context3.next = 8;
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
_context3.next = 8;
|
|
296
|
+
return this.checkIsEnoughToken(params.tokenAddress, params.user, params.amountInWei, aggregatorAddress, wallet);
|
|
297
|
+
case 8:
|
|
298
|
+
if (!(params.bridgeFee > 0n)) {
|
|
299
|
+
_context3.next = 17;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
provider = this.providerMap.get(params.chain);
|
|
303
|
+
if (provider) {
|
|
304
|
+
_context3.next = 12;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
throw new Error("Provider not configured for chain: ".concat(params.chain));
|
|
308
|
+
case 12:
|
|
309
|
+
_context3.next = 14;
|
|
310
|
+
return provider.getBalance(params.user);
|
|
311
|
+
case 14:
|
|
312
|
+
nativeBalance = _context3.sent;
|
|
313
|
+
if (!(nativeBalance < params.bridgeFee)) {
|
|
314
|
+
_context3.next = 17;
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
throw new Error('Insufficient native balance for bridge fee');
|
|
318
|
+
case 17:
|
|
319
|
+
aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet); // simulate
|
|
320
|
+
bridgeArgs = {
|
|
321
|
+
bridge: params.bridgeType,
|
|
322
|
+
token: params.tokenAddress,
|
|
323
|
+
amount: params.amountInWei,
|
|
324
|
+
bridgeAddress: params.bridgeAddress,
|
|
325
|
+
destChain: params.destChain,
|
|
326
|
+
destUser: params.destUser,
|
|
327
|
+
extra: (_params$extra = params.extra) !== null && _params$extra !== void 0 ? _params$extra : '0x'
|
|
328
|
+
};
|
|
329
|
+
txOverrides = {
|
|
330
|
+
from: wallet.address,
|
|
331
|
+
value: params.bridgeFee
|
|
332
|
+
}; // simulate
|
|
333
|
+
_context3.prev = 20;
|
|
334
|
+
_context3.next = 23;
|
|
335
|
+
return aggregator.bridge.estimateGas(params.user, bridgeArgs, txOverrides);
|
|
336
|
+
case 23:
|
|
337
|
+
_context3.next = 29;
|
|
338
|
+
break;
|
|
339
|
+
case 25:
|
|
340
|
+
_context3.prev = 25;
|
|
341
|
+
_context3.t0 = _context3["catch"](20);
|
|
342
|
+
console.error('Bridge gas estimation reverted', _context3.t0);
|
|
343
|
+
throw _context3.t0;
|
|
344
|
+
case 29:
|
|
345
|
+
_context3.prev = 29;
|
|
346
|
+
_context3.next = 32;
|
|
347
|
+
return aggregator.bridge(params.user, bridgeArgs, txOverrides);
|
|
348
|
+
case 32:
|
|
349
|
+
txResponse = _context3.sent;
|
|
350
|
+
_context3.next = 39;
|
|
351
|
+
break;
|
|
352
|
+
case 35:
|
|
353
|
+
_context3.prev = 35;
|
|
354
|
+
_context3.t1 = _context3["catch"](29);
|
|
355
|
+
console.error('Aggregator swap transaction failed', _context3.t1);
|
|
356
|
+
throw _context3.t1;
|
|
357
|
+
case 39:
|
|
275
358
|
receipt = {
|
|
276
359
|
fromToken: params.tokenAddress,
|
|
277
360
|
toToken: params.tokenAddress,
|
|
278
361
|
amountOut: params.amountInWei,
|
|
279
|
-
hash:
|
|
280
|
-
from:
|
|
281
|
-
to:
|
|
282
|
-
logs:
|
|
362
|
+
hash: txResponse.hash,
|
|
363
|
+
from: txResponse.from,
|
|
364
|
+
to: txResponse.to,
|
|
365
|
+
logs: txResponse.logs
|
|
283
366
|
};
|
|
284
367
|
return _context3.abrupt("return", Promise.resolve(receipt));
|
|
285
|
-
case
|
|
368
|
+
case 41:
|
|
286
369
|
case "end":
|
|
287
370
|
return _context3.stop();
|
|
288
371
|
}
|
|
289
|
-
}, _callee3, this);
|
|
372
|
+
}, _callee3, this, [[20, 25], [29, 35]]);
|
|
290
373
|
}));
|
|
291
374
|
function bridge(_x3) {
|
|
292
375
|
return _bridge.apply(this, arguments);
|
|
@@ -294,35 +377,196 @@ var Hermes = /*#__PURE__*/function () {
|
|
|
294
377
|
return bridge;
|
|
295
378
|
}()
|
|
296
379
|
}, {
|
|
297
|
-
key: "
|
|
380
|
+
key: "estimateBridgeFee",
|
|
298
381
|
value: function () {
|
|
299
|
-
var
|
|
300
|
-
var
|
|
382
|
+
var _estimateBridgeFee = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(params) {
|
|
383
|
+
var _params$extra2;
|
|
384
|
+
var wallet, address, quoter, bridgeArgs, bridgeFee;
|
|
301
385
|
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
302
386
|
while (1) switch (_context4.prev = _context4.next) {
|
|
303
387
|
case 0:
|
|
304
388
|
this.validateParams(params);
|
|
389
|
+
wallet = this.walletMap.get(params.chain);
|
|
390
|
+
if (wallet) {
|
|
391
|
+
_context4.next = 4;
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
throw new Error("Wallet not configured for chain: ".concat(params.chain));
|
|
395
|
+
case 4:
|
|
396
|
+
address = this.getQuoterAddress(params.chain);
|
|
397
|
+
if (address) {
|
|
398
|
+
_context4.next = 7;
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
throw new Error("Quoter address not found for chain: ".concat(params.chain));
|
|
402
|
+
case 7:
|
|
403
|
+
quoter = new Contract(address, QuoterAbi, wallet);
|
|
404
|
+
bridgeArgs = {
|
|
405
|
+
bridge: params.bridgeType,
|
|
406
|
+
token: params.tokenAddress,
|
|
407
|
+
amount: params.amountInWei,
|
|
408
|
+
bridgeAddress: params.bridgeAddress,
|
|
409
|
+
destChain: params.destChain,
|
|
410
|
+
destUser: params.destUser,
|
|
411
|
+
extra: (_params$extra2 = params.extra) !== null && _params$extra2 !== void 0 ? _params$extra2 : '0x'
|
|
412
|
+
};
|
|
413
|
+
_context4.next = 11;
|
|
414
|
+
return quoter.quoteBridge.staticCall(bridgeArgs, {
|
|
415
|
+
from: wallet.address
|
|
416
|
+
});
|
|
417
|
+
case 11:
|
|
418
|
+
bridgeFee = _context4.sent;
|
|
419
|
+
return _context4.abrupt("return", bridgeFee);
|
|
420
|
+
case 13:
|
|
421
|
+
case "end":
|
|
422
|
+
return _context4.stop();
|
|
423
|
+
}
|
|
424
|
+
}, _callee4, this);
|
|
425
|
+
}));
|
|
426
|
+
function estimateBridgeFee(_x4) {
|
|
427
|
+
return _estimateBridgeFee.apply(this, arguments);
|
|
428
|
+
}
|
|
429
|
+
return estimateBridgeFee;
|
|
430
|
+
}()
|
|
431
|
+
}, {
|
|
432
|
+
key: "swapAndBridge",
|
|
433
|
+
value: function () {
|
|
434
|
+
var _swapAndBridge = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(params) {
|
|
435
|
+
var _params$extra3;
|
|
436
|
+
var aggregatorAddress, wallet, fromCoinAddress, swapParams, bridgeArgs, txOverrides, aggregator, txResponse, receipt;
|
|
437
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
438
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
439
|
+
case 0:
|
|
440
|
+
this.validateParams(params);
|
|
441
|
+
|
|
442
|
+
// call the aggregator swap and bridge
|
|
443
|
+
aggregatorAddress = this.getAggregatorAddress(params.chain);
|
|
444
|
+
wallet = this.walletMap.get(params.chain);
|
|
445
|
+
if (wallet) {
|
|
446
|
+
_context5.next = 5;
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
throw new Error("Wallet not configured for chain: ".concat(params.chain));
|
|
450
|
+
case 5:
|
|
451
|
+
fromCoinAddress = params.path[0].fromCoinAddress;
|
|
452
|
+
if (!(fromCoinAddress && fromCoinAddress !== ethers.ZeroAddress)) {
|
|
453
|
+
_context5.next = 9;
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
_context5.next = 9;
|
|
457
|
+
return this.checkIsEnoughToken(fromCoinAddress, params.user, params.amountInWei, aggregatorAddress, wallet);
|
|
458
|
+
case 9:
|
|
459
|
+
// 准备合约参数
|
|
460
|
+
swapParams = params.path.map(function (pathItem) {
|
|
461
|
+
var _pathItem$extra2;
|
|
462
|
+
return {
|
|
463
|
+
dexType: pathItem.dexType,
|
|
464
|
+
pool: pathItem.poolAddress,
|
|
465
|
+
fromCoin: pathItem.fromCoinAddress,
|
|
466
|
+
toCoin: pathItem.toCoinAddress,
|
|
467
|
+
extra: (_pathItem$extra2 = pathItem.extra) !== null && _pathItem$extra2 !== void 0 ? _pathItem$extra2 : '0x'
|
|
468
|
+
};
|
|
469
|
+
});
|
|
470
|
+
bridgeArgs = {
|
|
471
|
+
bridge: params.bridgeType,
|
|
472
|
+
token: params.tokenAddress,
|
|
473
|
+
amount: params.amountInWei,
|
|
474
|
+
bridgeAddress: params.bridgeAddress,
|
|
475
|
+
destChain: params.destChain,
|
|
476
|
+
destUser: params.destUser,
|
|
477
|
+
extra: (_params$extra3 = params.extra) !== null && _params$extra3 !== void 0 ? _params$extra3 : '0x'
|
|
478
|
+
};
|
|
479
|
+
txOverrides = {
|
|
480
|
+
from: wallet.address,
|
|
481
|
+
value: params.bridgeFee
|
|
482
|
+
}; // simulate
|
|
483
|
+
aggregator = new Contract(aggregatorAddress, AggregatorAbi, wallet);
|
|
484
|
+
_context5.prev = 13;
|
|
485
|
+
_context5.next = 16;
|
|
486
|
+
return aggregator.swapAndBridge.estimateGas(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txOverrides);
|
|
487
|
+
case 16:
|
|
488
|
+
_context5.next = 22;
|
|
489
|
+
break;
|
|
490
|
+
case 18:
|
|
491
|
+
_context5.prev = 18;
|
|
492
|
+
_context5.t0 = _context5["catch"](13);
|
|
493
|
+
console.error('Bridge gas estimation reverted', _context5.t0);
|
|
494
|
+
throw _context5.t0;
|
|
495
|
+
case 22:
|
|
496
|
+
_context5.prev = 22;
|
|
497
|
+
_context5.next = 25;
|
|
498
|
+
return aggregator.swapAndBridge(params.user, params.amountInWei, swapParams, params.minAmountOutList, bridgeArgs, txOverrides);
|
|
499
|
+
case 25:
|
|
500
|
+
txResponse = _context5.sent;
|
|
501
|
+
_context5.next = 32;
|
|
502
|
+
break;
|
|
503
|
+
case 28:
|
|
504
|
+
_context5.prev = 28;
|
|
505
|
+
_context5.t1 = _context5["catch"](22);
|
|
506
|
+
console.error('Aggregator swap and bridge transaction failed', _context5.t1);
|
|
507
|
+
throw _context5.t1;
|
|
508
|
+
case 32:
|
|
305
509
|
receipt = {
|
|
306
510
|
fromToken: params.path[0].fromCoinAddress,
|
|
307
511
|
toToken: params.path[params.path.length - 1].toCoinAddress,
|
|
308
512
|
amountOut: params.minAmountOutList[params.minAmountOutList.length - 1],
|
|
309
|
-
hash:
|
|
310
|
-
from:
|
|
311
|
-
to:
|
|
312
|
-
logs:
|
|
513
|
+
hash: txResponse.hash,
|
|
514
|
+
from: txResponse.from,
|
|
515
|
+
to: txResponse.to,
|
|
516
|
+
logs: txResponse.logs
|
|
313
517
|
};
|
|
314
|
-
return
|
|
315
|
-
case
|
|
518
|
+
return _context5.abrupt("return", Promise.resolve(receipt));
|
|
519
|
+
case 34:
|
|
316
520
|
case "end":
|
|
317
|
-
return
|
|
521
|
+
return _context5.stop();
|
|
318
522
|
}
|
|
319
|
-
},
|
|
523
|
+
}, _callee5, this, [[13, 18], [22, 28]]);
|
|
320
524
|
}));
|
|
321
|
-
function swapAndBridge(
|
|
525
|
+
function swapAndBridge(_x5) {
|
|
322
526
|
return _swapAndBridge.apply(this, arguments);
|
|
323
527
|
}
|
|
324
528
|
return swapAndBridge;
|
|
325
529
|
}()
|
|
530
|
+
}, {
|
|
531
|
+
key: "checkIsEnoughToken",
|
|
532
|
+
value: function () {
|
|
533
|
+
var _checkIsEnoughToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(fromTokenAddress, userAddress, amountInWei, aggregatorAddress, wallet) {
|
|
534
|
+
var erc20, userBalance, currentAllowance;
|
|
535
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
536
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
537
|
+
case 0:
|
|
538
|
+
erc20 = new Contract(fromTokenAddress, ['function balanceOf(address) view returns (uint256)', 'function allowance(address, address) view returns (uint256)'], wallet);
|
|
539
|
+
_context6.next = 3;
|
|
540
|
+
return erc20.balanceOf(userAddress);
|
|
541
|
+
case 3:
|
|
542
|
+
userBalance = _context6.sent;
|
|
543
|
+
if (!(userBalance < amountInWei)) {
|
|
544
|
+
_context6.next = 6;
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
throw new Error('Insufficient balance token amount');
|
|
548
|
+
case 6:
|
|
549
|
+
_context6.next = 8;
|
|
550
|
+
return erc20.allowance(userAddress, aggregatorAddress);
|
|
551
|
+
case 8:
|
|
552
|
+
currentAllowance = _context6.sent;
|
|
553
|
+
console.log(currentAllowance);
|
|
554
|
+
if (!(currentAllowance < amountInWei)) {
|
|
555
|
+
_context6.next = 12;
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
throw new Error('Insufficient allowance token amount');
|
|
559
|
+
case 12:
|
|
560
|
+
case "end":
|
|
561
|
+
return _context6.stop();
|
|
562
|
+
}
|
|
563
|
+
}, _callee6);
|
|
564
|
+
}));
|
|
565
|
+
function checkIsEnoughToken(_x6, _x7, _x8, _x9, _x10) {
|
|
566
|
+
return _checkIsEnoughToken.apply(this, arguments);
|
|
567
|
+
}
|
|
568
|
+
return checkIsEnoughToken;
|
|
569
|
+
}()
|
|
326
570
|
}, {
|
|
327
571
|
key: "validateParams",
|
|
328
572
|
value: function validateParams(params) {
|