@pioneer-platform/uniswap-client 0.0.18 → 0.0.20
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 +102 -18
- 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,7 +459,7 @@ 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:
|
|
@@ -468,9 +476,72 @@ 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, fullResponse, domain, types, values, typedData, tx, e_6;
|
|
486
|
+
return __generator(this, function (_b) {
|
|
487
|
+
switch (_b.label) {
|
|
488
|
+
case 0:
|
|
489
|
+
_b.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 = _b.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
|
+
fullResponse = permit2_sdk_1.AllowanceTransfer.getPermitData(permit, permit2_sdk_1.PERMIT2_ADDRESS, chainId);
|
|
511
|
+
log.info("fullResponse: ", fullResponse);
|
|
512
|
+
domain = fullResponse.domain, types = fullResponse.types, values = fullResponse.values;
|
|
513
|
+
// Ensure 'EIP712Domain' is included in the types definitions
|
|
514
|
+
// If it's not already present or not recognized for some reason, define it explicitly
|
|
515
|
+
if (!types['EIP712Domain']) {
|
|
516
|
+
types['EIP712Domain'] = [
|
|
517
|
+
{ name: "name", type: "string" },
|
|
518
|
+
{ name: "chainId", type: "uint256" },
|
|
519
|
+
{ name: "verifyingContract", type: "address" },
|
|
520
|
+
];
|
|
521
|
+
}
|
|
522
|
+
typedData = {
|
|
523
|
+
domain: domain,
|
|
524
|
+
types: types,
|
|
525
|
+
message: values, // Use 'values' as the 'message' for EIP712 signing
|
|
526
|
+
primaryType: "PermitSingle" // Assuming 'PermitSingle' as the primaryType based on your types definition
|
|
527
|
+
};
|
|
528
|
+
tx = {
|
|
529
|
+
type: 'signTypedData',
|
|
530
|
+
txParams: typedData // Ensuring txParams contains the correctly formatted EIP-712 data
|
|
531
|
+
};
|
|
532
|
+
return [2 /*return*/, tx];
|
|
533
|
+
case 2:
|
|
534
|
+
e_6 = _b.sent();
|
|
535
|
+
console.error(e_6);
|
|
536
|
+
return [3 /*break*/, 3];
|
|
537
|
+
case 3: return [2 /*return*/];
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
});
|
|
541
|
+
};
|
|
471
542
|
var get_quote = function (quote) {
|
|
472
543
|
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,
|
|
544
|
+
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
545
|
return __generator(this, function (_d) {
|
|
475
546
|
switch (_d.label) {
|
|
476
547
|
case 0:
|
|
@@ -522,7 +593,7 @@ var get_quote = function (quote) {
|
|
|
522
593
|
log.info("sellTokenAddress: ", sellTokenAddress_1);
|
|
523
594
|
sellTokenContract = new ethers.Contract(sellTokenAddress_1, ERC20_ABI, provider);
|
|
524
595
|
}
|
|
525
|
-
permit2 =
|
|
596
|
+
permit2 = permit2_sdk_1.PERMIT2_ADDRESS;
|
|
526
597
|
router = universal_router_sdk_1.UNIVERSAL_ROUTER_ADDRESS;
|
|
527
598
|
planner = new universal_router_sdk_1.RoutePlanner();
|
|
528
599
|
sellAmountBigNumber = ethers.utils.parseUnits(quote.sellAmount.toString(), 'ether');
|
|
@@ -551,7 +622,7 @@ var get_quote = function (quote) {
|
|
|
551
622
|
console.log("sellTokenContract: ", sellTokenContract);
|
|
552
623
|
log.info(tag, "permit2Address: ", permit2Address);
|
|
553
624
|
log.info(tag, "quote.senderAddress: ", quote.senderAddress.toLowerCase());
|
|
554
|
-
return [4 /*yield*/, sellTokenContract.allowance(quote.senderAddress.toLowerCase(),
|
|
625
|
+
return [4 /*yield*/, sellTokenContract.allowance(quote.senderAddress.toLowerCase(), permit2)];
|
|
555
626
|
case 4:
|
|
556
627
|
currentAllowance = _d.sent();
|
|
557
628
|
console.log("currentAllowance: ", currentAllowance.toString());
|
|
@@ -615,10 +686,29 @@ var get_quote = function (quote) {
|
|
|
615
686
|
amountOut = trade.swaps[0].outputAmount;
|
|
616
687
|
log.info(tag, "amountOut: ", amountOut);
|
|
617
688
|
output.amountOut = amountOut.toFixed(18);
|
|
618
|
-
return [4 /*yield*/,
|
|
689
|
+
return [4 /*yield*/, build_permit_tx({
|
|
690
|
+
from: quote.senderAddress,
|
|
691
|
+
token: sellToken.address,
|
|
692
|
+
amount: "1000000000000000000000000",
|
|
693
|
+
chainId: chainIdInt
|
|
694
|
+
})
|
|
695
|
+
//
|
|
696
|
+
];
|
|
619
697
|
case 14:
|
|
620
698
|
tx = _d.sent();
|
|
621
|
-
|
|
699
|
+
//
|
|
700
|
+
output.txs.push(tx);
|
|
701
|
+
tx2 = {
|
|
702
|
+
type: "evm",
|
|
703
|
+
description: 'swap tokens',
|
|
704
|
+
chain: inputChain,
|
|
705
|
+
inputs: ['permit2sig'],
|
|
706
|
+
txParams: {}
|
|
707
|
+
};
|
|
708
|
+
output.txs.push(tx2);
|
|
709
|
+
//amountOutMin
|
|
710
|
+
// let tx = await buildTx({trade,from:quote.senderAddress, chainId:chainIdInt, provider})
|
|
711
|
+
// log.info(tag,"tx: ",tx)
|
|
622
712
|
output.meta = {
|
|
623
713
|
quoteMode: "ERC20-ERC20"
|
|
624
714
|
};
|
|
@@ -626,17 +716,11 @@ var get_quote = function (quote) {
|
|
|
626
716
|
output.complete = true;
|
|
627
717
|
output.type = 'EVM';
|
|
628
718
|
output.id = uuid();
|
|
629
|
-
output.txs.push({
|
|
630
|
-
type: "evm",
|
|
631
|
-
description: 'swap tokens',
|
|
632
|
-
chain: inputChain,
|
|
633
|
-
txParams: tx
|
|
634
|
-
});
|
|
635
719
|
return [2 /*return*/, output];
|
|
636
720
|
case 15:
|
|
637
|
-
|
|
638
|
-
console.error(tag, "e: ",
|
|
639
|
-
throw
|
|
721
|
+
e_7 = _d.sent();
|
|
722
|
+
console.error(tag, "e: ", e_7);
|
|
723
|
+
throw e_7;
|
|
640
724
|
case 16: return [2 /*return*/];
|
|
641
725
|
}
|
|
642
726
|
});
|