@curvefi/api 1.6.1 → 1.9.0
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/README.md +340 -8
- package/lib/boosting.d.ts +3 -0
- package/lib/boosting.js +30 -3
- package/lib/constants/abis/abis-ethereum.js +2 -2
- package/lib/constants/abis/json/registry_exchange.json +0 -37
- package/lib/constants/abis/json/tricrypto2/deposit.json +0 -79
- package/lib/curve.d.ts +1 -0
- package/lib/curve.js +47 -9
- package/lib/index.d.ts +9 -0
- package/lib/index.js +9 -0
- package/lib/pools.d.ts +6 -11
- package/lib/pools.js +413 -343
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +25 -3
- package/package.json +1 -1
- package/lib/constants/abis/json/registry.json +0 -940
package/lib/pools.js
CHANGED
|
@@ -59,14 +59,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
59
59
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
60
60
|
};
|
|
61
61
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
62
|
-
exports.crossAssetExchange = exports.crossAssetExchangeEstimateGas = exports.crossAssetExchangeExpected = exports.crossAssetExchangeOutputAndSlippage = exports._crossAssetExchangeInfo = exports._getSmallAmountForCoin = exports.crossAssetExchangeAvailable = exports.exchange = exports.exchangeEstimateGas = exports.
|
|
62
|
+
exports.crossAssetExchange = exports.crossAssetExchangeEstimateGas = exports.crossAssetExchangeApprove = exports.crossAssetExchangeApproveEstimateGas = exports.crossAssetExchangeIsApproved = exports.crossAssetExchangeExpected = exports.crossAssetExchangeOutputAndSlippage = exports._crossAssetExchangeInfo = exports._getSmallAmountForCoin = exports.crossAssetExchangeAvailable = exports.exchange = exports.exchangeEstimateGas = exports.exchangeApprove = exports.exchangeApproveEstimateGas = exports.exchangeIsApproved = exports.exchangeExpected = exports.getBestPoolAndOutput = exports.Pool = void 0;
|
|
63
63
|
var ethers_1 = require("ethers");
|
|
64
64
|
var utils_1 = require("./utils");
|
|
65
|
-
var registry_exchange_json_1 = __importDefault(require("./constants/abis/json/registry_exchange.json"));
|
|
66
|
-
var registry_json_1 = __importDefault(require("./constants/abis/json/registry.json"));
|
|
67
65
|
var abis_ethereum_1 = require("./constants/abis/abis-ethereum");
|
|
68
66
|
var curve_1 = require("./curve");
|
|
69
67
|
var coins_1 = require("./constants/coins");
|
|
68
|
+
var axios_1 = __importDefault(require("axios"));
|
|
70
69
|
var Pool = /** @class */ (function () {
|
|
71
70
|
function Pool(name) {
|
|
72
71
|
var _this = this;
|
|
@@ -79,7 +78,7 @@ var Pool = /** @class */ (function () {
|
|
|
79
78
|
switch (_a.label) {
|
|
80
79
|
case 0:
|
|
81
80
|
if (amounts.length !== this.underlyingCoinAddresses.length) {
|
|
82
|
-
throw Error(this.name
|
|
81
|
+
throw Error("".concat(this.name, " pool has ").concat(this.underlyingCoinAddresses.length, " coins (amounts provided for ").concat(amounts.length, ")"));
|
|
83
82
|
}
|
|
84
83
|
_amounts = amounts.map(function (amount, i) {
|
|
85
84
|
return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]);
|
|
@@ -113,7 +112,7 @@ var Pool = /** @class */ (function () {
|
|
|
113
112
|
switch (_a.label) {
|
|
114
113
|
case 0:
|
|
115
114
|
if (amounts.length !== this.coinAddresses.length) {
|
|
116
|
-
throw Error(this.name
|
|
115
|
+
throw Error("".concat(this.name, " pool has ").concat(this.coinAddresses.length, " coins (amounts provided for ").concat(amounts.length, ")"));
|
|
117
116
|
}
|
|
118
117
|
_amounts = amounts.map(function (amount, i) {
|
|
119
118
|
return ethers_1.ethers.utils.parseUnits(amount, _this.decimals[i]);
|
|
@@ -251,7 +250,7 @@ var Pool = /** @class */ (function () {
|
|
|
251
250
|
switch (_d.label) {
|
|
252
251
|
case 0:
|
|
253
252
|
if (amounts.length !== this.underlyingCoinAddresses.length) {
|
|
254
|
-
throw Error(this.name
|
|
253
|
+
throw Error("".concat(this.name, " pool has ").concat(this.underlyingCoinAddresses.length, " coins (amounts provided for ").concat(amounts.length, ")"));
|
|
255
254
|
}
|
|
256
255
|
_c = (_a = Object).values;
|
|
257
256
|
return [4 /*yield*/, this.underlyingCoinBalances()];
|
|
@@ -259,7 +258,7 @@ var Pool = /** @class */ (function () {
|
|
|
259
258
|
balances = _c.apply(_a, [_d.sent()]);
|
|
260
259
|
for (i = 0; i < balances.length; i++) {
|
|
261
260
|
if (Number(balances[i]) < Number(amounts[i])) {
|
|
262
|
-
throw Error("Not enough "
|
|
261
|
+
throw Error("Not enough ".concat(this.underlyingCoins[i], ". Actual: ").concat(balances[i], ", required: ").concat(amounts[i]));
|
|
263
262
|
}
|
|
264
263
|
}
|
|
265
264
|
return [4 /*yield*/, (0, utils_1.hasAllowance)(this.underlyingCoinAddresses, amounts, curve_1.curve.signerAddress, this.zap || this.swap)];
|
|
@@ -310,7 +309,7 @@ var Pool = /** @class */ (function () {
|
|
|
310
309
|
switch (_a.label) {
|
|
311
310
|
case 0:
|
|
312
311
|
if (amounts.length !== this.underlyingCoinAddresses.length) {
|
|
313
|
-
throw Error(this.name
|
|
312
|
+
throw Error("".concat(this.name, " pool has ").concat(this.underlyingCoinAddresses.length, " coins (amounts provided for ").concat(amounts.length, ")"));
|
|
314
313
|
}
|
|
315
314
|
_amounts = amounts.map(function (amount, i) {
|
|
316
315
|
return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]);
|
|
@@ -318,7 +317,7 @@ var Pool = /** @class */ (function () {
|
|
|
318
317
|
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
319
318
|
case 1:
|
|
320
319
|
_a.sent();
|
|
321
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
|
|
320
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name)) return [3 /*break*/, 3];
|
|
322
321
|
return [4 /*yield*/, this._addLiquidityZap(_amounts)];
|
|
323
322
|
case 2: return [2 /*return*/, _a.sent()];
|
|
324
323
|
case 3:
|
|
@@ -408,7 +407,7 @@ var Pool = /** @class */ (function () {
|
|
|
408
407
|
switch (_d.label) {
|
|
409
408
|
case 0:
|
|
410
409
|
if (amounts.length !== this.coinAddresses.length) {
|
|
411
|
-
throw Error(this.name
|
|
410
|
+
throw Error("".concat(this.name, " pool has ").concat(this.coinAddresses.length, " coins (amounts provided for ").concat(amounts.length, ")"));
|
|
412
411
|
}
|
|
413
412
|
_c = (_a = Object).values;
|
|
414
413
|
return [4 /*yield*/, this.coinBalances()];
|
|
@@ -416,7 +415,7 @@ var Pool = /** @class */ (function () {
|
|
|
416
415
|
balances = _c.apply(_a, [_d.sent()]);
|
|
417
416
|
for (i = 0; i < balances.length; i++) {
|
|
418
417
|
if (Number(balances[i]) < Number(amounts[i])) {
|
|
419
|
-
throw Error("Not enough "
|
|
418
|
+
throw Error("Not enough ".concat(this.coins[i], ". Actual: ").concat(balances[i], ", required: ").concat(amounts[i]));
|
|
420
419
|
}
|
|
421
420
|
}
|
|
422
421
|
return [4 /*yield*/, (0, utils_1.hasAllowance)(this.coinAddresses, amounts, curve_1.curve.signerAddress, this.swap)];
|
|
@@ -444,7 +443,7 @@ var Pool = /** @class */ (function () {
|
|
|
444
443
|
switch (_a.label) {
|
|
445
444
|
case 0:
|
|
446
445
|
if (amounts.length !== this.coinAddresses.length) {
|
|
447
|
-
throw Error(this.name
|
|
446
|
+
throw Error("".concat(this.name, " pool has ").concat(this.coinAddresses.length, " coins (amounts provided for ").concat(amounts.length, ")"));
|
|
448
447
|
}
|
|
449
448
|
_amounts = amounts.map(function (amount, i) {
|
|
450
449
|
return ethers_1.ethers.utils.parseUnits(amount, _this.decimals[i]);
|
|
@@ -492,7 +491,7 @@ var Pool = /** @class */ (function () {
|
|
|
492
491
|
return __generator(this, function (_a) {
|
|
493
492
|
switch (_a.label) {
|
|
494
493
|
case 0:
|
|
495
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !this.isMeta)
|
|
494
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name) && !this.isMeta)
|
|
496
495
|
return [2 /*return*/, true];
|
|
497
496
|
return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
|
|
498
497
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -503,7 +502,7 @@ var Pool = /** @class */ (function () {
|
|
|
503
502
|
return __generator(this, function (_a) {
|
|
504
503
|
switch (_a.label) {
|
|
505
504
|
case 0:
|
|
506
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !this.isMeta)
|
|
505
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name) && !this.isMeta)
|
|
507
506
|
return [2 /*return*/, 0];
|
|
508
507
|
return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.lpToken], [lpTokenAmount], this.zap)];
|
|
509
508
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -514,7 +513,7 @@ var Pool = /** @class */ (function () {
|
|
|
514
513
|
return __generator(this, function (_a) {
|
|
515
514
|
switch (_a.label) {
|
|
516
515
|
case 0:
|
|
517
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !this.isMeta)
|
|
516
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name) && !this.isMeta)
|
|
518
517
|
return [2 /*return*/, []];
|
|
519
518
|
return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.lpToken], [lpTokenAmount], this.zap)];
|
|
520
519
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -529,7 +528,7 @@ var Pool = /** @class */ (function () {
|
|
|
529
528
|
case 1:
|
|
530
529
|
lpTokenBalance = (_c.sent())['lpToken'];
|
|
531
530
|
if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
|
|
532
|
-
throw Error("Not enough LP tokens. Actual: "
|
|
531
|
+
throw Error("Not enough LP tokens. Actual: ".concat(lpTokenBalance, ", required: ").concat(lpTokenAmount));
|
|
533
532
|
}
|
|
534
533
|
_a = this.zap;
|
|
535
534
|
if (!_a) return [3 /*break*/, 3];
|
|
@@ -542,7 +541,7 @@ var Pool = /** @class */ (function () {
|
|
|
542
541
|
throw Error("Token allowance is needed to estimate gas");
|
|
543
542
|
}
|
|
544
543
|
_lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
|
|
545
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 5];
|
|
544
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name)) return [3 /*break*/, 5];
|
|
546
545
|
return [4 /*yield*/, this._removeLiquidityZap(_lpTokenAmount, true)];
|
|
547
546
|
case 4: return [2 /*return*/, _c.sent()];
|
|
548
547
|
case 5:
|
|
@@ -567,7 +566,7 @@ var Pool = /** @class */ (function () {
|
|
|
567
566
|
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
568
567
|
case 1:
|
|
569
568
|
_a.sent();
|
|
570
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
|
|
569
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name)) return [3 /*break*/, 3];
|
|
571
570
|
return [4 /*yield*/, this._removeLiquidityZap(_lpTokenAmount)];
|
|
572
571
|
case 2: return [2 /*return*/, _a.sent()];
|
|
573
572
|
case 3:
|
|
@@ -607,7 +606,7 @@ var Pool = /** @class */ (function () {
|
|
|
607
606
|
case 1:
|
|
608
607
|
lpTokenBalance = (_a.sent())['lpToken'];
|
|
609
608
|
if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
|
|
610
|
-
throw Error("Not enough LP tokens. Actual: "
|
|
609
|
+
throw Error("Not enough LP tokens. Actual: ".concat(lpTokenBalance, ", required: ").concat(lpTokenAmount));
|
|
611
610
|
}
|
|
612
611
|
if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 3];
|
|
613
612
|
return [4 /*yield*/, this._removeLiquidity(_lpTokenAmount, false, true)];
|
|
@@ -639,6 +638,9 @@ var Pool = /** @class */ (function () {
|
|
|
639
638
|
return __generator(this, function (_a) {
|
|
640
639
|
switch (_a.label) {
|
|
641
640
|
case 0:
|
|
641
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
642
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
643
|
+
}
|
|
642
644
|
amounts = amounts.map(function (a, i) { return Number(a).toFixed(_this.underlyingDecimals[i]); });
|
|
643
645
|
return [4 /*yield*/, this.calcLpTokenAmount(amounts, false)];
|
|
644
646
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -650,6 +652,9 @@ var Pool = /** @class */ (function () {
|
|
|
650
652
|
return __generator(this, function (_c) {
|
|
651
653
|
switch (_c.label) {
|
|
652
654
|
case 0:
|
|
655
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
656
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
657
|
+
}
|
|
653
658
|
totalAmount = amounts.reduce(function (s, a) { return s + Number(a); }, 0);
|
|
654
659
|
_a = Number;
|
|
655
660
|
return [4 /*yield*/, this.removeLiquidityImbalanceExpected(amounts)];
|
|
@@ -666,6 +671,9 @@ var Pool = /** @class */ (function () {
|
|
|
666
671
|
return __generator(this, function (_a) {
|
|
667
672
|
switch (_a.label) {
|
|
668
673
|
case 0:
|
|
674
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
675
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
676
|
+
}
|
|
669
677
|
_amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
|
|
670
678
|
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
|
|
671
679
|
return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
|
|
@@ -690,6 +698,9 @@ var Pool = /** @class */ (function () {
|
|
|
690
698
|
return __generator(this, function (_a) {
|
|
691
699
|
switch (_a.label) {
|
|
692
700
|
case 0:
|
|
701
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
702
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
703
|
+
}
|
|
693
704
|
_amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
|
|
694
705
|
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
|
|
695
706
|
return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
|
|
@@ -714,6 +725,9 @@ var Pool = /** @class */ (function () {
|
|
|
714
725
|
return __generator(this, function (_a) {
|
|
715
726
|
switch (_a.label) {
|
|
716
727
|
case 0:
|
|
728
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
729
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
730
|
+
}
|
|
717
731
|
_amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
|
|
718
732
|
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) return [3 /*break*/, 3];
|
|
719
733
|
return [4 /*yield*/, this._calcLpTokenAmountWithUnderlying(_amounts, false)];
|
|
@@ -737,14 +751,18 @@ var Pool = /** @class */ (function () {
|
|
|
737
751
|
var _this = this;
|
|
738
752
|
return __generator(this, function (_c) {
|
|
739
753
|
switch (_c.label) {
|
|
740
|
-
case 0:
|
|
754
|
+
case 0:
|
|
755
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
756
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
757
|
+
}
|
|
758
|
+
return [4 /*yield*/, this.removeLiquidityImbalanceExpected(amounts)];
|
|
741
759
|
case 1:
|
|
742
760
|
lpTokenAmount = _c.sent();
|
|
743
761
|
return [4 /*yield*/, this.lpTokenBalances()];
|
|
744
762
|
case 2:
|
|
745
763
|
lpTokenBalance = (_c.sent())['lpToken'];
|
|
746
764
|
if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
|
|
747
|
-
throw Error("Not enough LP tokens. Actual: "
|
|
765
|
+
throw Error("Not enough LP tokens. Actual: ".concat(lpTokenBalance, ", required: ").concat(lpTokenAmount));
|
|
748
766
|
}
|
|
749
767
|
_a = this.zap;
|
|
750
768
|
if (!_a) return [3 /*break*/, 4];
|
|
@@ -779,6 +797,9 @@ var Pool = /** @class */ (function () {
|
|
|
779
797
|
return __generator(this, function (_a) {
|
|
780
798
|
switch (_a.label) {
|
|
781
799
|
case 0:
|
|
800
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
801
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
802
|
+
}
|
|
782
803
|
_amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.underlyingDecimals[i]); });
|
|
783
804
|
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
784
805
|
case 1:
|
|
@@ -804,6 +825,9 @@ var Pool = /** @class */ (function () {
|
|
|
804
825
|
return __generator(this, function (_a) {
|
|
805
826
|
switch (_a.label) {
|
|
806
827
|
case 0:
|
|
828
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
829
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
830
|
+
}
|
|
807
831
|
amounts = amounts.map(function (a, i) { return Number(a).toFixed(_this.underlyingDecimals[i]); });
|
|
808
832
|
return [4 /*yield*/, this.calcLpTokenAmountWrapped(amounts, false)];
|
|
809
833
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -815,6 +839,9 @@ var Pool = /** @class */ (function () {
|
|
|
815
839
|
return __generator(this, function (_c) {
|
|
816
840
|
switch (_c.label) {
|
|
817
841
|
case 0:
|
|
842
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
843
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
844
|
+
}
|
|
818
845
|
totalAmount = amounts.reduce(function (s, a) { return s + Number(a); }, 0);
|
|
819
846
|
_a = Number;
|
|
820
847
|
return [4 /*yield*/, this.removeLiquidityImbalanceWrappedExpected(amounts)];
|
|
@@ -830,14 +857,18 @@ var Pool = /** @class */ (function () {
|
|
|
830
857
|
var _this = this;
|
|
831
858
|
return __generator(this, function (_a) {
|
|
832
859
|
switch (_a.label) {
|
|
833
|
-
case 0:
|
|
860
|
+
case 0:
|
|
861
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
862
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
863
|
+
}
|
|
864
|
+
return [4 /*yield*/, this.removeLiquidityImbalanceExpected(amounts)];
|
|
834
865
|
case 1:
|
|
835
866
|
lpTokenAmount = _a.sent();
|
|
836
867
|
return [4 /*yield*/, this.lpTokenBalances()];
|
|
837
868
|
case 2:
|
|
838
869
|
lpTokenBalance = (_a.sent())['lpToken'];
|
|
839
870
|
if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
|
|
840
|
-
throw Error("Not enough LP tokens. Actual: "
|
|
871
|
+
throw Error("Not enough LP tokens. Actual: ".concat(lpTokenBalance, ", required: ").concat(lpTokenAmount));
|
|
841
872
|
}
|
|
842
873
|
_amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.decimals[i]); });
|
|
843
874
|
if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 4];
|
|
@@ -856,6 +887,9 @@ var Pool = /** @class */ (function () {
|
|
|
856
887
|
return __generator(this, function (_a) {
|
|
857
888
|
switch (_a.label) {
|
|
858
889
|
case 0:
|
|
890
|
+
if (['tricrypto2'].includes(this.name)) {
|
|
891
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_imbalance method"));
|
|
892
|
+
}
|
|
859
893
|
_amounts = amounts.map(function (amount, i) { return ethers_1.ethers.utils.parseUnits(amount, _this.decimals[i]); });
|
|
860
894
|
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
861
895
|
case 1:
|
|
@@ -913,7 +947,7 @@ var Pool = /** @class */ (function () {
|
|
|
913
947
|
return __generator(this, function (_a) {
|
|
914
948
|
switch (_a.label) {
|
|
915
949
|
case 0:
|
|
916
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
|
|
950
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
|
|
917
951
|
return [2 /*return*/, true];
|
|
918
952
|
return [4 /*yield*/, (0, utils_1.hasAllowance)([this.lpToken], [lpTokenAmount], curve_1.curve.signerAddress, this.zap)];
|
|
919
953
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -924,7 +958,7 @@ var Pool = /** @class */ (function () {
|
|
|
924
958
|
return __generator(this, function (_a) {
|
|
925
959
|
switch (_a.label) {
|
|
926
960
|
case 0:
|
|
927
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
|
|
961
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
|
|
928
962
|
return [2 /*return*/, 0];
|
|
929
963
|
return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.lpToken], [lpTokenAmount], this.zap)];
|
|
930
964
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -935,7 +969,7 @@ var Pool = /** @class */ (function () {
|
|
|
935
969
|
return __generator(this, function (_a) {
|
|
936
970
|
switch (_a.label) {
|
|
937
971
|
case 0:
|
|
938
|
-
if (!['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
|
|
972
|
+
if (!['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name) && !(this.name === 'susd') && !this.isMeta)
|
|
939
973
|
return [2 /*return*/, []];
|
|
940
974
|
return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.lpToken], [lpTokenAmount], this.zap)];
|
|
941
975
|
case 1: return [2 /*return*/, _a.sent()];
|
|
@@ -950,7 +984,7 @@ var Pool = /** @class */ (function () {
|
|
|
950
984
|
case 1:
|
|
951
985
|
lpTokenBalance = (_c.sent())['lpToken'];
|
|
952
986
|
if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
|
|
953
|
-
throw Error("Not enough LP tokens. Actual: "
|
|
987
|
+
throw Error("Not enough LP tokens. Actual: ".concat(lpTokenBalance, ", required: ").concat(lpTokenAmount));
|
|
954
988
|
}
|
|
955
989
|
_a = this.zap;
|
|
956
990
|
if (!_a) return [3 /*break*/, 3];
|
|
@@ -964,7 +998,7 @@ var Pool = /** @class */ (function () {
|
|
|
964
998
|
}
|
|
965
999
|
i = this._getCoinIdx(coin);
|
|
966
1000
|
_lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
|
|
967
|
-
if (!(['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.name === 'susd' || this.isMeta)) return [3 /*break*/, 5];
|
|
1001
|
+
if (!(['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name) || this.name === 'susd' || this.isMeta)) return [3 /*break*/, 5];
|
|
968
1002
|
return [4 /*yield*/, this._removeLiquidityOneCoinZap(_lpTokenAmount, i, true)];
|
|
969
1003
|
case 4: return [2 /*return*/, _c.sent()];
|
|
970
1004
|
case 5:
|
|
@@ -988,7 +1022,7 @@ var Pool = /** @class */ (function () {
|
|
|
988
1022
|
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
989
1023
|
case 1:
|
|
990
1024
|
_a.sent();
|
|
991
|
-
if (!(['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name) || this.name === 'susd' || this.isMeta)) return [3 /*break*/, 3];
|
|
1025
|
+
if (!(['compound', 'usdt', 'y', 'busd', 'pax', 'tricrypto2'].includes(this.name) || this.name === 'susd' || this.isMeta)) return [3 /*break*/, 3];
|
|
992
1026
|
return [4 /*yield*/, this._removeLiquidityOneCoinZap(_lpTokenAmount, i)];
|
|
993
1027
|
case 2: return [2 /*return*/, _a.sent()];
|
|
994
1028
|
case 3:
|
|
@@ -1008,7 +1042,7 @@ var Pool = /** @class */ (function () {
|
|
|
1008
1042
|
switch (_a.label) {
|
|
1009
1043
|
case 0:
|
|
1010
1044
|
if (['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) {
|
|
1011
|
-
throw Error(this.name
|
|
1045
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_one_coin method for wrapped tokens"));
|
|
1012
1046
|
}
|
|
1013
1047
|
i = this._getCoinIdx(coin, false);
|
|
1014
1048
|
_lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
|
|
@@ -1047,11 +1081,11 @@ var Pool = /** @class */ (function () {
|
|
|
1047
1081
|
case 1:
|
|
1048
1082
|
lpTokenBalance = (_a.sent())['lpToken'];
|
|
1049
1083
|
if (Number(lpTokenBalance) < Number(lpTokenAmount)) {
|
|
1050
|
-
throw Error("Not enough LP tokens. Actual: "
|
|
1084
|
+
throw Error("Not enough LP tokens. Actual: ".concat(lpTokenBalance, ", required: ").concat(lpTokenAmount));
|
|
1051
1085
|
}
|
|
1052
1086
|
i = this._getCoinIdx(coin, false);
|
|
1053
1087
|
if (['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) {
|
|
1054
|
-
throw Error(this.name
|
|
1088
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_one_coin method for wrapped tokens"));
|
|
1055
1089
|
}
|
|
1056
1090
|
_lpTokenAmount = ethers_1.ethers.utils.parseUnits(lpTokenAmount);
|
|
1057
1091
|
if (!['aave', 'saave', 'ib'].includes(this.name)) return [3 /*break*/, 3];
|
|
@@ -1069,7 +1103,7 @@ var Pool = /** @class */ (function () {
|
|
|
1069
1103
|
case 0:
|
|
1070
1104
|
i = this._getCoinIdx(coin, false);
|
|
1071
1105
|
if (['compound', 'usdt', 'y', 'busd', 'pax'].includes(this.name)) {
|
|
1072
|
-
throw Error(this.name
|
|
1106
|
+
throw Error("".concat(this.name, " pool doesn't have remove_liquidity_one_coin method for wrapped tokens"));
|
|
1073
1107
|
}
|
|
1074
1108
|
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
1075
1109
|
case 1:
|
|
@@ -1156,70 +1190,70 @@ var Pool = /** @class */ (function () {
|
|
|
1156
1190
|
}); };
|
|
1157
1191
|
this.balances = function () {
|
|
1158
1192
|
var addresses = [];
|
|
1159
|
-
for (var
|
|
1160
|
-
addresses[
|
|
1193
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1194
|
+
addresses[_i] = arguments[_i];
|
|
1161
1195
|
}
|
|
1162
1196
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1163
|
-
return __generator(this, function (
|
|
1164
|
-
switch (
|
|
1197
|
+
return __generator(this, function (_a) {
|
|
1198
|
+
switch (_a.label) {
|
|
1165
1199
|
case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([__spreadArray(__spreadArray(['lpToken', 'gauge'], this.underlyingCoins, true), this.coins, true), __spreadArray(__spreadArray([this.lpToken, this.gauge], this.underlyingCoinAddresses, true), this.coinAddresses, true)], addresses, false))];
|
|
1166
|
-
case 1: return [2 /*return*/,
|
|
1200
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1167
1201
|
}
|
|
1168
1202
|
});
|
|
1169
1203
|
});
|
|
1170
1204
|
};
|
|
1171
1205
|
this.lpTokenBalances = function () {
|
|
1172
1206
|
var addresses = [];
|
|
1173
|
-
for (var
|
|
1174
|
-
addresses[
|
|
1207
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1208
|
+
addresses[_i] = arguments[_i];
|
|
1175
1209
|
}
|
|
1176
1210
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1177
|
-
return __generator(this, function (
|
|
1178
|
-
switch (
|
|
1211
|
+
return __generator(this, function (_a) {
|
|
1212
|
+
switch (_a.label) {
|
|
1179
1213
|
case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([['lpToken', 'gauge'], [this.lpToken, this.gauge]], addresses, false))];
|
|
1180
|
-
case 1: return [2 /*return*/,
|
|
1214
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1181
1215
|
}
|
|
1182
1216
|
});
|
|
1183
1217
|
});
|
|
1184
1218
|
};
|
|
1185
1219
|
this.underlyingCoinBalances = function () {
|
|
1186
1220
|
var addresses = [];
|
|
1187
|
-
for (var
|
|
1188
|
-
addresses[
|
|
1221
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1222
|
+
addresses[_i] = arguments[_i];
|
|
1189
1223
|
}
|
|
1190
1224
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1191
|
-
return __generator(this, function (
|
|
1192
|
-
switch (
|
|
1225
|
+
return __generator(this, function (_a) {
|
|
1226
|
+
switch (_a.label) {
|
|
1193
1227
|
case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([this.underlyingCoins, this.underlyingCoinAddresses], addresses, false))];
|
|
1194
|
-
case 1: return [2 /*return*/,
|
|
1228
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1195
1229
|
}
|
|
1196
1230
|
});
|
|
1197
1231
|
});
|
|
1198
1232
|
};
|
|
1199
1233
|
this.coinBalances = function () {
|
|
1200
1234
|
var addresses = [];
|
|
1201
|
-
for (var
|
|
1202
|
-
addresses[
|
|
1235
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1236
|
+
addresses[_i] = arguments[_i];
|
|
1203
1237
|
}
|
|
1204
1238
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1205
|
-
return __generator(this, function (
|
|
1206
|
-
switch (
|
|
1239
|
+
return __generator(this, function (_a) {
|
|
1240
|
+
switch (_a.label) {
|
|
1207
1241
|
case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([this.coins, this.coinAddresses], addresses, false))];
|
|
1208
|
-
case 1: return [2 /*return*/,
|
|
1242
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1209
1243
|
}
|
|
1210
1244
|
});
|
|
1211
1245
|
});
|
|
1212
1246
|
};
|
|
1213
1247
|
this.allCoinBalances = function () {
|
|
1214
1248
|
var addresses = [];
|
|
1215
|
-
for (var
|
|
1216
|
-
addresses[
|
|
1249
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1250
|
+
addresses[_i] = arguments[_i];
|
|
1217
1251
|
}
|
|
1218
1252
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1219
|
-
return __generator(this, function (
|
|
1220
|
-
switch (
|
|
1253
|
+
return __generator(this, function (_a) {
|
|
1254
|
+
switch (_a.label) {
|
|
1221
1255
|
case 0: return [4 /*yield*/, this._balances.apply(this, __spreadArray([__spreadArray(__spreadArray([], this.underlyingCoins, true), this.coins, true), __spreadArray(__spreadArray([], this.underlyingCoinAddresses, true), this.coinAddresses, true)], addresses, false))];
|
|
1222
|
-
case 1: return [2 /*return*/,
|
|
1256
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
1223
1257
|
}
|
|
1224
1258
|
});
|
|
1225
1259
|
});
|
|
@@ -1279,9 +1313,6 @@ var Pool = /** @class */ (function () {
|
|
|
1279
1313
|
return __generator(this, function (_d) {
|
|
1280
1314
|
switch (_d.label) {
|
|
1281
1315
|
case 0:
|
|
1282
|
-
if (this.name === 'tricrypto2') {
|
|
1283
|
-
throw Error("Use exchangeTricryptoEstimateGas method for tricrypto2 pool instead");
|
|
1284
|
-
}
|
|
1285
1316
|
i = this._getCoinIdx(inputCoin);
|
|
1286
1317
|
j = this._getCoinIdx(outputCoin);
|
|
1287
1318
|
_c = (_a = Object).values;
|
|
@@ -1289,7 +1320,7 @@ var Pool = /** @class */ (function () {
|
|
|
1289
1320
|
case 1:
|
|
1290
1321
|
inputCoinBalance = _c.apply(_a, [_d.sent()])[i];
|
|
1291
1322
|
if (Number(inputCoinBalance) < Number(amount)) {
|
|
1292
|
-
throw Error("Not enough "
|
|
1323
|
+
throw Error("Not enough ".concat(this.underlyingCoins[i], ". Actual: ").concat(inputCoinBalance, ", required: ").concat(amount));
|
|
1293
1324
|
}
|
|
1294
1325
|
return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
|
|
1295
1326
|
case 2:
|
|
@@ -1304,8 +1335,11 @@ var Pool = /** @class */ (function () {
|
|
|
1304
1335
|
contract = curve_1.curve.contracts[this.swap].contract;
|
|
1305
1336
|
exchangeMethod = Object.prototype.hasOwnProperty.call(contract, 'exchange_underlying') ? 'exchange_underlying' : 'exchange';
|
|
1306
1337
|
value = (0, utils_1.isEth)(this.underlyingCoinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
|
|
1307
|
-
|
|
1338
|
+
if (!(this.name === "tricrypto2")) return [3 /*break*/, 5];
|
|
1339
|
+
return [4 /*yield*/, contract.estimateGas[exchangeMethod](i, j, _amount, _minRecvAmount, true, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1308
1340
|
case 4: return [2 /*return*/, (_d.sent()).toNumber()];
|
|
1341
|
+
case 5: return [4 /*yield*/, contract.estimateGas[exchangeMethod](i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1342
|
+
case 6: return [2 /*return*/, (_d.sent()).toNumber()];
|
|
1309
1343
|
}
|
|
1310
1344
|
});
|
|
1311
1345
|
});
|
|
@@ -1313,13 +1347,10 @@ var Pool = /** @class */ (function () {
|
|
|
1313
1347
|
this.exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
|
|
1314
1348
|
if (maxSlippage === void 0) { maxSlippage = 0.01; }
|
|
1315
1349
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1316
|
-
var i, j, _amount, _expected, _minRecvAmount, contract, exchangeMethod, value, gasLimit;
|
|
1350
|
+
var i, j, _amount, _expected, _minRecvAmount, contract, exchangeMethod, value, gasLimit_1, gasLimit;
|
|
1317
1351
|
return __generator(this, function (_a) {
|
|
1318
1352
|
switch (_a.label) {
|
|
1319
1353
|
case 0:
|
|
1320
|
-
if (this.name === 'tricrypto2') {
|
|
1321
|
-
throw Error("Use exchangeTricrypto method for tricrypto2 pool instead");
|
|
1322
|
-
}
|
|
1323
1354
|
i = this._getCoinIdx(inputCoin);
|
|
1324
1355
|
j = this._getCoinIdx(outputCoin);
|
|
1325
1356
|
_amount = ethers_1.ethers.utils.parseUnits(amount, this.underlyingDecimals[i]);
|
|
@@ -1336,135 +1367,17 @@ var Pool = /** @class */ (function () {
|
|
|
1336
1367
|
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
1337
1368
|
case 3:
|
|
1338
1369
|
_a.sent();
|
|
1339
|
-
|
|
1370
|
+
if (!(this.name === 'tricrypto2')) return [3 /*break*/, 6];
|
|
1371
|
+
return [4 /*yield*/, contract.estimateGas[exchangeMethod](i, j, _amount, _minRecvAmount, true, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1340
1372
|
case 4:
|
|
1341
|
-
|
|
1342
|
-
return [4 /*yield*/, contract[exchangeMethod](i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit:
|
|
1373
|
+
gasLimit_1 = (_a.sent()).mul(130).div(100);
|
|
1374
|
+
return [4 /*yield*/, contract[exchangeMethod](i, j, _amount, _minRecvAmount, true, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit_1 }))];
|
|
1343
1375
|
case 5: return [2 /*return*/, (_a.sent()).hash];
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
});
|
|
1347
|
-
};
|
|
1348
|
-
this.exchangeTricryptoIsApproved = function (inputCoin, amount, useEth) {
|
|
1349
|
-
if (useEth === void 0) { useEth = false; }
|
|
1350
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
1351
|
-
var i;
|
|
1352
|
-
return __generator(this, function (_a) {
|
|
1353
|
-
switch (_a.label) {
|
|
1354
|
-
case 0:
|
|
1355
|
-
i = this._getCoinIdx(inputCoin);
|
|
1356
|
-
if (i === 2 && useEth)
|
|
1357
|
-
return [2 /*return*/, true];
|
|
1358
|
-
return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
|
|
1359
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1360
|
-
}
|
|
1361
|
-
});
|
|
1362
|
-
});
|
|
1363
|
-
};
|
|
1364
|
-
this.exchangeTricryptoApproveEstimateGas = function (inputCoin, amount, useEth) {
|
|
1365
|
-
if (useEth === void 0) { useEth = false; }
|
|
1366
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
1367
|
-
var i;
|
|
1368
|
-
return __generator(this, function (_a) {
|
|
1369
|
-
switch (_a.label) {
|
|
1370
|
-
case 0:
|
|
1371
|
-
i = this._getCoinIdx(inputCoin);
|
|
1372
|
-
if (i === 2 && useEth)
|
|
1373
|
-
return [2 /*return*/, 0];
|
|
1374
|
-
return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.underlyingCoinAddresses[i]], [amount], this.swap)];
|
|
1375
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1376
|
-
}
|
|
1377
|
-
});
|
|
1378
|
-
});
|
|
1379
|
-
};
|
|
1380
|
-
this.exchangeTricryptoApprove = function (inputCoin, amount, useEth) {
|
|
1381
|
-
if (useEth === void 0) { useEth = false; }
|
|
1382
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
1383
|
-
var i;
|
|
1384
|
-
return __generator(this, function (_a) {
|
|
1385
|
-
switch (_a.label) {
|
|
1386
|
-
case 0:
|
|
1387
|
-
i = this._getCoinIdx(inputCoin);
|
|
1388
|
-
if (i === 2 && useEth)
|
|
1389
|
-
return [2 /*return*/, []];
|
|
1390
|
-
return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.underlyingCoinAddresses[i]], [amount], this.swap)];
|
|
1391
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1392
|
-
}
|
|
1393
|
-
});
|
|
1394
|
-
});
|
|
1395
|
-
};
|
|
1396
|
-
this.exchangeTricryptoEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage, useEth) {
|
|
1397
|
-
if (maxSlippage === void 0) { maxSlippage = 0.01; }
|
|
1398
|
-
if (useEth === void 0) { useEth = false; }
|
|
1399
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
1400
|
-
var i, j, inputCoinBalance, _a, _c, _amount, _expected, _minRecvAmount, contract, value;
|
|
1401
|
-
return __generator(this, function (_d) {
|
|
1402
|
-
switch (_d.label) {
|
|
1403
|
-
case 0:
|
|
1404
|
-
if (this.name !== 'tricrypto2') {
|
|
1405
|
-
throw Error("This method is for only tricrypto2 pool");
|
|
1406
|
-
}
|
|
1407
|
-
i = this._getCoinIdx(inputCoin);
|
|
1408
|
-
j = this._getCoinIdx(outputCoin);
|
|
1409
|
-
_c = (_a = Object).values;
|
|
1410
|
-
return [4 /*yield*/, this.underlyingCoinBalances()];
|
|
1411
|
-
case 1:
|
|
1412
|
-
inputCoinBalance = _c.apply(_a, [_d.sent()])[i];
|
|
1413
|
-
if (Number(inputCoinBalance) < Number(amount)) {
|
|
1414
|
-
throw Error("Not enough " + this.underlyingCoins[i] + ". Actual: " + inputCoinBalance + ", required: " + amount);
|
|
1415
|
-
}
|
|
1416
|
-
return [4 /*yield*/, (0, utils_1.hasAllowance)([this.underlyingCoinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
|
|
1417
|
-
case 2:
|
|
1418
|
-
if (!(_d.sent())) {
|
|
1419
|
-
throw Error("Token allowance is needed to estimate gas");
|
|
1420
|
-
}
|
|
1421
|
-
_amount = ethers_1.ethers.utils.parseUnits(amount, this.underlyingDecimals[i]);
|
|
1422
|
-
return [4 /*yield*/, this._getExchangeOutput(i, j, _amount)];
|
|
1423
|
-
case 3:
|
|
1424
|
-
_expected = _d.sent();
|
|
1425
|
-
_minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
|
|
1426
|
-
contract = curve_1.curve.contracts[this.swap].contract;
|
|
1427
|
-
value = useEth && i == 2 ? _amount : ethers_1.ethers.BigNumber.from(0);
|
|
1428
|
-
return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, useEth, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1429
|
-
case 4: return [2 /*return*/, (_d.sent()).toNumber()];
|
|
1430
|
-
}
|
|
1431
|
-
});
|
|
1432
|
-
});
|
|
1433
|
-
};
|
|
1434
|
-
this.exchangeTricrypto = function (inputCoin, outputCoin, amount, maxSlippage, useEth) {
|
|
1435
|
-
if (maxSlippage === void 0) { maxSlippage = 0.01; }
|
|
1436
|
-
if (useEth === void 0) { useEth = false; }
|
|
1437
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
1438
|
-
var i, j, _amount, _expected, _minRecvAmount, contract, value, gasLimit;
|
|
1439
|
-
return __generator(this, function (_a) {
|
|
1440
|
-
switch (_a.label) {
|
|
1441
|
-
case 0:
|
|
1442
|
-
if (this.name !== 'tricrypto2') {
|
|
1443
|
-
throw Error("This method is for only tricrypto2 pool");
|
|
1444
|
-
}
|
|
1445
|
-
i = this._getCoinIdx(inputCoin);
|
|
1446
|
-
j = this._getCoinIdx(outputCoin);
|
|
1447
|
-
_amount = ethers_1.ethers.utils.parseUnits(amount, this.underlyingDecimals[i]);
|
|
1448
|
-
return [4 /*yield*/, this._getExchangeOutput(i, j, _amount)];
|
|
1449
|
-
case 1:
|
|
1450
|
-
_expected = _a.sent();
|
|
1451
|
-
_minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
|
|
1452
|
-
if (!(i !== 2 || !useEth)) return [3 /*break*/, 3];
|
|
1453
|
-
return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.underlyingCoinAddresses[i]], [_amount], this.swap)];
|
|
1454
|
-
case 2:
|
|
1455
|
-
_a.sent();
|
|
1456
|
-
_a.label = 3;
|
|
1457
|
-
case 3:
|
|
1458
|
-
contract = curve_1.curve.contracts[this.swap].contract;
|
|
1459
|
-
value = useEth && i == 2 ? _amount : ethers_1.ethers.BigNumber.from(0);
|
|
1460
|
-
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
1461
|
-
case 4:
|
|
1462
|
-
_a.sent();
|
|
1463
|
-
return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, useEth, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1464
|
-
case 5:
|
|
1376
|
+
case 6: return [4 /*yield*/, contract.estimateGas[exchangeMethod](i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1377
|
+
case 7:
|
|
1465
1378
|
gasLimit = (_a.sent()).mul(130).div(100);
|
|
1466
|
-
return [4 /*yield*/, contract
|
|
1467
|
-
case
|
|
1379
|
+
return [4 /*yield*/, contract[exchangeMethod](i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
|
|
1380
|
+
case 8: return [2 /*return*/, (_a.sent()).hash];
|
|
1468
1381
|
}
|
|
1469
1382
|
});
|
|
1470
1383
|
});
|
|
@@ -1489,7 +1402,7 @@ var Pool = /** @class */ (function () {
|
|
|
1489
1402
|
return __generator(this, function (_a) {
|
|
1490
1403
|
switch (_a.label) {
|
|
1491
1404
|
case 0:
|
|
1492
|
-
i = this._getCoinIdx(inputCoin);
|
|
1405
|
+
i = this._getCoinIdx(inputCoin, false);
|
|
1493
1406
|
return [4 /*yield*/, (0, utils_1.hasAllowance)([this.coinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
|
|
1494
1407
|
case 1: return [2 /*return*/, _a.sent()];
|
|
1495
1408
|
}
|
|
@@ -1500,7 +1413,7 @@ var Pool = /** @class */ (function () {
|
|
|
1500
1413
|
return __generator(this, function (_a) {
|
|
1501
1414
|
switch (_a.label) {
|
|
1502
1415
|
case 0:
|
|
1503
|
-
i = this._getCoinIdx(inputCoin);
|
|
1416
|
+
i = this._getCoinIdx(inputCoin, false);
|
|
1504
1417
|
return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([this.coinAddresses[i]], [amount], this.swap)];
|
|
1505
1418
|
case 1: return [2 /*return*/, _a.sent()];
|
|
1506
1419
|
}
|
|
@@ -1511,7 +1424,7 @@ var Pool = /** @class */ (function () {
|
|
|
1511
1424
|
return __generator(this, function (_a) {
|
|
1512
1425
|
switch (_a.label) {
|
|
1513
1426
|
case 0:
|
|
1514
|
-
i = this._getCoinIdx(inputCoin);
|
|
1427
|
+
i = this._getCoinIdx(inputCoin, false);
|
|
1515
1428
|
return [4 /*yield*/, (0, utils_1.ensureAllowance)([this.coinAddresses[i]], [amount], this.swap)];
|
|
1516
1429
|
case 1: return [2 /*return*/, _a.sent()];
|
|
1517
1430
|
}
|
|
@@ -1531,7 +1444,7 @@ var Pool = /** @class */ (function () {
|
|
|
1531
1444
|
case 1:
|
|
1532
1445
|
inputCoinBalance = _c.apply(_a, [_d.sent()])[i];
|
|
1533
1446
|
if (Number(inputCoinBalance) < Number(amount)) {
|
|
1534
|
-
throw Error("Not enough "
|
|
1447
|
+
throw Error("Not enough ".concat(this.coins[i], ". Actual: ").concat(inputCoinBalance, ", required: ").concat(amount));
|
|
1535
1448
|
}
|
|
1536
1449
|
return [4 /*yield*/, (0, utils_1.hasAllowance)([this.coinAddresses[i]], [amount], curve_1.curve.signerAddress, this.swap)];
|
|
1537
1450
|
case 2:
|
|
@@ -1545,8 +1458,11 @@ var Pool = /** @class */ (function () {
|
|
|
1545
1458
|
_minRecvAmount = _expected.mul((1 - maxSlippage) * 100).div(100);
|
|
1546
1459
|
contract = curve_1.curve.contracts[this.swap].contract;
|
|
1547
1460
|
value = (0, utils_1.isEth)(this.coinAddresses[i]) ? _amount : ethers_1.ethers.BigNumber.from(0);
|
|
1548
|
-
|
|
1461
|
+
if (!(this.name === 'tricrypto2')) return [3 /*break*/, 5];
|
|
1462
|
+
return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, false, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1549
1463
|
case 4: return [2 /*return*/, (_d.sent()).toNumber()];
|
|
1464
|
+
case 5: return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1465
|
+
case 6: return [2 /*return*/, (_d.sent()).toNumber()];
|
|
1550
1466
|
}
|
|
1551
1467
|
});
|
|
1552
1468
|
});
|
|
@@ -1554,7 +1470,7 @@ var Pool = /** @class */ (function () {
|
|
|
1554
1470
|
this.exchangeWrapped = function (inputCoin, outputCoin, amount, maxSlippage) {
|
|
1555
1471
|
if (maxSlippage === void 0) { maxSlippage = 0.01; }
|
|
1556
1472
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1557
|
-
var i, j, _amount, _expected, _minRecvAmount, contract, value, gasLimit;
|
|
1473
|
+
var i, j, _amount, _expected, _minRecvAmount, contract, value, gasLimit_2, gasLimit;
|
|
1558
1474
|
return __generator(this, function (_a) {
|
|
1559
1475
|
switch (_a.label) {
|
|
1560
1476
|
case 0:
|
|
@@ -1573,24 +1489,30 @@ var Pool = /** @class */ (function () {
|
|
|
1573
1489
|
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
1574
1490
|
case 3:
|
|
1575
1491
|
_a.sent();
|
|
1576
|
-
|
|
1492
|
+
if (!(this.name === 'tricrypto2')) return [3 /*break*/, 6];
|
|
1493
|
+
return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, false, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1577
1494
|
case 4:
|
|
1495
|
+
gasLimit_2 = (_a.sent()).mul(130).div(100);
|
|
1496
|
+
return [4 /*yield*/, contract.exchange(i, j, _amount, _minRecvAmount, false, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit_2 }))];
|
|
1497
|
+
case 5: return [2 /*return*/, (_a.sent()).hash];
|
|
1498
|
+
case 6: return [4 /*yield*/, contract.estimateGas.exchange(i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
1499
|
+
case 7:
|
|
1578
1500
|
gasLimit = (_a.sent()).mul(130).div(100);
|
|
1579
1501
|
return [4 /*yield*/, contract.exchange(i, j, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
|
|
1580
|
-
case
|
|
1502
|
+
case 8: return [2 /*return*/, (_a.sent()).hash];
|
|
1581
1503
|
}
|
|
1582
1504
|
});
|
|
1583
1505
|
});
|
|
1584
1506
|
};
|
|
1585
1507
|
this.gaugeMaxBoostedDeposit = function () {
|
|
1586
1508
|
var addresses = [];
|
|
1587
|
-
for (var
|
|
1588
|
-
addresses[
|
|
1509
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1510
|
+
addresses[_i] = arguments[_i];
|
|
1589
1511
|
}
|
|
1590
1512
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1591
|
-
var votingEscrowContract, gaugeContract, contractCalls, response,
|
|
1592
|
-
return __generator(this, function (
|
|
1593
|
-
switch (
|
|
1513
|
+
var votingEscrowContract, gaugeContract, contractCalls, response, _a, veTotalSupply, gaugeTotalSupply, resultBN, result, _c, _d, entry;
|
|
1514
|
+
return __generator(this, function (_e) {
|
|
1515
|
+
switch (_e.label) {
|
|
1594
1516
|
case 0:
|
|
1595
1517
|
if (addresses.length == 1 && Array.isArray(addresses[0]))
|
|
1596
1518
|
addresses = addresses[0];
|
|
@@ -1602,15 +1524,15 @@ var Pool = /** @class */ (function () {
|
|
|
1602
1524
|
});
|
|
1603
1525
|
return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
|
|
1604
1526
|
case 1:
|
|
1605
|
-
response = (
|
|
1606
|
-
|
|
1527
|
+
response = (_e.sent()).map(function (value) { return (0, utils_1.toBN)(value); });
|
|
1528
|
+
_a = response.splice(0, 2), veTotalSupply = _a[0], gaugeTotalSupply = _a[1];
|
|
1607
1529
|
resultBN = {};
|
|
1608
1530
|
addresses.forEach(function (acct, i) {
|
|
1609
1531
|
resultBN[acct] = response[i].div(veTotalSupply).times(gaugeTotalSupply);
|
|
1610
1532
|
});
|
|
1611
1533
|
result = {};
|
|
1612
|
-
for (
|
|
1613
|
-
entry =
|
|
1534
|
+
for (_c = 0, _d = Object.entries(resultBN); _c < _d.length; _c++) {
|
|
1535
|
+
entry = _d[_c];
|
|
1614
1536
|
result[entry[0]] = (0, utils_1.toStringFromBN)(entry[1]);
|
|
1615
1537
|
}
|
|
1616
1538
|
return [2 /*return*/, result];
|
|
@@ -1620,13 +1542,13 @@ var Pool = /** @class */ (function () {
|
|
|
1620
1542
|
};
|
|
1621
1543
|
this.gaugeOptimalDeposits = function () {
|
|
1622
1544
|
var accounts = [];
|
|
1623
|
-
for (var
|
|
1624
|
-
accounts[
|
|
1545
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1546
|
+
accounts[_i] = arguments[_i];
|
|
1625
1547
|
}
|
|
1626
1548
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1627
|
-
var votingEscrowContract, lpTokenContract, gaugeContract, contractCalls, response,
|
|
1628
|
-
return __generator(this, function (
|
|
1629
|
-
switch (
|
|
1549
|
+
var votingEscrowContract, lpTokenContract, gaugeContract, contractCalls, response, _a, veTotalSupply, gaugeTotalSupply, votingPower, totalBalance, _c, accounts_1, acct, totalPower, optimalBN, _d, accounts_2, acct, amount, _e, accounts_3, acct, optimal, _f, _h, entry;
|
|
1550
|
+
return __generator(this, function (_j) {
|
|
1551
|
+
switch (_j.label) {
|
|
1630
1552
|
case 0:
|
|
1631
1553
|
if (accounts.length == 1 && Array.isArray(accounts[0]))
|
|
1632
1554
|
accounts = accounts[0];
|
|
@@ -1639,12 +1561,12 @@ var Pool = /** @class */ (function () {
|
|
|
1639
1561
|
});
|
|
1640
1562
|
return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
|
|
1641
1563
|
case 1:
|
|
1642
|
-
response = (
|
|
1643
|
-
|
|
1564
|
+
response = (_j.sent()).map(function (value) { return (0, utils_1.toBN)(value); });
|
|
1565
|
+
_a = response.splice(0, 2), veTotalSupply = _a[0], gaugeTotalSupply = _a[1];
|
|
1644
1566
|
votingPower = {};
|
|
1645
1567
|
totalBalance = (0, utils_1.BN)(0);
|
|
1646
|
-
for (
|
|
1647
|
-
acct = accounts_1[
|
|
1568
|
+
for (_c = 0, accounts_1 = accounts; _c < accounts_1.length; _c++) {
|
|
1569
|
+
acct = accounts_1[_c];
|
|
1648
1570
|
votingPower[acct] = response[0];
|
|
1649
1571
|
totalBalance = totalBalance.plus(response[1]).plus(response[2]);
|
|
1650
1572
|
response.splice(0, 3);
|
|
@@ -1652,8 +1574,8 @@ var Pool = /** @class */ (function () {
|
|
|
1652
1574
|
totalPower = Object.values(votingPower).reduce(function (sum, item) { return sum.plus(item); });
|
|
1653
1575
|
optimalBN = Object.fromEntries(accounts.map(function (acc) { return [acc, (0, utils_1.BN)(0)]; }));
|
|
1654
1576
|
if (totalBalance.lt(gaugeTotalSupply.times(totalPower).div(veTotalSupply))) {
|
|
1655
|
-
for (
|
|
1656
|
-
acct = accounts_2[
|
|
1577
|
+
for (_d = 0, accounts_2 = accounts; _d < accounts_2.length; _d++) {
|
|
1578
|
+
acct = accounts_2[_d];
|
|
1657
1579
|
amount = gaugeTotalSupply.times(votingPower[acct]).div(veTotalSupply).lt(totalBalance) ?
|
|
1658
1580
|
gaugeTotalSupply.times(votingPower[acct]).div(veTotalSupply) : totalBalance;
|
|
1659
1581
|
optimalBN[acct] = amount;
|
|
@@ -1665,16 +1587,16 @@ var Pool = /** @class */ (function () {
|
|
|
1665
1587
|
}
|
|
1666
1588
|
else {
|
|
1667
1589
|
if (totalPower.lt(0)) {
|
|
1668
|
-
for (
|
|
1669
|
-
acct = accounts_3[
|
|
1590
|
+
for (_e = 0, accounts_3 = accounts; _e < accounts_3.length; _e++) {
|
|
1591
|
+
acct = accounts_3[_e];
|
|
1670
1592
|
optimalBN[acct] = totalBalance.times(votingPower[acct]).div(totalPower);
|
|
1671
1593
|
}
|
|
1672
1594
|
}
|
|
1673
1595
|
optimalBN[accounts[0]] = optimalBN[accounts[0]].plus(totalBalance.minus(Object.values(optimalBN).reduce(function (sum, item) { return sum.plus(item); })));
|
|
1674
1596
|
}
|
|
1675
1597
|
optimal = {};
|
|
1676
|
-
for (
|
|
1677
|
-
entry = _h[
|
|
1598
|
+
for (_f = 0, _h = Object.entries(optimalBN); _f < _h.length; _f++) {
|
|
1599
|
+
entry = _h[_f];
|
|
1678
1600
|
optimal[entry[0]] = (0, utils_1.toStringFromBN)(entry[1]);
|
|
1679
1601
|
}
|
|
1680
1602
|
return [2 /*return*/, optimal];
|
|
@@ -1737,14 +1659,17 @@ var Pool = /** @class */ (function () {
|
|
|
1737
1659
|
throw Error('Index must be >= 0');
|
|
1738
1660
|
}
|
|
1739
1661
|
if (idx_1 > coins_N - 1) {
|
|
1740
|
-
throw Error("Index must be < "
|
|
1662
|
+
throw Error("Index must be < ".concat(coins_N));
|
|
1741
1663
|
}
|
|
1742
1664
|
return idx_1;
|
|
1743
1665
|
}
|
|
1744
|
-
var
|
|
1745
|
-
var
|
|
1666
|
+
var coinAddress = (0, utils_1._getCoinAddresses)(coin)[0];
|
|
1667
|
+
var lowerCaseCoinAddresses = useUnderlying ?
|
|
1668
|
+
_this.underlyingCoinAddresses.map(function (c) { return c.toLowerCase(); }) :
|
|
1669
|
+
_this.coinAddresses.map(function (c) { return c.toLowerCase(); });
|
|
1670
|
+
var idx = lowerCaseCoinAddresses.indexOf(coinAddress.toLowerCase());
|
|
1746
1671
|
if (idx === -1) {
|
|
1747
|
-
throw Error("There is no "
|
|
1672
|
+
throw Error("There is no ".concat(coin, " in ").concat(_this.name, " pool"));
|
|
1748
1673
|
}
|
|
1749
1674
|
return idx;
|
|
1750
1675
|
};
|
|
@@ -1789,13 +1714,13 @@ var Pool = /** @class */ (function () {
|
|
|
1789
1714
|
}); };
|
|
1790
1715
|
this._balances = function (rawCoinNames, rawCoinAddresses) {
|
|
1791
1716
|
var addresses = [];
|
|
1792
|
-
for (var
|
|
1793
|
-
addresses[
|
|
1717
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
1718
|
+
addresses[_i - 2] = arguments[_i];
|
|
1794
1719
|
}
|
|
1795
1720
|
return __awaiter(_this, void 0, void 0, function () {
|
|
1796
|
-
var coinNames, coinAddresses, i, rawBalances, balances,
|
|
1797
|
-
return __generator(this, function (
|
|
1798
|
-
switch (
|
|
1721
|
+
var coinNames, coinAddresses, i, rawBalances, balances, _a, addresses_1, address, _c, coinNames_1, coinName;
|
|
1722
|
+
return __generator(this, function (_d) {
|
|
1723
|
+
switch (_d.label) {
|
|
1799
1724
|
case 0:
|
|
1800
1725
|
coinNames = [];
|
|
1801
1726
|
coinAddresses = [];
|
|
@@ -1806,20 +1731,16 @@ var Pool = /** @class */ (function () {
|
|
|
1806
1731
|
coinAddresses.push(rawCoinAddresses[i]);
|
|
1807
1732
|
}
|
|
1808
1733
|
}
|
|
1809
|
-
if (this.name === 'tricrypto2') {
|
|
1810
|
-
coinNames.push('ETH');
|
|
1811
|
-
coinAddresses.push('0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE');
|
|
1812
|
-
}
|
|
1813
1734
|
addresses = (0, utils_1._prepareAddresses)(addresses);
|
|
1814
1735
|
return [4 /*yield*/, (0, utils_1._getBalances)(coinAddresses, addresses)];
|
|
1815
1736
|
case 1:
|
|
1816
|
-
rawBalances =
|
|
1737
|
+
rawBalances = _d.sent();
|
|
1817
1738
|
balances = {};
|
|
1818
|
-
for (
|
|
1819
|
-
address = addresses_1[
|
|
1739
|
+
for (_a = 0, addresses_1 = addresses; _a < addresses_1.length; _a++) {
|
|
1740
|
+
address = addresses_1[_a];
|
|
1820
1741
|
balances[address] = {};
|
|
1821
|
-
for (
|
|
1822
|
-
coinName = coinNames_1[
|
|
1742
|
+
for (_c = 0, coinNames_1 = coinNames; _c < coinNames_1.length; _c++) {
|
|
1743
|
+
coinName = coinNames_1[_c];
|
|
1823
1744
|
balances[address][coinName] = rawBalances[address].shift();
|
|
1824
1745
|
}
|
|
1825
1746
|
}
|
|
@@ -1912,13 +1833,13 @@ var Pool = /** @class */ (function () {
|
|
|
1912
1833
|
var poolBalancesRatios = poolBalances.map(function (b) { return b / poolBalances.reduce(function (a, b) { return a + b; }); });
|
|
1913
1834
|
// Cross factors for each wallet balance used as reference to see the
|
|
1914
1835
|
// max that can be used according to the lowest relative wallet balance
|
|
1915
|
-
var
|
|
1916
|
-
var firstCoinBalanceForEachScenario =
|
|
1836
|
+
var balancedAmountsForEachScenario = walletBalances.map(function (_, i) { return (walletBalances.map(function (_, j) { return (poolBalancesRatios[j] * walletBalances[i] / poolBalancesRatios[i]); })); });
|
|
1837
|
+
var firstCoinBalanceForEachScenario = balancedAmountsForEachScenario.map(function (_a) {
|
|
1917
1838
|
var a = _a[0];
|
|
1918
1839
|
return a;
|
|
1919
1840
|
});
|
|
1920
1841
|
var scenarioWithLowestBalances = firstCoinBalanceForEachScenario.indexOf(Math.min.apply(Math, firstCoinBalanceForEachScenario));
|
|
1921
|
-
return
|
|
1842
|
+
return balancedAmountsForEachScenario[scenarioWithLowestBalances].map(function (a, i) { return a.toFixed(decimals[i]); });
|
|
1922
1843
|
};
|
|
1923
1844
|
this._calcLpTokenAmount = function (_amounts, isDeposit) {
|
|
1924
1845
|
if (isDeposit === void 0) { isDeposit = true; }
|
|
@@ -2031,7 +1952,7 @@ var Pool = /** @class */ (function () {
|
|
|
2031
1952
|
this._addLiquidityMetaZap = function (_amounts, estimateGas) {
|
|
2032
1953
|
if (estimateGas === void 0) { estimateGas = false; }
|
|
2033
1954
|
return __awaiter(_this, void 0, void 0, function () {
|
|
2034
|
-
var _minMintAmount, ethIndex, value, contract, gas_1,
|
|
1955
|
+
var _minMintAmount, ethIndex, value, contract, gas_1, gasLimit_3, gas, gasLimit;
|
|
2035
1956
|
return __generator(this, function (_a) {
|
|
2036
1957
|
switch (_a.label) {
|
|
2037
1958
|
case 0:
|
|
@@ -2053,8 +1974,8 @@ var Pool = /** @class */ (function () {
|
|
|
2053
1974
|
if (estimateGas) {
|
|
2054
1975
|
return [2 /*return*/, gas_1.toNumber()];
|
|
2055
1976
|
}
|
|
2056
|
-
|
|
2057
|
-
return [4 /*yield*/, contract.add_liquidity(this.swap, _amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit:
|
|
1977
|
+
gasLimit_3 = gas_1.mul(130).div(100);
|
|
1978
|
+
return [4 /*yield*/, contract.add_liquidity(this.swap, _amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_3, value: value }))];
|
|
2058
1979
|
case 5: return [2 /*return*/, (_a.sent()).hash];
|
|
2059
1980
|
case 6: return [4 /*yield*/, contract.estimateGas.add_liquidity(_amounts, _minMintAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
2060
1981
|
case 7:
|
|
@@ -2113,21 +2034,21 @@ var Pool = /** @class */ (function () {
|
|
|
2113
2034
|
});
|
|
2114
2035
|
};
|
|
2115
2036
|
this._calcExpectedAmounts = function (_lpTokenAmount) { return __awaiter(_this, void 0, void 0, function () {
|
|
2116
|
-
var coinBalancesBN, i, _balance, totalSupplyBN, _a, expectedAmountsBN,
|
|
2037
|
+
var coinBalancesBN, i, _balance, totalSupplyBN, _a, expectedAmountsBN, _i, coinBalancesBN_1, coinBalance;
|
|
2117
2038
|
var _this = this;
|
|
2118
|
-
return __generator(this, function (
|
|
2119
|
-
switch (
|
|
2039
|
+
return __generator(this, function (_c) {
|
|
2040
|
+
switch (_c.label) {
|
|
2120
2041
|
case 0:
|
|
2121
2042
|
coinBalancesBN = [];
|
|
2122
2043
|
i = 0;
|
|
2123
|
-
|
|
2044
|
+
_c.label = 1;
|
|
2124
2045
|
case 1:
|
|
2125
2046
|
if (!(i < this.coinAddresses.length)) return [3 /*break*/, 4];
|
|
2126
2047
|
return [4 /*yield*/, curve_1.curve.contracts[this.swap].contract.balances(i, curve_1.curve.constantOptions)];
|
|
2127
2048
|
case 2:
|
|
2128
|
-
_balance =
|
|
2049
|
+
_balance = _c.sent();
|
|
2129
2050
|
coinBalancesBN.push((0, utils_1.toBN)(_balance, this.decimals[i]));
|
|
2130
|
-
|
|
2051
|
+
_c.label = 3;
|
|
2131
2052
|
case 3:
|
|
2132
2053
|
i++;
|
|
2133
2054
|
return [3 /*break*/, 1];
|
|
@@ -2135,10 +2056,10 @@ var Pool = /** @class */ (function () {
|
|
|
2135
2056
|
_a = utils_1.toBN;
|
|
2136
2057
|
return [4 /*yield*/, curve_1.curve.contracts[this.lpToken].contract.totalSupply(curve_1.curve.constantOptions)];
|
|
2137
2058
|
case 5:
|
|
2138
|
-
totalSupplyBN = _a.apply(void 0, [
|
|
2059
|
+
totalSupplyBN = _a.apply(void 0, [_c.sent()]);
|
|
2139
2060
|
expectedAmountsBN = [];
|
|
2140
|
-
for (
|
|
2141
|
-
coinBalance = coinBalancesBN_1[
|
|
2061
|
+
for (_i = 0, coinBalancesBN_1 = coinBalancesBN; _i < coinBalancesBN_1.length; _i++) {
|
|
2062
|
+
coinBalance = coinBalancesBN_1[_i];
|
|
2142
2063
|
expectedAmountsBN.push(coinBalance.times((0, utils_1.toBN)(_lpTokenAmount)).div(totalSupplyBN));
|
|
2143
2064
|
}
|
|
2144
2065
|
return [2 /*return*/, expectedAmountsBN.map(function (amount, i) { return (0, utils_1.fromBN)(amount, _this.decimals[i]); })];
|
|
@@ -2255,7 +2176,7 @@ var Pool = /** @class */ (function () {
|
|
|
2255
2176
|
this._removeLiquidityMetaZap = function (_lpTokenAmount, estimateGas) {
|
|
2256
2177
|
if (estimateGas === void 0) { estimateGas = false; }
|
|
2257
2178
|
return __awaiter(_this, void 0, void 0, function () {
|
|
2258
|
-
var _minAmounts, contract, gas_2,
|
|
2179
|
+
var _minAmounts, contract, gas_2, gasLimit_4, gas, gasLimit;
|
|
2259
2180
|
return __generator(this, function (_a) {
|
|
2260
2181
|
switch (_a.label) {
|
|
2261
2182
|
case 0:
|
|
@@ -2275,8 +2196,8 @@ var Pool = /** @class */ (function () {
|
|
|
2275
2196
|
if (estimateGas) {
|
|
2276
2197
|
return [2 /*return*/, gas_2.toNumber()];
|
|
2277
2198
|
}
|
|
2278
|
-
|
|
2279
|
-
return [4 /*yield*/, contract.remove_liquidity(this.swap, _lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit:
|
|
2199
|
+
gasLimit_4 = gas_2.mul(130).div(100);
|
|
2200
|
+
return [4 /*yield*/, contract.remove_liquidity(this.swap, _lpTokenAmount, _minAmounts, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_4 }))];
|
|
2280
2201
|
case 5: return [2 /*return*/, (_a.sent()).hash];
|
|
2281
2202
|
case 6: return [4 /*yield*/, contract.estimateGas.remove_liquidity(_lpTokenAmount, _minAmounts, curve_1.curve.constantOptions)];
|
|
2282
2203
|
case 7:
|
|
@@ -2379,7 +2300,7 @@ var Pool = /** @class */ (function () {
|
|
|
2379
2300
|
this._removeLiquidityImbalanceMetaZap = function (_amounts, estimateGas) {
|
|
2380
2301
|
if (estimateGas === void 0) { estimateGas = false; }
|
|
2381
2302
|
return __awaiter(_this, void 0, void 0, function () {
|
|
2382
|
-
var _maxBurnAmount, contract, gas_3,
|
|
2303
|
+
var _maxBurnAmount, contract, gas_3, gasLimit_5, gas, gasLimit;
|
|
2383
2304
|
return __generator(this, function (_a) {
|
|
2384
2305
|
switch (_a.label) {
|
|
2385
2306
|
case 0: return [4 /*yield*/, this._calcLpTokenAmountZap(_amounts, false)];
|
|
@@ -2399,8 +2320,8 @@ var Pool = /** @class */ (function () {
|
|
|
2399
2320
|
if (estimateGas) {
|
|
2400
2321
|
return [2 /*return*/, gas_3.toNumber()];
|
|
2401
2322
|
}
|
|
2402
|
-
|
|
2403
|
-
return [4 /*yield*/, contract.remove_liquidity_imbalance(this.swap, _amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit:
|
|
2323
|
+
gasLimit_5 = gas_3.mul(130).div(100);
|
|
2324
|
+
return [4 /*yield*/, contract.remove_liquidity_imbalance(this.swap, _amounts, _maxBurnAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_5 }))];
|
|
2404
2325
|
case 5: return [2 /*return*/, (_a.sent())];
|
|
2405
2326
|
case 6: return [4 /*yield*/, contract.estimateGas.remove_liquidity_imbalance(_amounts, _maxBurnAmount, curve_1.curve.constantOptions)];
|
|
2406
2327
|
case 7:
|
|
@@ -2508,38 +2429,48 @@ var Pool = /** @class */ (function () {
|
|
|
2508
2429
|
this._removeLiquidityOneCoinZap = function (_lpTokenAmount, i, estimateGas) {
|
|
2509
2430
|
if (estimateGas === void 0) { estimateGas = false; }
|
|
2510
2431
|
return __awaiter(_this, void 0, void 0, function () {
|
|
2511
|
-
var _minAmount, contract, gas_4,
|
|
2512
|
-
return __generator(this, function (
|
|
2513
|
-
switch (
|
|
2432
|
+
var _minAmount, _a, contract, gas_4, gasLimit_6, gas, gasLimit;
|
|
2433
|
+
return __generator(this, function (_c) {
|
|
2434
|
+
switch (_c.label) {
|
|
2514
2435
|
case 0:
|
|
2515
2436
|
if (!!estimateGas) return [3 /*break*/, 2];
|
|
2516
2437
|
return [4 /*yield*/, (0, utils_1._ensureAllowance)([this.lpToken], [_lpTokenAmount], this.zap)];
|
|
2517
2438
|
case 1:
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
case 2:
|
|
2439
|
+
_c.sent();
|
|
2440
|
+
_c.label = 2;
|
|
2441
|
+
case 2:
|
|
2442
|
+
if (!(this.name === 'tricrypto2')) return [3 /*break*/, 4];
|
|
2443
|
+
return [4 /*yield*/, this._calcWithdrawOneCoinSwap(_lpTokenAmount, i)];
|
|
2521
2444
|
case 3:
|
|
2522
|
-
|
|
2445
|
+
_a = _c.sent();
|
|
2446
|
+
return [3 /*break*/, 6];
|
|
2447
|
+
case 4: return [4 /*yield*/, this._calcWithdrawOneCoinZap(_lpTokenAmount, i)];
|
|
2448
|
+
case 5:
|
|
2449
|
+
_a = _c.sent();
|
|
2450
|
+
_c.label = 6;
|
|
2451
|
+
case 6:
|
|
2452
|
+
_minAmount = _a;
|
|
2453
|
+
_minAmount = _minAmount.mul(99).div(100);
|
|
2523
2454
|
contract = curve_1.curve.contracts[this.zap].contract;
|
|
2524
|
-
if (!this.isFactory) return [3 /*break*/,
|
|
2455
|
+
if (!this.isFactory) return [3 /*break*/, 9];
|
|
2525
2456
|
return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(this.swap, _lpTokenAmount, i, _minAmount, curve_1.curve.constantOptions)];
|
|
2526
|
-
case
|
|
2527
|
-
gas_4 =
|
|
2457
|
+
case 7:
|
|
2458
|
+
gas_4 = _c.sent();
|
|
2528
2459
|
if (estimateGas) {
|
|
2529
2460
|
return [2 /*return*/, gas_4.toNumber()];
|
|
2530
2461
|
}
|
|
2531
|
-
|
|
2532
|
-
return [4 /*yield*/, contract.remove_liquidity_one_coin(this.swap, _lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit:
|
|
2533
|
-
case
|
|
2534
|
-
case
|
|
2535
|
-
case
|
|
2536
|
-
gas =
|
|
2462
|
+
gasLimit_6 = gas_4.mul(130).div(100);
|
|
2463
|
+
return [4 /*yield*/, contract.remove_liquidity_one_coin(this.swap, _lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit_6 }))];
|
|
2464
|
+
case 8: return [2 /*return*/, (_c.sent()).hash];
|
|
2465
|
+
case 9: return [4 /*yield*/, contract.estimateGas.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, curve_1.curve.constantOptions)];
|
|
2466
|
+
case 10:
|
|
2467
|
+
gas = _c.sent();
|
|
2537
2468
|
if (estimateGas) {
|
|
2538
2469
|
return [2 /*return*/, gas.toNumber()];
|
|
2539
2470
|
}
|
|
2540
2471
|
gasLimit = gas.mul(130).div(100);
|
|
2541
2472
|
return [4 /*yield*/, contract.remove_liquidity_one_coin(_lpTokenAmount, i, _minAmount, __assign(__assign({}, curve_1.curve.options), { gasLimit: gasLimit }))];
|
|
2542
|
-
case
|
|
2473
|
+
case 11: return [2 /*return*/, (_c.sent()).hash];
|
|
2543
2474
|
}
|
|
2544
2475
|
});
|
|
2545
2476
|
});
|
|
@@ -2635,8 +2566,6 @@ var Pool = /** @class */ (function () {
|
|
|
2635
2566
|
removeLiquidityOneCoinWrapped: this.removeLiquidityOneCoinWrappedEstimateGas,
|
|
2636
2567
|
exchangeApprove: this.exchangeApproveEstimateGas,
|
|
2637
2568
|
exchange: this.exchangeEstimateGas,
|
|
2638
|
-
exchangeTricryptoApprove: this.exchangeTricryptoApproveEstimateGas,
|
|
2639
|
-
exchangeTricrypto: this.exchangeTricryptoEstimateGas,
|
|
2640
2569
|
exchangeWrappedApprove: this.exchangeWrappedApproveEstimateGas,
|
|
2641
2570
|
exchangeWrapped: this.exchangeWrappedEstimateGas,
|
|
2642
2571
|
};
|
|
@@ -2651,41 +2580,135 @@ var Pool = /** @class */ (function () {
|
|
|
2651
2580
|
}());
|
|
2652
2581
|
exports.Pool = Pool;
|
|
2653
2582
|
// --------- Exchange Using All Pools ---------
|
|
2654
|
-
var
|
|
2655
|
-
|
|
2583
|
+
var _estimatedGasForPoolsCache = {};
|
|
2584
|
+
var _estimateGasForPools = function (poolAddresses, inputCoinAddress, outputCoinAddress, _amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2585
|
+
var registryExchangeContract, sortedCoins, gasPromises, _i, poolAddresses_1, poolAddress, key, gasPromise, _gasAmounts_1, err_1;
|
|
2586
|
+
var _a;
|
|
2656
2587
|
return __generator(this, function (_c) {
|
|
2657
2588
|
switch (_c.label) {
|
|
2658
2589
|
case 0:
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2590
|
+
registryExchangeContract = curve_1.curve.contracts[curve_1.ALIASES.registry_exchange].contract;
|
|
2591
|
+
sortedCoins = [inputCoinAddress, outputCoinAddress].sort();
|
|
2592
|
+
gasPromises = [];
|
|
2593
|
+
for (_i = 0, poolAddresses_1 = poolAddresses; _i < poolAddresses_1.length; _i++) {
|
|
2594
|
+
poolAddress = poolAddresses_1[_i];
|
|
2595
|
+
key = "".concat(poolAddress, "-").concat(sortedCoins[0], "-").concat(sortedCoins[1]);
|
|
2596
|
+
gasPromise = void 0;
|
|
2597
|
+
if ((((_a = _estimatedGasForPoolsCache[key]) === null || _a === void 0 ? void 0 : _a.time) || 0) + 3600000 < Date.now()) {
|
|
2598
|
+
gasPromise = registryExchangeContract.estimateGas.exchange(poolAddress, inputCoinAddress, outputCoinAddress, _amount, 0, curve_1.curve.constantOptions);
|
|
2599
|
+
}
|
|
2600
|
+
else {
|
|
2601
|
+
gasPromise = Promise.resolve(_estimatedGasForPoolsCache[key].gas);
|
|
2602
|
+
}
|
|
2603
|
+
gasPromises.push(gasPromise);
|
|
2662
2604
|
}
|
|
2663
|
-
|
|
2664
|
-
return [4 /*yield*/, addressProviderContract.get_address(2, curve_1.curve.constantOptions)];
|
|
2605
|
+
_c.label = 1;
|
|
2665
2606
|
case 1:
|
|
2666
|
-
|
|
2667
|
-
|
|
2607
|
+
_c.trys.push([1, 3, , 4]);
|
|
2608
|
+
return [4 /*yield*/, Promise.all(gasPromises)];
|
|
2609
|
+
case 2:
|
|
2610
|
+
_gasAmounts_1 = _c.sent();
|
|
2611
|
+
poolAddresses.forEach(function (poolAddress, i) {
|
|
2612
|
+
var key = "".concat(poolAddress, "-").concat(sortedCoins[0], "-").concat(sortedCoins[1]);
|
|
2613
|
+
_estimatedGasForPoolsCache[key] = { 'gas': _gasAmounts_1[i], 'time': Date.now() };
|
|
2614
|
+
});
|
|
2615
|
+
return [2 /*return*/, _gasAmounts_1.map(function (_g) { return Number(ethers_1.ethers.utils.formatUnits(_g, 0)); })];
|
|
2616
|
+
case 3:
|
|
2617
|
+
err_1 = _c.sent();
|
|
2618
|
+
return [2 /*return*/, poolAddresses.map(function () { return 0; })];
|
|
2619
|
+
case 4: return [2 /*return*/];
|
|
2620
|
+
}
|
|
2621
|
+
});
|
|
2622
|
+
}); };
|
|
2623
|
+
var _getBestPoolAndOutput = function (inputCoinAddress, outputCoinAddress, amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2624
|
+
var availablePools, _a, inputCoinDecimals, outputCoinDecimals, _amount, poolAddress, registryExchangeContract, _output, registryExchangeMulticall, calls, _i, availablePools_1, pool, _c, _expectedAmounts, gasAmounts, outputCoinUsdRate, gasData, ethUsdRate, gasPrice, expectedAmounts, expectedAmountsUsd, txCostsUsd, bestPool;
|
|
2625
|
+
return __generator(this, function (_d) {
|
|
2626
|
+
switch (_d.label) {
|
|
2627
|
+
case 0:
|
|
2628
|
+
availablePools = Object.entries(abis_ethereum_1.poolsData).map(function (pool) {
|
|
2629
|
+
var _a;
|
|
2630
|
+
var coin_addresses = pool[1].coin_addresses.map(function (a) { return a.toLowerCase(); });
|
|
2631
|
+
var underlying_coin_addresses = pool[1].underlying_coin_addresses.map(function (a) { return a.toLowerCase(); });
|
|
2632
|
+
var meta_coin_addresses = (_a = pool[1].meta_coin_addresses) === null || _a === void 0 ? void 0 : _a.map(function (a) { return a.toLowerCase(); });
|
|
2633
|
+
var inputCoinIndexes = [
|
|
2634
|
+
coin_addresses.indexOf(inputCoinAddress.toLowerCase()),
|
|
2635
|
+
underlying_coin_addresses.indexOf(inputCoinAddress.toLowerCase()),
|
|
2636
|
+
meta_coin_addresses ? meta_coin_addresses.indexOf(inputCoinAddress.toLowerCase()) : -1,
|
|
2637
|
+
];
|
|
2638
|
+
var outputCoinIndexes = [
|
|
2639
|
+
coin_addresses.indexOf(outputCoinAddress.toLowerCase()),
|
|
2640
|
+
underlying_coin_addresses.indexOf(outputCoinAddress.toLowerCase()),
|
|
2641
|
+
meta_coin_addresses ? meta_coin_addresses.indexOf(outputCoinAddress.toLowerCase()) : -1,
|
|
2642
|
+
];
|
|
2643
|
+
if (inputCoinIndexes[0] >= 0 && outputCoinIndexes[0] >= 0) {
|
|
2644
|
+
return { poolAddress: pool[1].swap_address, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 };
|
|
2645
|
+
}
|
|
2646
|
+
else if (inputCoinIndexes[1] >= 0 && outputCoinIndexes[1] >= 0) {
|
|
2647
|
+
return { poolAddress: pool[1].swap_address, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 };
|
|
2648
|
+
}
|
|
2649
|
+
else if (inputCoinIndexes[0] === 0 && outputCoinIndexes[2] >= 0) {
|
|
2650
|
+
return { poolAddress: pool[1].swap_address, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 };
|
|
2651
|
+
}
|
|
2652
|
+
else if (inputCoinIndexes[2] >= 0 && outputCoinIndexes[0] === 0) {
|
|
2653
|
+
return { poolAddress: pool[1].swap_address, _output: ethers_1.ethers.BigNumber.from(0), outputUsd: 0, txCostUsd: 0 };
|
|
2654
|
+
}
|
|
2655
|
+
else {
|
|
2656
|
+
return null;
|
|
2657
|
+
}
|
|
2658
|
+
}).filter(function (pool) { return pool !== null; });
|
|
2659
|
+
_a = (0, utils_1._getCoinDecimals)(inputCoinAddress, outputCoinAddress), inputCoinDecimals = _a[0], outputCoinDecimals = _a[1];
|
|
2668
2660
|
_amount = ethers_1.ethers.utils.parseUnits(amount.toString(), inputCoinDecimals);
|
|
2669
|
-
|
|
2661
|
+
if (availablePools.length === 0) {
|
|
2662
|
+
return [2 /*return*/, { poolAddress: "0x0000000000000000000000000000000000000000", _output: ethers_1.ethers.BigNumber.from(0) }];
|
|
2663
|
+
}
|
|
2664
|
+
if (!(availablePools.length === 1)) return [3 /*break*/, 2];
|
|
2665
|
+
poolAddress = availablePools[0].poolAddress;
|
|
2666
|
+
registryExchangeContract = curve_1.curve.contracts[curve_1.ALIASES.registry_exchange].contract;
|
|
2667
|
+
return [4 /*yield*/, registryExchangeContract.get_exchange_amount(poolAddress, inputCoinAddress, outputCoinAddress, _amount, curve_1.curve.constantOptions)];
|
|
2668
|
+
case 1:
|
|
2669
|
+
_output = _d.sent();
|
|
2670
|
+
return [2 /*return*/, { poolAddress: poolAddress, _output: _output }];
|
|
2670
2671
|
case 2:
|
|
2671
|
-
|
|
2672
|
-
|
|
2672
|
+
registryExchangeMulticall = curve_1.curve.contracts[curve_1.ALIASES.registry_exchange].multicallContract;
|
|
2673
|
+
calls = [];
|
|
2674
|
+
for (_i = 0, availablePools_1 = availablePools; _i < availablePools_1.length; _i++) {
|
|
2675
|
+
pool = availablePools_1[_i];
|
|
2676
|
+
calls.push(registryExchangeMulticall.get_exchange_amount(pool.poolAddress, inputCoinAddress, outputCoinAddress, _amount));
|
|
2677
|
+
}
|
|
2678
|
+
return [4 /*yield*/, Promise.all([
|
|
2679
|
+
curve_1.curve.multicallProvider.all(calls),
|
|
2680
|
+
_estimateGasForPools(availablePools.map(function (pool) { return pool.poolAddress; }), inputCoinAddress, outputCoinAddress, _amount),
|
|
2681
|
+
(0, utils_1._getUsdRate)(outputCoinAddress),
|
|
2682
|
+
axios_1.default.get("https://api.curve.fi/api/getGas"),
|
|
2683
|
+
(0, utils_1._getUsdRate)(coins_1.COINS.eth),
|
|
2684
|
+
])];
|
|
2685
|
+
case 3:
|
|
2686
|
+
_c = _d.sent(), _expectedAmounts = _c[0], gasAmounts = _c[1], outputCoinUsdRate = _c[2], gasData = _c[3], ethUsdRate = _c[4];
|
|
2687
|
+
gasPrice = gasData.data.data.gas.standard;
|
|
2688
|
+
expectedAmounts = _expectedAmounts.map(function (_amount) { return Number(ethers_1.ethers.utils.formatUnits(_amount, outputCoinDecimals)); });
|
|
2689
|
+
expectedAmountsUsd = expectedAmounts.map(function (a) { return a * outputCoinUsdRate; });
|
|
2690
|
+
txCostsUsd = gasAmounts.map(function (a) { return ethUsdRate * a * gasPrice / 1e18; });
|
|
2691
|
+
availablePools.forEach(function (pool, i) {
|
|
2692
|
+
pool._output = _expectedAmounts[i];
|
|
2693
|
+
pool.outputUsd = expectedAmountsUsd[i];
|
|
2694
|
+
pool.txCostUsd = txCostsUsd[i];
|
|
2695
|
+
});
|
|
2696
|
+
bestPool = availablePools.reduce(function (pool1, pool2) { return (pool1.outputUsd - pool1.txCostUsd) - (pool2.outputUsd - pool2.txCostUsd) >= 0 ? pool1 : pool2; });
|
|
2697
|
+
return [2 /*return*/, { poolAddress: bestPool.poolAddress, _output: bestPool._output }];
|
|
2673
2698
|
}
|
|
2674
2699
|
});
|
|
2675
2700
|
}); };
|
|
2676
|
-
exports._getBestPoolAndOutput = _getBestPoolAndOutput;
|
|
2677
2701
|
var getBestPoolAndOutput = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2678
|
-
var _a, inputCoinAddress, outputCoinAddress,
|
|
2679
|
-
return __generator(this, function (
|
|
2680
|
-
switch (
|
|
2702
|
+
var _a, inputCoinAddress, outputCoinAddress, outputCoinDecimals, _c, poolAddress, _output;
|
|
2703
|
+
return __generator(this, function (_d) {
|
|
2704
|
+
switch (_d.label) {
|
|
2681
2705
|
case 0:
|
|
2682
2706
|
_a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
|
|
2683
|
-
|
|
2684
|
-
return [4 /*yield*/,
|
|
2707
|
+
outputCoinDecimals = (0, utils_1._getCoinDecimals)(outputCoinAddress)[0];
|
|
2708
|
+
return [4 /*yield*/, _getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, amount)];
|
|
2685
2709
|
case 1:
|
|
2686
|
-
|
|
2687
|
-
output
|
|
2688
|
-
return [2 /*return*/, { poolAddress: poolAddress, output: output }];
|
|
2710
|
+
_c = _d.sent(), poolAddress = _c.poolAddress, _output = _c._output;
|
|
2711
|
+
return [2 /*return*/, { poolAddress: poolAddress, output: ethers_1.ethers.utils.formatUnits(_output, outputCoinDecimals) }];
|
|
2689
2712
|
}
|
|
2690
2713
|
});
|
|
2691
2714
|
}); };
|
|
@@ -2699,38 +2722,57 @@ var exchangeExpected = function (inputCoin, outputCoin, amount) { return __await
|
|
|
2699
2722
|
});
|
|
2700
2723
|
}); };
|
|
2701
2724
|
exports.exchangeExpected = exchangeExpected;
|
|
2725
|
+
var exchangeIsApproved = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2726
|
+
return __generator(this, function (_a) {
|
|
2727
|
+
switch (_a.label) {
|
|
2728
|
+
case 0: return [4 /*yield*/, (0, utils_1.hasAllowance)([inputCoin], [amount], curve_1.curve.signerAddress, curve_1.ALIASES.registry_exchange)];
|
|
2729
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2730
|
+
}
|
|
2731
|
+
});
|
|
2732
|
+
}); };
|
|
2733
|
+
exports.exchangeIsApproved = exchangeIsApproved;
|
|
2734
|
+
var exchangeApproveEstimateGas = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2735
|
+
return __generator(this, function (_a) {
|
|
2736
|
+
switch (_a.label) {
|
|
2737
|
+
case 0: return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([inputCoin], [amount], curve_1.ALIASES.registry_exchange)];
|
|
2738
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2739
|
+
}
|
|
2740
|
+
});
|
|
2741
|
+
}); };
|
|
2742
|
+
exports.exchangeApproveEstimateGas = exchangeApproveEstimateGas;
|
|
2743
|
+
var exchangeApprove = function (inputCoin, outputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2744
|
+
return __generator(this, function (_a) {
|
|
2745
|
+
switch (_a.label) {
|
|
2746
|
+
case 0: return [4 /*yield*/, (0, utils_1.ensureAllowance)([inputCoin], [amount], curve_1.ALIASES.registry_exchange)];
|
|
2747
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2748
|
+
}
|
|
2749
|
+
});
|
|
2750
|
+
}); };
|
|
2751
|
+
exports.exchangeApprove = exchangeApprove;
|
|
2702
2752
|
var exchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
|
|
2703
2753
|
if (maxSlippage === void 0) { maxSlippage = 0.01; }
|
|
2704
2754
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
2705
|
-
var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals,
|
|
2755
|
+
var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals, _c, poolAddress, _output, _amount, _minRecvAmount, contract, value;
|
|
2706
2756
|
return __generator(this, function (_d) {
|
|
2707
2757
|
switch (_d.label) {
|
|
2708
2758
|
case 0:
|
|
2709
2759
|
_a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
|
|
2710
2760
|
inputCoinDecimals = (0, utils_1._getCoinDecimals)(inputCoinAddress)[0];
|
|
2711
|
-
|
|
2712
|
-
return [4 /*yield*/, addressProviderContract.get_registry()];
|
|
2761
|
+
return [4 /*yield*/, _getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, amount)];
|
|
2713
2762
|
case 1:
|
|
2714
|
-
|
|
2715
|
-
registryContract = new ethers_1.ethers.Contract(registryAddress, registry_json_1.default, curve_1.curve.signer);
|
|
2716
|
-
return [4 /*yield*/, (0, exports._getBestPoolAndOutput)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
|
|
2717
|
-
case 2:
|
|
2718
|
-
poolAddress = (_d.sent()).poolAddress;
|
|
2763
|
+
_c = _d.sent(), poolAddress = _c.poolAddress, _output = _c._output;
|
|
2719
2764
|
if (poolAddress === "0x0000000000000000000000000000000000000000") {
|
|
2720
2765
|
throw new Error("This pair can't be exchanged");
|
|
2721
2766
|
}
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
case 4: return [2 /*return*/, _d.sent()];
|
|
2732
|
-
case 5: return [4 /*yield*/, pool.estimateGas.exchangeWrapped(i, j, amount, maxSlippage)];
|
|
2733
|
-
case 6: return [2 /*return*/, _d.sent()];
|
|
2767
|
+
_amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
|
|
2768
|
+
_minRecvAmount = _output.mul((1 - maxSlippage) * 100).div(100);
|
|
2769
|
+
contract = curve_1.curve.contracts[curve_1.ALIASES.registry_exchange].contract;
|
|
2770
|
+
value = (0, utils_1.isEth)(inputCoinAddress) ? _amount : ethers_1.ethers.BigNumber.from(0);
|
|
2771
|
+
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
2772
|
+
case 2:
|
|
2773
|
+
_d.sent();
|
|
2774
|
+
return [4 /*yield*/, contract.estimateGas.exchange(poolAddress, inputCoinAddress, outputCoinAddress, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
2775
|
+
case 3: return [2 /*return*/, (_d.sent()).toNumber()];
|
|
2734
2776
|
}
|
|
2735
2777
|
});
|
|
2736
2778
|
});
|
|
@@ -2739,35 +2781,33 @@ exports.exchangeEstimateGas = exchangeEstimateGas;
|
|
|
2739
2781
|
var exchange = function (inputCoin, outputCoin, amount, maxSlippage) {
|
|
2740
2782
|
if (maxSlippage === void 0) { maxSlippage = 0.01; }
|
|
2741
2783
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
2742
|
-
var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals,
|
|
2784
|
+
var _a, inputCoinAddress, outputCoinAddress, inputCoinDecimals, _c, poolAddress, _output, _amount, _minRecvAmount, contract, value, gasLimit;
|
|
2743
2785
|
return __generator(this, function (_d) {
|
|
2744
2786
|
switch (_d.label) {
|
|
2745
2787
|
case 0:
|
|
2746
2788
|
_a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
|
|
2747
2789
|
inputCoinDecimals = (0, utils_1._getCoinDecimals)(inputCoinAddress)[0];
|
|
2748
|
-
|
|
2749
|
-
return [4 /*yield*/, addressProviderContract.get_registry()];
|
|
2790
|
+
return [4 /*yield*/, (0, utils_1.ensureAllowance)([inputCoin], [amount], curve_1.ALIASES.registry_exchange)];
|
|
2750
2791
|
case 1:
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
return [4 /*yield*/, (0, exports._getBestPoolAndOutput)(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount)];
|
|
2792
|
+
_d.sent();
|
|
2793
|
+
return [4 /*yield*/, _getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, amount)];
|
|
2754
2794
|
case 2:
|
|
2755
|
-
|
|
2795
|
+
_c = _d.sent(), poolAddress = _c.poolAddress, _output = _c._output;
|
|
2756
2796
|
if (poolAddress === "0x0000000000000000000000000000000000000000") {
|
|
2757
2797
|
throw new Error("This pair can't be exchanged");
|
|
2758
2798
|
}
|
|
2759
|
-
|
|
2760
|
-
|
|
2799
|
+
_amount = ethers_1.ethers.utils.parseUnits(amount, inputCoinDecimals);
|
|
2800
|
+
_minRecvAmount = _output.mul((1 - maxSlippage) * 100).div(100);
|
|
2801
|
+
contract = curve_1.curve.contracts[curve_1.ALIASES.registry_exchange].contract;
|
|
2802
|
+
value = (0, utils_1.isEth)(inputCoinAddress) ? _amount : ethers_1.ethers.BigNumber.from(0);
|
|
2803
|
+
return [4 /*yield*/, curve_1.curve.updateFeeData()];
|
|
2761
2804
|
case 3:
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
case 4: return [2 /*return*/, _d.sent()];
|
|
2769
|
-
case 5: return [4 /*yield*/, pool.exchangeWrapped(i, j, amount, maxSlippage)];
|
|
2770
|
-
case 6: return [2 /*return*/, _d.sent()];
|
|
2805
|
+
_d.sent();
|
|
2806
|
+
return [4 /*yield*/, contract.estimateGas.exchange(poolAddress, inputCoinAddress, outputCoinAddress, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.constantOptions), { value: value }))];
|
|
2807
|
+
case 4:
|
|
2808
|
+
gasLimit = (_d.sent()).mul(200).div(100);
|
|
2809
|
+
return [4 /*yield*/, contract.exchange(poolAddress, inputCoinAddress, outputCoinAddress, _amount, _minRecvAmount, __assign(__assign({}, curve_1.curve.options), { value: value, gasLimit: gasLimit }))];
|
|
2810
|
+
case 5: return [2 /*return*/, (_d.sent()).hash];
|
|
2771
2811
|
}
|
|
2772
2812
|
});
|
|
2773
2813
|
});
|
|
@@ -2780,6 +2820,9 @@ var crossAssetExchangeAvailable = function (inputCoin, outputCoin) { return __aw
|
|
|
2780
2820
|
switch (_c.label) {
|
|
2781
2821
|
case 0:
|
|
2782
2822
|
_a = (0, utils_1._getCoinAddresses)(inputCoin, outputCoin), inputCoinAddress = _a[0], outputCoinAddress = _a[1];
|
|
2823
|
+
// TODO remove it when fixed
|
|
2824
|
+
if (inputCoinAddress.toLowerCase() === coins_1.COINS.weth.toLowerCase() || outputCoinAddress.toLowerCase() === coins_1.COINS.weth.toLowerCase())
|
|
2825
|
+
return [2 /*return*/, false];
|
|
2783
2826
|
return [4 /*yield*/, curve_1.curve.contracts[curve_1.ALIASES.router].contract];
|
|
2784
2827
|
case 1:
|
|
2785
2828
|
routerContract = _c.sent();
|
|
@@ -2861,6 +2904,33 @@ var crossAssetExchangeExpected = function (inputCoin, outputCoin, amount) { retu
|
|
|
2861
2904
|
});
|
|
2862
2905
|
}); };
|
|
2863
2906
|
exports.crossAssetExchangeExpected = crossAssetExchangeExpected;
|
|
2907
|
+
var crossAssetExchangeIsApproved = function (inputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2908
|
+
return __generator(this, function (_a) {
|
|
2909
|
+
switch (_a.label) {
|
|
2910
|
+
case 0: return [4 /*yield*/, (0, utils_1.hasAllowance)([inputCoin], [amount], curve_1.curve.signerAddress, curve_1.ALIASES.router)];
|
|
2911
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2912
|
+
}
|
|
2913
|
+
});
|
|
2914
|
+
}); };
|
|
2915
|
+
exports.crossAssetExchangeIsApproved = crossAssetExchangeIsApproved;
|
|
2916
|
+
var crossAssetExchangeApproveEstimateGas = function (inputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2917
|
+
return __generator(this, function (_a) {
|
|
2918
|
+
switch (_a.label) {
|
|
2919
|
+
case 0: return [4 /*yield*/, (0, utils_1.ensureAllowanceEstimateGas)([inputCoin], [amount], curve_1.ALIASES.router)];
|
|
2920
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2921
|
+
}
|
|
2922
|
+
});
|
|
2923
|
+
}); };
|
|
2924
|
+
exports.crossAssetExchangeApproveEstimateGas = crossAssetExchangeApproveEstimateGas;
|
|
2925
|
+
var crossAssetExchangeApprove = function (inputCoin, amount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
2926
|
+
return __generator(this, function (_a) {
|
|
2927
|
+
switch (_a.label) {
|
|
2928
|
+
case 0: return [4 /*yield*/, (0, utils_1.ensureAllowance)([inputCoin], [amount], curve_1.ALIASES.router)];
|
|
2929
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
2930
|
+
}
|
|
2931
|
+
});
|
|
2932
|
+
}); };
|
|
2933
|
+
exports.crossAssetExchangeApprove = crossAssetExchangeApprove;
|
|
2864
2934
|
var crossAssetExchangeEstimateGas = function (inputCoin, outputCoin, amount, maxSlippage) {
|
|
2865
2935
|
if (maxSlippage === void 0) { maxSlippage = 0.02; }
|
|
2866
2936
|
return __awaiter(void 0, void 0, void 0, function () {
|
|
@@ -2876,7 +2946,7 @@ var crossAssetExchangeEstimateGas = function (inputCoin, outputCoin, amount, max
|
|
|
2876
2946
|
case 2:
|
|
2877
2947
|
inputCoinBalance = (_e.sent())[curve_1.curve.signerAddress];
|
|
2878
2948
|
if (Number(inputCoinBalance) < Number(amount)) {
|
|
2879
|
-
throw Error("Not enough "
|
|
2949
|
+
throw Error("Not enough ".concat(inputCoin, ". Actual: ").concat(inputCoinBalance, ", required: ").concat(amount));
|
|
2880
2950
|
}
|
|
2881
2951
|
return [4 /*yield*/, (0, utils_1.hasAllowance)([inputCoinAddress], [amount], curve_1.curve.signerAddress, curve_1.ALIASES.router)];
|
|
2882
2952
|
case 3:
|