@pioneer-platform/uniswap-client 0.0.14 → 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/lib/index.js +133 -43
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -320,17 +320,38 @@ function getDefaultTransactionTtl(chainId) {
|
|
|
320
320
|
return constants_1.DEFAULT_DEADLINE_FROM_NOW / 60;
|
|
321
321
|
}
|
|
322
322
|
exports.getDefaultTransactionTtl = getDefaultTransactionTtl;
|
|
323
|
+
var getDeadline = function (provider) { return __awaiter(void 0, void 0, void 0, function () {
|
|
324
|
+
var block, deadline, error_1;
|
|
325
|
+
return __generator(this, function (_a) {
|
|
326
|
+
switch (_a.label) {
|
|
327
|
+
case 0:
|
|
328
|
+
_a.trys.push([0, 2, , 3]);
|
|
329
|
+
return [4 /*yield*/, provider.getBlock('latest')];
|
|
330
|
+
case 1:
|
|
331
|
+
block = _a.sent();
|
|
332
|
+
deadline = block.timestamp + 60 * 60;
|
|
333
|
+
console.log("Current Block Timestamp: ".concat(block.timestamp));
|
|
334
|
+
console.log("Deadline (60 minutes later): ".concat(deadline));
|
|
335
|
+
return [2 /*return*/, deadline];
|
|
336
|
+
case 2:
|
|
337
|
+
error_1 = _a.sent();
|
|
338
|
+
console.error('Error fetching block or calculating deadline:', error_1);
|
|
339
|
+
return [2 /*return*/, null];
|
|
340
|
+
case 3: return [2 /*return*/];
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}); };
|
|
323
344
|
var buildTx = function (_a) {
|
|
324
345
|
var trade = _a.trade, from = _a.from, chainId = _a.chainId, provider = _a.provider;
|
|
325
346
|
return __awaiter(this, void 0, void 0, function () {
|
|
326
|
-
var tag, BIPS_BASE, slippageTolerance, responseRouter, data, value, nonce, gas, estimatedGas, e_2, gasPrice, adjustedGasPrice, tx, e_3;
|
|
347
|
+
var tag, BIPS_BASE, slippageTolerance, deadline, responseRouter, data, value, nonce, gas, estimatedGas, e_2, gasPrice, adjustedGasPrice, tx, e_3;
|
|
327
348
|
return __generator(this, function (_b) {
|
|
328
349
|
switch (_b.label) {
|
|
329
350
|
case 0:
|
|
330
351
|
tag = TAG + " | buildTx | ";
|
|
331
352
|
_b.label = 1;
|
|
332
353
|
case 1:
|
|
333
|
-
_b.trys.push([1,
|
|
354
|
+
_b.trys.push([1, 9, , 10]);
|
|
334
355
|
BIPS_BASE = BigNumber.from(10000);
|
|
335
356
|
slippageTolerance = new sdk_core_1.Percent(BigNumber.from(200), BIPS_BASE);
|
|
336
357
|
log.info(tag, "slippageTolerance: ", slippageTolerance);
|
|
@@ -338,10 +359,14 @@ var buildTx = function (_a) {
|
|
|
338
359
|
log.info(tag, "trade: ", trade.tradeType);
|
|
339
360
|
log.info(tag, "swaps: ", trade.swaps[0]);
|
|
340
361
|
log.info(tag, "routes: ", trade.routes[0]);
|
|
362
|
+
return [4 /*yield*/, getDeadline(provider)];
|
|
363
|
+
case 2:
|
|
364
|
+
deadline = _b.sent();
|
|
365
|
+
log.info(tag, "deadline: ", deadline);
|
|
341
366
|
responseRouter = universal_router_sdk_1.SwapRouter.swapERC20CallParameters(trade, {
|
|
342
367
|
recipient: from,
|
|
343
368
|
slippageTolerance: slippageTolerance,
|
|
344
|
-
|
|
369
|
+
deadlineOrPreviousBlockhash: deadline,
|
|
345
370
|
// inputTokenPermit: options.permit,
|
|
346
371
|
// fee: options.feeOptions,
|
|
347
372
|
});
|
|
@@ -349,6 +374,62 @@ var buildTx = function (_a) {
|
|
|
349
374
|
data = responseRouter.calldata;
|
|
350
375
|
value = responseRouter.value;
|
|
351
376
|
return [4 /*yield*/, provider.getTransactionCount(from, "latest")];
|
|
377
|
+
case 3:
|
|
378
|
+
nonce = _b.sent();
|
|
379
|
+
gas = "0x".concat(BigInt("135120").toString(16));
|
|
380
|
+
_b.label = 4;
|
|
381
|
+
case 4:
|
|
382
|
+
_b.trys.push([4, 6, , 7]);
|
|
383
|
+
return [4 /*yield*/, provider.estimateGas({
|
|
384
|
+
from: from,
|
|
385
|
+
to: EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter, // Uniswap Router address
|
|
386
|
+
data: data,
|
|
387
|
+
value: ethers.utils.parseEther("0"), // Value for token swaps
|
|
388
|
+
})];
|
|
389
|
+
case 5:
|
|
390
|
+
estimatedGas = _b.sent();
|
|
391
|
+
console.log("estimatedGas: ", estimatedGas);
|
|
392
|
+
gas = "0x".concat(estimatedGas.toString(16));
|
|
393
|
+
return [3 /*break*/, 7];
|
|
394
|
+
case 6:
|
|
395
|
+
e_2 = _b.sent();
|
|
396
|
+
console.error("Error in estimateGas: ", e_2);
|
|
397
|
+
//@TODO get custom gas limit defaults per chain
|
|
398
|
+
gas = "0x".concat(BigInt("135120").toString(16));
|
|
399
|
+
return [3 /*break*/, 7];
|
|
400
|
+
case 7: return [4 /*yield*/, provider.getGasPrice()];
|
|
401
|
+
case 8:
|
|
402
|
+
gasPrice = _b.sent();
|
|
403
|
+
adjustedGasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100));
|
|
404
|
+
tx = __assign(__assign({ from: from, to: EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter, chainId: chainId, data: data }, (value && !isZero(value) ? { value: (0, v3_sdk_1.toHex)(value) } : {})), { gas: gas, gasPrice: (0, v3_sdk_1.toHex)(adjustedGasPrice), nonce: (0, v3_sdk_1.toHex)(nonce) });
|
|
405
|
+
return [2 /*return*/, tx];
|
|
406
|
+
case 9:
|
|
407
|
+
e_3 = _b.sent();
|
|
408
|
+
console.error(e_3);
|
|
409
|
+
return [3 /*break*/, 10];
|
|
410
|
+
case 10: return [2 /*return*/];
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
};
|
|
415
|
+
var buildApprovalTx = function (_a) {
|
|
416
|
+
var from = _a.from, contract = _a.contract, address = _a.address, chainId = _a.chainId, provider = _a.provider;
|
|
417
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
418
|
+
var tag, data, value, nonce, gas, estimatedGas, e_4, gasPrice, adjustedGasPrice, tx, e_5;
|
|
419
|
+
return __generator(this, function (_b) {
|
|
420
|
+
switch (_b.label) {
|
|
421
|
+
case 0:
|
|
422
|
+
tag = TAG + " | buildTx | ";
|
|
423
|
+
_b.label = 1;
|
|
424
|
+
case 1:
|
|
425
|
+
_b.trys.push([1, 8, , 9]);
|
|
426
|
+
//
|
|
427
|
+
// let result = await contract.approve(contract.address, address, ethers.constants.MaxUint256)
|
|
428
|
+
console.log(tag, "from: ", from);
|
|
429
|
+
data = contract.interface.encodeFunctionData("approve", [address, ethers.constants.MaxUint256]);
|
|
430
|
+
console.log(tag, "data: ", data);
|
|
431
|
+
value = '0x';
|
|
432
|
+
return [4 /*yield*/, provider.getTransactionCount(from, "latest")];
|
|
352
433
|
case 2:
|
|
353
434
|
nonce = _b.sent();
|
|
354
435
|
gas = "0x".concat(BigInt("135120").toString(16));
|
|
@@ -367,8 +448,8 @@ var buildTx = function (_a) {
|
|
|
367
448
|
gas = "0x".concat(estimatedGas.toString(16));
|
|
368
449
|
return [3 /*break*/, 6];
|
|
369
450
|
case 5:
|
|
370
|
-
|
|
371
|
-
console.error("Error in estimateGas: ",
|
|
451
|
+
e_4 = _b.sent();
|
|
452
|
+
console.error("Error in estimateGas: ", e_4);
|
|
372
453
|
//@TODO get custom gas limit defaults per chain
|
|
373
454
|
gas = "0x".concat(BigInt("135120").toString(16));
|
|
374
455
|
return [3 /*break*/, 6];
|
|
@@ -379,8 +460,8 @@ var buildTx = function (_a) {
|
|
|
379
460
|
tx = __assign(__assign({ from: from, to: EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter, chainId: chainId, data: data }, (value && !isZero(value) ? { value: (0, v3_sdk_1.toHex)(value) } : {})), { gas: gas, gasPrice: (0, v3_sdk_1.toHex)(adjustedGasPrice), nonce: (0, v3_sdk_1.toHex)(nonce) });
|
|
380
461
|
return [2 /*return*/, tx];
|
|
381
462
|
case 8:
|
|
382
|
-
|
|
383
|
-
console.error(
|
|
463
|
+
e_5 = _b.sent();
|
|
464
|
+
console.error(e_5);
|
|
384
465
|
return [3 /*break*/, 9];
|
|
385
466
|
case 9: return [2 /*return*/];
|
|
386
467
|
}
|
|
@@ -389,14 +470,14 @@ var buildTx = function (_a) {
|
|
|
389
470
|
};
|
|
390
471
|
var get_quote = function (quote) {
|
|
391
472
|
return __awaiter(this, void 0, void 0, function () {
|
|
392
|
-
var tag, output, slippageTolerance, recipient, inputChain, outputChain, providerUrl, provider, sellTokenContract, buyTokenContract, buyTokenAddress_1, balance, sellTokenAddress_1, permit2, router, planner, sellAmountBigNumber, path, permit2Address, currentAllowance, _a, sellTokenAddress, buyTokenAddress, sellTokenDecimals, _b, buyTokenDecimals, _c, amountInBigNumber,
|
|
473
|
+
var tag, output, slippageTolerance, recipient, inputChain, outputChain, providerUrl, chainIdInt, provider, sellTokenContract, buyTokenContract, buyTokenAddress_1, balance, sellTokenAddress_1, permit2, router, planner, sellAmountBigNumber, path, permit2Address, currentAllowance, allowanceTx, formattedAllowanceTx, _a, sellTokenAddress, buyTokenAddress, sellTokenDecimals, _b, buyTokenDecimals, _c, amountInBigNumber, sellToken, buyToken, route, trade, amountOut, tx, e_6;
|
|
393
474
|
return __generator(this, function (_d) {
|
|
394
475
|
switch (_d.label) {
|
|
395
476
|
case 0:
|
|
396
477
|
tag = TAG + " | get_quote | ";
|
|
397
478
|
_d.label = 1;
|
|
398
479
|
case 1:
|
|
399
|
-
_d.trys.push([1,
|
|
480
|
+
_d.trys.push([1, 15, , 16]);
|
|
400
481
|
output = {};
|
|
401
482
|
if (!quote.sellAsset)
|
|
402
483
|
throw new Error("missing sellAsset");
|
|
@@ -410,6 +491,7 @@ var get_quote = function (quote) {
|
|
|
410
491
|
throw new Error("missing recipientAddress");
|
|
411
492
|
if (!quote.slippage)
|
|
412
493
|
throw new Error("missing slippage");
|
|
494
|
+
output.txs = [];
|
|
413
495
|
slippageTolerance = quote.slippage;
|
|
414
496
|
recipient = quote.recipientAddress;
|
|
415
497
|
inputChain = caipToNetworkId(quote.sellAsset);
|
|
@@ -421,6 +503,8 @@ var get_quote = function (quote) {
|
|
|
421
503
|
if (!providerUrl)
|
|
422
504
|
throw new Error("missing providerUrl");
|
|
423
505
|
log.info("providerUrl: ", providerUrl);
|
|
506
|
+
chainIdInt = parseInt(inputChain.replace('eip155:', ''));
|
|
507
|
+
log.info(tag, "chainIdInt: ", chainIdInt);
|
|
424
508
|
provider = new ethers.providers.JsonRpcProvider(providerUrl);
|
|
425
509
|
sellTokenContract = void 0, buyTokenContract = void 0;
|
|
426
510
|
if (!(quote.buyAsset.indexOf('erc20') > -1)) return [3 /*break*/, 3];
|
|
@@ -460,51 +544,56 @@ var get_quote = function (quote) {
|
|
|
460
544
|
}
|
|
461
545
|
if (!path)
|
|
462
546
|
throw new Error("missing path");
|
|
463
|
-
if (!sellTokenContract) return [3 /*break*/,
|
|
547
|
+
if (!sellTokenContract) return [3 /*break*/, 6];
|
|
464
548
|
permit2Address = EIP155_MAINNET_CHAINS[inputChain].permit2;
|
|
465
549
|
if (!permit2Address)
|
|
466
550
|
throw new Error("missing permit2Address for chain: " + inputChain);
|
|
467
551
|
console.log("sellTokenContract: ", sellTokenContract);
|
|
468
552
|
log.info(tag, "permit2Address: ", permit2Address);
|
|
469
553
|
log.info(tag, "quote.senderAddress: ", quote.senderAddress.toLowerCase());
|
|
470
|
-
return [4 /*yield*/, sellTokenContract.allowance(quote.senderAddress.toLowerCase(),
|
|
554
|
+
return [4 /*yield*/, sellTokenContract.allowance(quote.senderAddress.toLowerCase(), EIP155_MAINNET_CHAINS['eip155:' + chainIdInt].universalRouter)];
|
|
471
555
|
case 4:
|
|
472
556
|
currentAllowance = _d.sent();
|
|
473
557
|
console.log("currentAllowance: ", currentAllowance.toString());
|
|
474
558
|
console.log("sellAmountBigNumber: ", sellAmountBigNumber.toString());
|
|
475
|
-
if (currentAllowance.lt(sellAmountBigNumber))
|
|
476
|
-
|
|
477
|
-
throw Error("TODO write code for allowance!");
|
|
478
|
-
}
|
|
479
|
-
_d.label = 5;
|
|
559
|
+
if (!currentAllowance.lt(sellAmountBigNumber)) return [3 /*break*/, 6];
|
|
560
|
+
return [4 /*yield*/, buildApprovalTx({ provider: provider, contract: sellTokenContract, address: EIP155_MAINNET_CHAINS['eip155:' + chainIdInt].universalRouter, from: quote.senderAddress, chainId: chainIdInt })];
|
|
480
561
|
case 5:
|
|
562
|
+
allowanceTx = _d.sent();
|
|
563
|
+
console.log("allowanceTx: ", allowanceTx);
|
|
564
|
+
formattedAllowanceTx = {
|
|
565
|
+
type: "evm",
|
|
566
|
+
description: 'approve router to spend tokens',
|
|
567
|
+
chain: inputChain,
|
|
568
|
+
txParams: allowanceTx
|
|
569
|
+
};
|
|
570
|
+
output.txs.push(formattedAllowanceTx);
|
|
571
|
+
_d.label = 6;
|
|
572
|
+
case 6:
|
|
481
573
|
_a = path.map(function (address) { return ethers.utils.getAddress(address); }), sellTokenAddress = _a[0], buyTokenAddress = _a[1];
|
|
482
574
|
console.info("".concat(tag, " sellTokenAddress: ").concat(sellTokenAddress, ", buyTokenAddress: ").concat(buyTokenAddress));
|
|
483
|
-
if (!(sellTokenAddress !== ethers.constants.AddressZero)) return [3 /*break*/,
|
|
575
|
+
if (!(sellTokenAddress !== ethers.constants.AddressZero)) return [3 /*break*/, 8];
|
|
484
576
|
return [4 /*yield*/, new ethers.Contract(sellTokenAddress, ERC20_ABI, provider).decimals()];
|
|
485
|
-
case 6:
|
|
486
|
-
_b = _d.sent();
|
|
487
|
-
return [3 /*break*/, 8];
|
|
488
577
|
case 7:
|
|
489
|
-
_b =
|
|
490
|
-
|
|
578
|
+
_b = _d.sent();
|
|
579
|
+
return [3 /*break*/, 9];
|
|
491
580
|
case 8:
|
|
581
|
+
_b = 18;
|
|
582
|
+
_d.label = 9;
|
|
583
|
+
case 9:
|
|
492
584
|
sellTokenDecimals = _b;
|
|
493
|
-
if (!(buyTokenAddress !== ethers.constants.AddressZero)) return [3 /*break*/,
|
|
585
|
+
if (!(buyTokenAddress !== ethers.constants.AddressZero)) return [3 /*break*/, 11];
|
|
494
586
|
return [4 /*yield*/, new ethers.Contract(buyTokenAddress, ERC20_ABI, provider).decimals()];
|
|
495
|
-
case 9:
|
|
496
|
-
_c = _d.sent();
|
|
497
|
-
return [3 /*break*/, 11];
|
|
498
587
|
case 10:
|
|
499
|
-
_c =
|
|
500
|
-
|
|
588
|
+
_c = _d.sent();
|
|
589
|
+
return [3 /*break*/, 12];
|
|
501
590
|
case 11:
|
|
591
|
+
_c = 18;
|
|
592
|
+
_d.label = 12;
|
|
593
|
+
case 12:
|
|
502
594
|
buyTokenDecimals = _c;
|
|
503
595
|
log.info("".concat(tag, " sellTokenDecimals: ").concat(sellTokenDecimals, ", buyTokenDecimals: ").concat(buyTokenDecimals));
|
|
504
596
|
amountInBigNumber = ethers.utils.parseUnits(quote.sellAmount.toString(), sellTokenDecimals);
|
|
505
|
-
log.info("".concat(tag, " amountInBigNumber: ").concat(amountInBigNumber.toString()));
|
|
506
|
-
chainIdInt = parseInt(inputChain.replace('eip155:', ''));
|
|
507
|
-
log.info(tag, "chainIdInt: ", chainIdInt);
|
|
508
597
|
sellToken = sellTokenAddress === ethers.constants.AddressZero ?
|
|
509
598
|
WETH[sdk_core_1.ChainId.MAINNET] : new sdk_core_1.Token(chainIdInt, sellTokenAddress, sellTokenDecimals);
|
|
510
599
|
buyToken = buyTokenAddress === ethers.constants.AddressZero ?
|
|
@@ -516,7 +605,7 @@ var get_quote = function (quote) {
|
|
|
516
605
|
amountIn: amountInBigNumber,
|
|
517
606
|
provider: provider
|
|
518
607
|
})];
|
|
519
|
-
case
|
|
608
|
+
case 13:
|
|
520
609
|
route = _d.sent();
|
|
521
610
|
console.log("route: ", route);
|
|
522
611
|
if (!route)
|
|
@@ -527,7 +616,7 @@ var get_quote = function (quote) {
|
|
|
527
616
|
log.info(tag, "amountOut: ", amountOut);
|
|
528
617
|
output.amountOut = amountOut.toFixed(18);
|
|
529
618
|
return [4 /*yield*/, buildTx({ trade: trade, from: quote.senderAddress, chainId: chainIdInt, provider: provider })];
|
|
530
|
-
case
|
|
619
|
+
case 14:
|
|
531
620
|
tx = _d.sent();
|
|
532
621
|
log.info(tag, "tx: ", tx);
|
|
533
622
|
output.meta = {
|
|
@@ -537,17 +626,18 @@ var get_quote = function (quote) {
|
|
|
537
626
|
output.complete = true;
|
|
538
627
|
output.type = 'EVM';
|
|
539
628
|
output.id = uuid();
|
|
540
|
-
output.txs
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
629
|
+
output.txs.push({
|
|
630
|
+
type: "evm",
|
|
631
|
+
description: 'swap tokens',
|
|
632
|
+
chain: inputChain,
|
|
633
|
+
txParams: tx
|
|
634
|
+
});
|
|
545
635
|
return [2 /*return*/, output];
|
|
546
|
-
case
|
|
547
|
-
|
|
548
|
-
console.error(tag, "e: ",
|
|
549
|
-
throw
|
|
550
|
-
case
|
|
636
|
+
case 15:
|
|
637
|
+
e_6 = _d.sent();
|
|
638
|
+
console.error(tag, "e: ", e_6);
|
|
639
|
+
throw e_6;
|
|
640
|
+
case 16: return [2 /*return*/];
|
|
551
641
|
}
|
|
552
642
|
});
|
|
553
643
|
});
|