@pioneer-platform/uniswap-client 0.0.17 → 0.0.19
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 +86 -19
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -62,6 +62,7 @@ var TAG = " | Uniswap | ";
|
|
|
62
62
|
var uuid = require('uuidv4').uuid;
|
|
63
63
|
var log = require('@pioneer-platform/loggerdog')();
|
|
64
64
|
var _a = require("@pioneer-platform/pioneer-caip"), caipToNetworkId = _a.caipToNetworkId, shortListSymbolToCaip = _a.shortListSymbolToCaip, ChainToNetworkId = _a.ChainToNetworkId;
|
|
65
|
+
var permit2_sdk_1 = require("@uniswap/permit2-sdk");
|
|
65
66
|
var _b = require('@pioneer-platform/pioneer-coins'), createMemo = _b.createMemo, parseMemo = _b.parseMemo;
|
|
66
67
|
var _c = require('@uniswap/v2-sdk'), Pair = _c.Pair, WETH = _c.WETH, Route = _c.Route, Trade = _c.Trade, TokenAmount = _c.TokenAmount; // Import necessary components from v2-sdk
|
|
67
68
|
var constants_1 = require("./constants");
|
|
@@ -92,6 +93,13 @@ module.exports = {
|
|
|
92
93
|
getQuote: function (quote) {
|
|
93
94
|
return get_quote(quote);
|
|
94
95
|
},
|
|
96
|
+
// //buildSwapTx
|
|
97
|
+
// buildApprovalTx: function (approval:any) {
|
|
98
|
+
// return build_approval_tx(approval);
|
|
99
|
+
// },
|
|
100
|
+
// buildApprovalTx: function (approval:any) {
|
|
101
|
+
// return build_approval_tx(approval);
|
|
102
|
+
// }
|
|
95
103
|
};
|
|
96
104
|
// Router Helpers
|
|
97
105
|
exports.MAX_UINT = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
|
|
@@ -168,7 +176,7 @@ var EIP155_MAINNET_CHAINS = {
|
|
|
168
176
|
name: 'Ethereum',
|
|
169
177
|
logo: '/chain-logos/eip155-1.png',
|
|
170
178
|
rgb: '99, 125, 234',
|
|
171
|
-
permit2:
|
|
179
|
+
permit2: permit2_sdk_1.PERMIT2_ADDRESS,
|
|
172
180
|
universalRouter: universal_router_sdk_1.UNIVERSAL_ROUTER_ADDRESS,
|
|
173
181
|
rpc: 'https://eth.llamarpc.com',
|
|
174
182
|
defaultGasLimit: 250000,
|
|
@@ -341,7 +349,7 @@ var getDeadline = function (provider) { return __awaiter(void 0, void 0, void 0,
|
|
|
341
349
|
}
|
|
342
350
|
});
|
|
343
351
|
}); };
|
|
344
|
-
var
|
|
352
|
+
var buildSwapTx = function (_a) {
|
|
345
353
|
var trade = _a.trade, from = _a.from, chainId = _a.chainId, provider = _a.provider;
|
|
346
354
|
return __awaiter(this, void 0, void 0, function () {
|
|
347
355
|
var tag, BIPS_BASE, slippageTolerance, deadline, responseRouter, data, value, nonce, gas, estimatedGas, e_2, gasPrice, adjustedGasPrice, tx, e_3;
|
|
@@ -395,7 +403,7 @@ var buildTx = function (_a) {
|
|
|
395
403
|
e_2 = _b.sent();
|
|
396
404
|
console.error("Error in estimateGas: ", e_2);
|
|
397
405
|
//@TODO get custom gas limit defaults per chain
|
|
398
|
-
gas = "0x".concat(BigInt("
|
|
406
|
+
gas = "0x".concat(BigInt("335120").toString(16));
|
|
399
407
|
return [3 /*break*/, 7];
|
|
400
408
|
case 7: return [4 /*yield*/, provider.getGasPrice()];
|
|
401
409
|
case 8:
|
|
@@ -451,13 +459,13 @@ var buildApprovalTx = function (_a) {
|
|
|
451
459
|
e_4 = _b.sent();
|
|
452
460
|
console.error("Error in estimateGas: ", e_4);
|
|
453
461
|
//@TODO get custom gas limit defaults per chain
|
|
454
|
-
gas = "0x".concat(BigInt("
|
|
462
|
+
gas = "0x".concat(BigInt("335120").toString(16));
|
|
455
463
|
return [3 /*break*/, 6];
|
|
456
464
|
case 6: return [4 /*yield*/, provider.getGasPrice()];
|
|
457
465
|
case 7:
|
|
458
466
|
gasPrice = _b.sent();
|
|
459
467
|
adjustedGasPrice = gasPrice.mul(ethers.BigNumber.from(110)).div(ethers.BigNumber.from(100));
|
|
460
|
-
tx = __assign(__assign({ from: from, to:
|
|
468
|
+
tx = __assign(__assign({ from: from, to: contract.address, 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
469
|
return [2 /*return*/, tx];
|
|
462
470
|
case 8:
|
|
463
471
|
e_5 = _b.sent();
|
|
@@ -468,9 +476,55 @@ var buildApprovalTx = function (_a) {
|
|
|
468
476
|
});
|
|
469
477
|
});
|
|
470
478
|
};
|
|
479
|
+
function toDeadline(expiration) {
|
|
480
|
+
return Math.floor((Date.now() + expiration) / 1000);
|
|
481
|
+
}
|
|
482
|
+
var build_permit_tx = function (_a) {
|
|
483
|
+
var from = _a.from, token = _a.token, amount = _a.amount, chainId = _a.chainId;
|
|
484
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
485
|
+
var PERMIT_EXPIRATION, PERMIT_SIG_EXPIRATION, providerUrl, provider, nonce, permit, _b, domain, types, values, tx, e_6;
|
|
486
|
+
return __generator(this, function (_c) {
|
|
487
|
+
switch (_c.label) {
|
|
488
|
+
case 0:
|
|
489
|
+
_c.trys.push([0, 2, , 3]);
|
|
490
|
+
PERMIT_EXPIRATION = 2592000000;
|
|
491
|
+
PERMIT_SIG_EXPIRATION = 1800000;
|
|
492
|
+
providerUrl = EIP155_MAINNET_CHAINS['eip155:' + chainId].rpc;
|
|
493
|
+
if (!providerUrl)
|
|
494
|
+
throw new Error("missing providerUrl");
|
|
495
|
+
log.info("providerUrl: ", providerUrl);
|
|
496
|
+
provider = new ethers.providers.JsonRpcProvider(providerUrl);
|
|
497
|
+
return [4 /*yield*/, provider.getTransactionCount(from, "latest")];
|
|
498
|
+
case 1:
|
|
499
|
+
nonce = _c.sent();
|
|
500
|
+
permit = {
|
|
501
|
+
details: {
|
|
502
|
+
token: token,
|
|
503
|
+
amount: amount,
|
|
504
|
+
expiration: toDeadline(PERMIT_EXPIRATION),
|
|
505
|
+
nonce: nonce,
|
|
506
|
+
},
|
|
507
|
+
spender: from,
|
|
508
|
+
sigDeadline: toDeadline(PERMIT_SIG_EXPIRATION),
|
|
509
|
+
};
|
|
510
|
+
_b = permit2_sdk_1.AllowanceTransfer.getPermitData(permit, permit2_sdk_1.PERMIT2_ADDRESS, chainId), domain = _b.domain, types = _b.types, values = _b.values;
|
|
511
|
+
tx = {
|
|
512
|
+
type: 'signTypedData',
|
|
513
|
+
txParams: { domain: domain, types: types, values: values }
|
|
514
|
+
};
|
|
515
|
+
return [2 /*return*/, tx];
|
|
516
|
+
case 2:
|
|
517
|
+
e_6 = _c.sent();
|
|
518
|
+
console.error(e_6);
|
|
519
|
+
return [3 /*break*/, 3];
|
|
520
|
+
case 3: return [2 /*return*/];
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
};
|
|
471
525
|
var get_quote = function (quote) {
|
|
472
526
|
return __awaiter(this, void 0, void 0, function () {
|
|
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,
|
|
527
|
+
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, tx2, e_7;
|
|
474
528
|
return __generator(this, function (_d) {
|
|
475
529
|
switch (_d.label) {
|
|
476
530
|
case 0:
|
|
@@ -522,7 +576,7 @@ var get_quote = function (quote) {
|
|
|
522
576
|
log.info("sellTokenAddress: ", sellTokenAddress_1);
|
|
523
577
|
sellTokenContract = new ethers.Contract(sellTokenAddress_1, ERC20_ABI, provider);
|
|
524
578
|
}
|
|
525
|
-
permit2 =
|
|
579
|
+
permit2 = permit2_sdk_1.PERMIT2_ADDRESS;
|
|
526
580
|
router = universal_router_sdk_1.UNIVERSAL_ROUTER_ADDRESS;
|
|
527
581
|
planner = new universal_router_sdk_1.RoutePlanner();
|
|
528
582
|
sellAmountBigNumber = ethers.utils.parseUnits(quote.sellAmount.toString(), 'ether');
|
|
@@ -551,7 +605,7 @@ var get_quote = function (quote) {
|
|
|
551
605
|
console.log("sellTokenContract: ", sellTokenContract);
|
|
552
606
|
log.info(tag, "permit2Address: ", permit2Address);
|
|
553
607
|
log.info(tag, "quote.senderAddress: ", quote.senderAddress.toLowerCase());
|
|
554
|
-
return [4 /*yield*/, sellTokenContract.allowance(quote.senderAddress.toLowerCase(),
|
|
608
|
+
return [4 /*yield*/, sellTokenContract.allowance(quote.senderAddress.toLowerCase(), permit2)];
|
|
555
609
|
case 4:
|
|
556
610
|
currentAllowance = _d.sent();
|
|
557
611
|
console.log("currentAllowance: ", currentAllowance.toString());
|
|
@@ -615,10 +669,29 @@ var get_quote = function (quote) {
|
|
|
615
669
|
amountOut = trade.swaps[0].outputAmount;
|
|
616
670
|
log.info(tag, "amountOut: ", amountOut);
|
|
617
671
|
output.amountOut = amountOut.toFixed(18);
|
|
618
|
-
return [4 /*yield*/,
|
|
672
|
+
return [4 /*yield*/, build_permit_tx({
|
|
673
|
+
from: quote.senderAddress,
|
|
674
|
+
token: sellToken.address,
|
|
675
|
+
amount: "1000000000000000000000000",
|
|
676
|
+
chainId: chainIdInt
|
|
677
|
+
})
|
|
678
|
+
//
|
|
679
|
+
];
|
|
619
680
|
case 14:
|
|
620
681
|
tx = _d.sent();
|
|
621
|
-
|
|
682
|
+
//
|
|
683
|
+
output.txs.push(tx);
|
|
684
|
+
tx2 = {
|
|
685
|
+
type: "evm",
|
|
686
|
+
description: 'swap tokens',
|
|
687
|
+
chain: inputChain,
|
|
688
|
+
inputs: ['permit2sig'],
|
|
689
|
+
txParams: {}
|
|
690
|
+
};
|
|
691
|
+
output.txs.push(tx2);
|
|
692
|
+
//amountOutMin
|
|
693
|
+
// let tx = await buildTx({trade,from:quote.senderAddress, chainId:chainIdInt, provider})
|
|
694
|
+
// log.info(tag,"tx: ",tx)
|
|
622
695
|
output.meta = {
|
|
623
696
|
quoteMode: "ERC20-ERC20"
|
|
624
697
|
};
|
|
@@ -626,17 +699,11 @@ var get_quote = function (quote) {
|
|
|
626
699
|
output.complete = true;
|
|
627
700
|
output.type = 'EVM';
|
|
628
701
|
output.id = uuid();
|
|
629
|
-
output.txs.push({
|
|
630
|
-
type: "evm",
|
|
631
|
-
description: 'swap tokens',
|
|
632
|
-
chain: inputChain,
|
|
633
|
-
txParams: tx
|
|
634
|
-
});
|
|
635
702
|
return [2 /*return*/, output];
|
|
636
703
|
case 15:
|
|
637
|
-
|
|
638
|
-
console.error(tag, "e: ",
|
|
639
|
-
throw
|
|
704
|
+
e_7 = _d.sent();
|
|
705
|
+
console.error(tag, "e: ", e_7);
|
|
706
|
+
throw e_7;
|
|
640
707
|
case 16: return [2 /*return*/];
|
|
641
708
|
}
|
|
642
709
|
});
|