@pioneer-platform/uniswap-client 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/lib/index.js +101 -36
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -412,16 +412,72 @@ var buildTx = function (_a) {
|
|
|
412
412
|
});
|
|
413
413
|
});
|
|
414
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")];
|
|
433
|
+
case 2:
|
|
434
|
+
nonce = _b.sent();
|
|
435
|
+
gas = "0x".concat(BigInt("135120").toString(16));
|
|
436
|
+
_b.label = 3;
|
|
437
|
+
case 3:
|
|
438
|
+
_b.trys.push([3, 5, , 6]);
|
|
439
|
+
return [4 /*yield*/, provider.estimateGas({
|
|
440
|
+
from: from,
|
|
441
|
+
to: EIP155_MAINNET_CHAINS['eip155:' + chainId].universalRouter, // Uniswap Router address
|
|
442
|
+
data: data,
|
|
443
|
+
value: ethers.utils.parseEther("0"), // Value for token swaps
|
|
444
|
+
})];
|
|
445
|
+
case 4:
|
|
446
|
+
estimatedGas = _b.sent();
|
|
447
|
+
console.log("estimatedGas: ", estimatedGas);
|
|
448
|
+
gas = "0x".concat(estimatedGas.toString(16));
|
|
449
|
+
return [3 /*break*/, 6];
|
|
450
|
+
case 5:
|
|
451
|
+
e_4 = _b.sent();
|
|
452
|
+
console.error("Error in estimateGas: ", e_4);
|
|
453
|
+
//@TODO get custom gas limit defaults per chain
|
|
454
|
+
gas = "0x".concat(BigInt("135120").toString(16));
|
|
455
|
+
return [3 /*break*/, 6];
|
|
456
|
+
case 6: return [4 /*yield*/, provider.getGasPrice()];
|
|
457
|
+
case 7:
|
|
458
|
+
gasPrice = _b.sent();
|
|
459
|
+
adjustedGasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100));
|
|
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) });
|
|
461
|
+
return [2 /*return*/, tx];
|
|
462
|
+
case 8:
|
|
463
|
+
e_5 = _b.sent();
|
|
464
|
+
console.error(e_5);
|
|
465
|
+
return [3 /*break*/, 9];
|
|
466
|
+
case 9: return [2 /*return*/];
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
};
|
|
415
471
|
var get_quote = function (quote) {
|
|
416
472
|
return __awaiter(this, void 0, void 0, function () {
|
|
417
|
-
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;
|
|
418
474
|
return __generator(this, function (_d) {
|
|
419
475
|
switch (_d.label) {
|
|
420
476
|
case 0:
|
|
421
477
|
tag = TAG + " | get_quote | ";
|
|
422
478
|
_d.label = 1;
|
|
423
479
|
case 1:
|
|
424
|
-
_d.trys.push([1,
|
|
480
|
+
_d.trys.push([1, 15, , 16]);
|
|
425
481
|
output = {};
|
|
426
482
|
if (!quote.sellAsset)
|
|
427
483
|
throw new Error("missing sellAsset");
|
|
@@ -435,6 +491,7 @@ var get_quote = function (quote) {
|
|
|
435
491
|
throw new Error("missing recipientAddress");
|
|
436
492
|
if (!quote.slippage)
|
|
437
493
|
throw new Error("missing slippage");
|
|
494
|
+
output.txs = [];
|
|
438
495
|
slippageTolerance = quote.slippage;
|
|
439
496
|
recipient = quote.recipientAddress;
|
|
440
497
|
inputChain = caipToNetworkId(quote.sellAsset);
|
|
@@ -446,6 +503,8 @@ var get_quote = function (quote) {
|
|
|
446
503
|
if (!providerUrl)
|
|
447
504
|
throw new Error("missing providerUrl");
|
|
448
505
|
log.info("providerUrl: ", providerUrl);
|
|
506
|
+
chainIdInt = parseInt(inputChain.replace('eip155:', ''));
|
|
507
|
+
log.info(tag, "chainIdInt: ", chainIdInt);
|
|
449
508
|
provider = new ethers.providers.JsonRpcProvider(providerUrl);
|
|
450
509
|
sellTokenContract = void 0, buyTokenContract = void 0;
|
|
451
510
|
if (!(quote.buyAsset.indexOf('erc20') > -1)) return [3 /*break*/, 3];
|
|
@@ -485,51 +544,56 @@ var get_quote = function (quote) {
|
|
|
485
544
|
}
|
|
486
545
|
if (!path)
|
|
487
546
|
throw new Error("missing path");
|
|
488
|
-
if (!sellTokenContract) return [3 /*break*/,
|
|
547
|
+
if (!sellTokenContract) return [3 /*break*/, 6];
|
|
489
548
|
permit2Address = EIP155_MAINNET_CHAINS[inputChain].permit2;
|
|
490
549
|
if (!permit2Address)
|
|
491
550
|
throw new Error("missing permit2Address for chain: " + inputChain);
|
|
492
551
|
console.log("sellTokenContract: ", sellTokenContract);
|
|
493
552
|
log.info(tag, "permit2Address: ", permit2Address);
|
|
494
553
|
log.info(tag, "quote.senderAddress: ", quote.senderAddress.toLowerCase());
|
|
495
|
-
return [4 /*yield*/, sellTokenContract.allowance(quote.senderAddress.toLowerCase(),
|
|
554
|
+
return [4 /*yield*/, sellTokenContract.allowance(quote.senderAddress.toLowerCase(), EIP155_MAINNET_CHAINS['eip155:' + chainIdInt].universalRouter)];
|
|
496
555
|
case 4:
|
|
497
556
|
currentAllowance = _d.sent();
|
|
498
557
|
console.log("currentAllowance: ", currentAllowance.toString());
|
|
499
558
|
console.log("sellAmountBigNumber: ", sellAmountBigNumber.toString());
|
|
500
|
-
if (currentAllowance.lt(sellAmountBigNumber))
|
|
501
|
-
|
|
502
|
-
throw Error("TODO write code for allowance!");
|
|
503
|
-
}
|
|
504
|
-
_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 })];
|
|
505
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:
|
|
506
573
|
_a = path.map(function (address) { return ethers.utils.getAddress(address); }), sellTokenAddress = _a[0], buyTokenAddress = _a[1];
|
|
507
574
|
console.info("".concat(tag, " sellTokenAddress: ").concat(sellTokenAddress, ", buyTokenAddress: ").concat(buyTokenAddress));
|
|
508
|
-
if (!(sellTokenAddress !== ethers.constants.AddressZero)) return [3 /*break*/,
|
|
575
|
+
if (!(sellTokenAddress !== ethers.constants.AddressZero)) return [3 /*break*/, 8];
|
|
509
576
|
return [4 /*yield*/, new ethers.Contract(sellTokenAddress, ERC20_ABI, provider).decimals()];
|
|
510
|
-
case 6:
|
|
511
|
-
_b = _d.sent();
|
|
512
|
-
return [3 /*break*/, 8];
|
|
513
577
|
case 7:
|
|
514
|
-
_b =
|
|
515
|
-
|
|
578
|
+
_b = _d.sent();
|
|
579
|
+
return [3 /*break*/, 9];
|
|
516
580
|
case 8:
|
|
581
|
+
_b = 18;
|
|
582
|
+
_d.label = 9;
|
|
583
|
+
case 9:
|
|
517
584
|
sellTokenDecimals = _b;
|
|
518
|
-
if (!(buyTokenAddress !== ethers.constants.AddressZero)) return [3 /*break*/,
|
|
585
|
+
if (!(buyTokenAddress !== ethers.constants.AddressZero)) return [3 /*break*/, 11];
|
|
519
586
|
return [4 /*yield*/, new ethers.Contract(buyTokenAddress, ERC20_ABI, provider).decimals()];
|
|
520
|
-
case 9:
|
|
521
|
-
_c = _d.sent();
|
|
522
|
-
return [3 /*break*/, 11];
|
|
523
587
|
case 10:
|
|
524
|
-
_c =
|
|
525
|
-
|
|
588
|
+
_c = _d.sent();
|
|
589
|
+
return [3 /*break*/, 12];
|
|
526
590
|
case 11:
|
|
591
|
+
_c = 18;
|
|
592
|
+
_d.label = 12;
|
|
593
|
+
case 12:
|
|
527
594
|
buyTokenDecimals = _c;
|
|
528
595
|
log.info("".concat(tag, " sellTokenDecimals: ").concat(sellTokenDecimals, ", buyTokenDecimals: ").concat(buyTokenDecimals));
|
|
529
596
|
amountInBigNumber = ethers.utils.parseUnits(quote.sellAmount.toString(), sellTokenDecimals);
|
|
530
|
-
log.info("".concat(tag, " amountInBigNumber: ").concat(amountInBigNumber.toString()));
|
|
531
|
-
chainIdInt = parseInt(inputChain.replace('eip155:', ''));
|
|
532
|
-
log.info(tag, "chainIdInt: ", chainIdInt);
|
|
533
597
|
sellToken = sellTokenAddress === ethers.constants.AddressZero ?
|
|
534
598
|
WETH[sdk_core_1.ChainId.MAINNET] : new sdk_core_1.Token(chainIdInt, sellTokenAddress, sellTokenDecimals);
|
|
535
599
|
buyToken = buyTokenAddress === ethers.constants.AddressZero ?
|
|
@@ -541,7 +605,7 @@ var get_quote = function (quote) {
|
|
|
541
605
|
amountIn: amountInBigNumber,
|
|
542
606
|
provider: provider
|
|
543
607
|
})];
|
|
544
|
-
case
|
|
608
|
+
case 13:
|
|
545
609
|
route = _d.sent();
|
|
546
610
|
console.log("route: ", route);
|
|
547
611
|
if (!route)
|
|
@@ -552,7 +616,7 @@ var get_quote = function (quote) {
|
|
|
552
616
|
log.info(tag, "amountOut: ", amountOut);
|
|
553
617
|
output.amountOut = amountOut.toFixed(18);
|
|
554
618
|
return [4 /*yield*/, buildTx({ trade: trade, from: quote.senderAddress, chainId: chainIdInt, provider: provider })];
|
|
555
|
-
case
|
|
619
|
+
case 14:
|
|
556
620
|
tx = _d.sent();
|
|
557
621
|
log.info(tag, "tx: ", tx);
|
|
558
622
|
output.meta = {
|
|
@@ -562,17 +626,18 @@ var get_quote = function (quote) {
|
|
|
562
626
|
output.complete = true;
|
|
563
627
|
output.type = 'EVM';
|
|
564
628
|
output.id = uuid();
|
|
565
|
-
output.txs
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
629
|
+
output.txs.push({
|
|
630
|
+
type: "evm",
|
|
631
|
+
description: 'swap tokens',
|
|
632
|
+
chain: inputChain,
|
|
633
|
+
txParams: tx
|
|
634
|
+
});
|
|
570
635
|
return [2 /*return*/, output];
|
|
571
|
-
case
|
|
572
|
-
|
|
573
|
-
console.error(tag, "e: ",
|
|
574
|
-
throw
|
|
575
|
-
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*/];
|
|
576
641
|
}
|
|
577
642
|
});
|
|
578
643
|
});
|