@curvefi/api 1.16.0 → 1.16.1
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/pools.d.ts +4 -0
- package/lib/pools.js +197 -25
- package/package.json +1 -1
package/lib/pools.d.ts
CHANGED
|
@@ -161,7 +161,11 @@ export declare class Pool {
|
|
|
161
161
|
private _getCoinIdx;
|
|
162
162
|
private _getRates;
|
|
163
163
|
private _balances;
|
|
164
|
+
private _underlyingPrices;
|
|
165
|
+
private _wrappedPrices;
|
|
166
|
+
private _addLiquidityCryptoSlippage;
|
|
164
167
|
private _addLiquiditySlippage;
|
|
168
|
+
private _removeLiquidityCryptoSlippage;
|
|
165
169
|
private _removeLiquiditySlippage;
|
|
166
170
|
private _balancedAmounts;
|
|
167
171
|
private _calcLpTokenAmount;
|
package/lib/pools.js
CHANGED
|
@@ -382,17 +382,29 @@ var Pool = /** @class */ (function () {
|
|
|
382
382
|
});
|
|
383
383
|
}); };
|
|
384
384
|
this.addLiquiditySlippage = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
|
|
385
|
-
var totalAmount, expected,
|
|
386
|
-
return __generator(this, function (
|
|
387
|
-
switch (
|
|
385
|
+
var prices_1, totalAmountUSD, expected_1, _a, totalAmount, expected, _c;
|
|
386
|
+
return __generator(this, function (_d) {
|
|
387
|
+
switch (_d.label) {
|
|
388
388
|
case 0:
|
|
389
|
-
|
|
389
|
+
if (!this.isCrypto) return [3 /*break*/, 4];
|
|
390
|
+
return [4 /*yield*/, this._underlyingPrices()];
|
|
391
|
+
case 1:
|
|
392
|
+
prices_1 = _d.sent();
|
|
393
|
+
totalAmountUSD = amounts.reduce(function (s, a, i) { return s + (Number(a) * prices_1[i]); }, 0);
|
|
390
394
|
_a = Number;
|
|
391
395
|
return [4 /*yield*/, this.addLiquidityExpected(amounts)];
|
|
392
|
-
case
|
|
393
|
-
|
|
396
|
+
case 2:
|
|
397
|
+
expected_1 = _a.apply(void 0, [_d.sent()]);
|
|
398
|
+
return [4 /*yield*/, this._addLiquidityCryptoSlippage(totalAmountUSD, expected_1)];
|
|
399
|
+
case 3: return [2 /*return*/, _d.sent()];
|
|
400
|
+
case 4:
|
|
401
|
+
totalAmount = amounts.reduce(function (s, a) { return s + Number(a); }, 0);
|
|
402
|
+
_c = Number;
|
|
403
|
+
return [4 /*yield*/, this.addLiquidityExpected(amounts)];
|
|
404
|
+
case 5:
|
|
405
|
+
expected = _c.apply(void 0, [_d.sent()]);
|
|
394
406
|
return [4 /*yield*/, this._addLiquiditySlippage(totalAmount, expected)];
|
|
395
|
-
case
|
|
407
|
+
case 6: return [2 /*return*/, _d.sent()];
|
|
396
408
|
}
|
|
397
409
|
});
|
|
398
410
|
}); };
|
|
@@ -474,7 +486,7 @@ var Pool = /** @class */ (function () {
|
|
|
474
486
|
});
|
|
475
487
|
}); };
|
|
476
488
|
this.balancedAmounts = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
477
|
-
var poolBalances, walletBalances, _a, _c;
|
|
489
|
+
var poolBalances, walletBalances, _a, _c, prices_2, poolBalancesUSD, walletBalancesUSD, balancedAmountsUSD;
|
|
478
490
|
return __generator(this, function (_d) {
|
|
479
491
|
switch (_d.label) {
|
|
480
492
|
case 0: return [4 /*yield*/, this.getPoolBalances()];
|
|
@@ -484,7 +496,15 @@ var Pool = /** @class */ (function () {
|
|
|
484
496
|
return [4 /*yield*/, this.underlyingCoinBalances()];
|
|
485
497
|
case 2:
|
|
486
498
|
walletBalances = _c.apply(_a, [_d.sent()]).map(Number);
|
|
487
|
-
return [
|
|
499
|
+
if (!this.isCrypto) return [3 /*break*/, 4];
|
|
500
|
+
return [4 /*yield*/, this._underlyingPrices()];
|
|
501
|
+
case 3:
|
|
502
|
+
prices_2 = _d.sent();
|
|
503
|
+
poolBalancesUSD = poolBalances.map(function (b, i) { return b * prices_2[i]; });
|
|
504
|
+
walletBalancesUSD = walletBalances.map(function (b, i) { return b * prices_2[i]; });
|
|
505
|
+
balancedAmountsUSD = this._balancedAmounts(poolBalancesUSD, walletBalancesUSD, this.underlyingDecimals);
|
|
506
|
+
return [2 /*return*/, balancedAmountsUSD.map(function (b, i) { return String(Math.min(Number(b) / prices_2[i], poolBalances[i])); })];
|
|
507
|
+
case 4: return [2 /*return*/, this._balancedAmounts(poolBalances, walletBalances, this.underlyingDecimals)];
|
|
488
508
|
}
|
|
489
509
|
});
|
|
490
510
|
}); };
|
|
@@ -522,7 +542,7 @@ var Pool = /** @class */ (function () {
|
|
|
522
542
|
});
|
|
523
543
|
}); };
|
|
524
544
|
this.balancedWrappedAmounts = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
525
|
-
var poolBalances, walletBalances, _a, _c;
|
|
545
|
+
var poolBalances, walletBalances, _a, _c, prices_3, poolBalancesUSD, walletBalancesUSD, balancedAmountsUSD;
|
|
526
546
|
return __generator(this, function (_d) {
|
|
527
547
|
switch (_d.label) {
|
|
528
548
|
case 0:
|
|
@@ -536,7 +556,15 @@ var Pool = /** @class */ (function () {
|
|
|
536
556
|
return [4 /*yield*/, this.coinBalances()];
|
|
537
557
|
case 2:
|
|
538
558
|
walletBalances = _c.apply(_a, [_d.sent()]).map(Number);
|
|
539
|
-
return [
|
|
559
|
+
if (!this.isCrypto) return [3 /*break*/, 4];
|
|
560
|
+
return [4 /*yield*/, this._wrappedPrices()];
|
|
561
|
+
case 3:
|
|
562
|
+
prices_3 = _d.sent();
|
|
563
|
+
poolBalancesUSD = poolBalances.map(function (b, i) { return b * prices_3[i]; });
|
|
564
|
+
walletBalancesUSD = walletBalances.map(function (b, i) { return b * prices_3[i]; });
|
|
565
|
+
balancedAmountsUSD = this._balancedAmounts(poolBalancesUSD, walletBalancesUSD, this.decimals);
|
|
566
|
+
return [2 /*return*/, balancedAmountsUSD.map(function (b, i) { return String(Math.min(Number(b) / prices_3[i], poolBalances[i])); })];
|
|
567
|
+
case 4: return [2 /*return*/, this._balancedAmounts(poolBalances, walletBalances, this.decimals)];
|
|
540
568
|
}
|
|
541
569
|
});
|
|
542
570
|
}); };
|
|
@@ -555,20 +583,32 @@ var Pool = /** @class */ (function () {
|
|
|
555
583
|
});
|
|
556
584
|
}); };
|
|
557
585
|
this.addLiquidityWrappedSlippage = function (amounts) { return __awaiter(_this, void 0, void 0, function () {
|
|
558
|
-
var totalAmount, expected,
|
|
559
|
-
return __generator(this, function (
|
|
560
|
-
switch (
|
|
586
|
+
var prices_4, totalAmountUSD, expected_2, _a, totalAmount, expected, _c;
|
|
587
|
+
return __generator(this, function (_d) {
|
|
588
|
+
switch (_d.label) {
|
|
561
589
|
case 0:
|
|
562
590
|
if (this.isFake) {
|
|
563
591
|
throw Error("".concat(this.name, " pool doesn't have this method"));
|
|
564
592
|
}
|
|
565
|
-
|
|
593
|
+
if (!this.isCrypto) return [3 /*break*/, 4];
|
|
594
|
+
return [4 /*yield*/, this._wrappedPrices()];
|
|
595
|
+
case 1:
|
|
596
|
+
prices_4 = _d.sent();
|
|
597
|
+
totalAmountUSD = amounts.reduce(function (s, a, i) { return s + (Number(a) * prices_4[i]); }, 0);
|
|
566
598
|
_a = Number;
|
|
567
599
|
return [4 /*yield*/, this.addLiquidityWrappedExpected(amounts)];
|
|
568
|
-
case
|
|
569
|
-
|
|
600
|
+
case 2:
|
|
601
|
+
expected_2 = _a.apply(void 0, [_d.sent()]);
|
|
602
|
+
return [4 /*yield*/, this._addLiquidityCryptoSlippage(totalAmountUSD, expected_2, false)];
|
|
603
|
+
case 3: return [2 /*return*/, _d.sent()];
|
|
604
|
+
case 4:
|
|
605
|
+
totalAmount = amounts.reduce(function (s, a) { return s + Number(a); }, 0);
|
|
606
|
+
_c = Number;
|
|
607
|
+
return [4 /*yield*/, this.addLiquidityWrappedExpected(amounts)];
|
|
608
|
+
case 5:
|
|
609
|
+
expected = _c.apply(void 0, [_d.sent()]);
|
|
570
610
|
return [4 /*yield*/, this._addLiquiditySlippage(totalAmount, expected, false)];
|
|
571
|
-
case
|
|
611
|
+
case 6: return [2 /*return*/, _d.sent()];
|
|
572
612
|
}
|
|
573
613
|
});
|
|
574
614
|
}); };
|
|
@@ -1153,7 +1193,7 @@ var Pool = /** @class */ (function () {
|
|
|
1153
1193
|
});
|
|
1154
1194
|
}); };
|
|
1155
1195
|
this.removeLiquidityOneCoinSlippage = function (lpTokenAmount, coin) { return __awaiter(_this, void 0, void 0, function () {
|
|
1156
|
-
var totalAmount, _a;
|
|
1196
|
+
var totalAmount, _a, coinPrice;
|
|
1157
1197
|
return __generator(this, function (_c) {
|
|
1158
1198
|
switch (_c.label) {
|
|
1159
1199
|
case 0:
|
|
@@ -1161,8 +1201,14 @@ var Pool = /** @class */ (function () {
|
|
|
1161
1201
|
return [4 /*yield*/, this.removeLiquidityOneCoinExpected(lpTokenAmount, coin)];
|
|
1162
1202
|
case 1:
|
|
1163
1203
|
totalAmount = _a.apply(void 0, [_c.sent()]);
|
|
1164
|
-
return [
|
|
1165
|
-
|
|
1204
|
+
if (!this.isCrypto) return [3 /*break*/, 4];
|
|
1205
|
+
return [4 /*yield*/, this._underlyingPrices()];
|
|
1206
|
+
case 2:
|
|
1207
|
+
coinPrice = (_c.sent())[this._getCoinIdx(coin)];
|
|
1208
|
+
return [4 /*yield*/, this._removeLiquidityCryptoSlippage(totalAmount * coinPrice, Number(lpTokenAmount))];
|
|
1209
|
+
case 3: return [2 /*return*/, _c.sent()];
|
|
1210
|
+
case 4: return [4 /*yield*/, this._removeLiquiditySlippage(totalAmount, Number(lpTokenAmount))];
|
|
1211
|
+
case 5: return [2 /*return*/, _c.sent()];
|
|
1166
1212
|
}
|
|
1167
1213
|
});
|
|
1168
1214
|
}); };
|
|
@@ -1286,7 +1332,7 @@ var Pool = /** @class */ (function () {
|
|
|
1286
1332
|
});
|
|
1287
1333
|
}); };
|
|
1288
1334
|
this.removeLiquidityOneCoinWrappedSlippage = function (lpTokenAmount, coin) { return __awaiter(_this, void 0, void 0, function () {
|
|
1289
|
-
var totalAmount, _a;
|
|
1335
|
+
var totalAmount, _a, coinPrice;
|
|
1290
1336
|
return __generator(this, function (_c) {
|
|
1291
1337
|
switch (_c.label) {
|
|
1292
1338
|
case 0:
|
|
@@ -1297,8 +1343,14 @@ var Pool = /** @class */ (function () {
|
|
|
1297
1343
|
return [4 /*yield*/, this.removeLiquidityOneCoinWrappedExpected(lpTokenAmount, coin)];
|
|
1298
1344
|
case 1:
|
|
1299
1345
|
totalAmount = _a.apply(void 0, [_c.sent()]);
|
|
1300
|
-
return [
|
|
1301
|
-
|
|
1346
|
+
if (!this.isCrypto) return [3 /*break*/, 4];
|
|
1347
|
+
return [4 /*yield*/, this._underlyingPrices()];
|
|
1348
|
+
case 2:
|
|
1349
|
+
coinPrice = (_c.sent())[this._getCoinIdx(coin, false)];
|
|
1350
|
+
return [4 /*yield*/, this._removeLiquidityCryptoSlippage(totalAmount * coinPrice, Number(lpTokenAmount))];
|
|
1351
|
+
case 3: return [2 /*return*/, _c.sent()];
|
|
1352
|
+
case 4: return [4 /*yield*/, this._removeLiquiditySlippage(totalAmount, Number(lpTokenAmount), false)];
|
|
1353
|
+
case 5: return [2 /*return*/, _c.sent()];
|
|
1302
1354
|
}
|
|
1303
1355
|
});
|
|
1304
1356
|
}); };
|
|
@@ -2121,6 +2173,90 @@ var Pool = /** @class */ (function () {
|
|
|
2121
2173
|
});
|
|
2122
2174
|
});
|
|
2123
2175
|
};
|
|
2176
|
+
this._underlyingPrices = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
2177
|
+
var promises, _i, _a, addr;
|
|
2178
|
+
return __generator(this, function (_c) {
|
|
2179
|
+
switch (_c.label) {
|
|
2180
|
+
case 0:
|
|
2181
|
+
promises = [];
|
|
2182
|
+
for (_i = 0, _a = this.underlyingCoinAddresses; _i < _a.length; _i++) {
|
|
2183
|
+
addr = _a[_i];
|
|
2184
|
+
promises.push((0, utils_1._getUsdRate)(addr));
|
|
2185
|
+
}
|
|
2186
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
2187
|
+
case 1: return [2 /*return*/, _c.sent()];
|
|
2188
|
+
}
|
|
2189
|
+
});
|
|
2190
|
+
}); };
|
|
2191
|
+
// NOTE! It may crash!
|
|
2192
|
+
this._wrappedPrices = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
2193
|
+
var promises, _i, _a, addr;
|
|
2194
|
+
return __generator(this, function (_c) {
|
|
2195
|
+
switch (_c.label) {
|
|
2196
|
+
case 0:
|
|
2197
|
+
promises = [];
|
|
2198
|
+
for (_i = 0, _a = this.coinAddresses; _i < _a.length; _i++) {
|
|
2199
|
+
addr = _a[_i];
|
|
2200
|
+
promises.push((0, utils_1._getUsdRate)(addr));
|
|
2201
|
+
}
|
|
2202
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
2203
|
+
case 1: return [2 /*return*/, _c.sent()];
|
|
2204
|
+
}
|
|
2205
|
+
});
|
|
2206
|
+
}); };
|
|
2207
|
+
this._addLiquidityCryptoSlippage = function (totalAmountUSD, expected, useUnderlying) {
|
|
2208
|
+
if (useUnderlying === void 0) { useUnderlying = true; }
|
|
2209
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
2210
|
+
var poolBalances, _a, prices, _c, poolBalancesUSD, poolTotalBalance, poolBalancesRatios, balancedAmountsUSD, balancedAmounts, balancedExpected, _d, _e, _f;
|
|
2211
|
+
return __generator(this, function (_h) {
|
|
2212
|
+
switch (_h.label) {
|
|
2213
|
+
case 0:
|
|
2214
|
+
if (!useUnderlying) return [3 /*break*/, 2];
|
|
2215
|
+
return [4 /*yield*/, this.getPoolBalances()];
|
|
2216
|
+
case 1:
|
|
2217
|
+
_a = (_h.sent()).map(Number);
|
|
2218
|
+
return [3 /*break*/, 4];
|
|
2219
|
+
case 2: return [4 /*yield*/, this.getPoolWrappedBalances()];
|
|
2220
|
+
case 3:
|
|
2221
|
+
_a = (_h.sent()).map(Number);
|
|
2222
|
+
_h.label = 4;
|
|
2223
|
+
case 4:
|
|
2224
|
+
poolBalances = _a;
|
|
2225
|
+
if (!useUnderlying) return [3 /*break*/, 6];
|
|
2226
|
+
return [4 /*yield*/, this._underlyingPrices()];
|
|
2227
|
+
case 5:
|
|
2228
|
+
_c = _h.sent();
|
|
2229
|
+
return [3 /*break*/, 8];
|
|
2230
|
+
case 6: return [4 /*yield*/, this._wrappedPrices()];
|
|
2231
|
+
case 7:
|
|
2232
|
+
_c = _h.sent();
|
|
2233
|
+
_h.label = 8;
|
|
2234
|
+
case 8:
|
|
2235
|
+
prices = _c;
|
|
2236
|
+
poolBalancesUSD = poolBalances.map(function (b, i) { return Number(b) * prices[i]; });
|
|
2237
|
+
poolTotalBalance = poolBalancesUSD.reduce(function (a, b) { return a + b; });
|
|
2238
|
+
poolBalancesRatios = poolBalancesUSD.map(function (b) { return b / poolTotalBalance; });
|
|
2239
|
+
balancedAmountsUSD = poolBalancesRatios.map(function (r) { return r * totalAmountUSD; });
|
|
2240
|
+
balancedAmounts = balancedAmountsUSD.map(function (a, i) { return String(a / prices[i]); });
|
|
2241
|
+
if (!useUnderlying) return [3 /*break*/, 10];
|
|
2242
|
+
_e = Number;
|
|
2243
|
+
return [4 /*yield*/, this.addLiquidityExpected(balancedAmounts)];
|
|
2244
|
+
case 9:
|
|
2245
|
+
_d = _e.apply(void 0, [_h.sent()]);
|
|
2246
|
+
return [3 /*break*/, 12];
|
|
2247
|
+
case 10:
|
|
2248
|
+
_f = Number;
|
|
2249
|
+
return [4 /*yield*/, this.addLiquidityWrappedExpected(balancedAmounts)];
|
|
2250
|
+
case 11:
|
|
2251
|
+
_d = _f.apply(void 0, [_h.sent()]);
|
|
2252
|
+
_h.label = 12;
|
|
2253
|
+
case 12:
|
|
2254
|
+
balancedExpected = _d;
|
|
2255
|
+
return [2 /*return*/, String((balancedExpected - expected) / balancedExpected)];
|
|
2256
|
+
}
|
|
2257
|
+
});
|
|
2258
|
+
});
|
|
2259
|
+
};
|
|
2124
2260
|
this._addLiquiditySlippage = function (totalAmount, expected, useUnderlying) {
|
|
2125
2261
|
if (useUnderlying === void 0) { useUnderlying = true; }
|
|
2126
2262
|
return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -2161,6 +2297,41 @@ var Pool = /** @class */ (function () {
|
|
|
2161
2297
|
});
|
|
2162
2298
|
});
|
|
2163
2299
|
};
|
|
2300
|
+
this._removeLiquidityCryptoSlippage = function (totalAmountUSD, lpTokenAmount, useUnderlying) {
|
|
2301
|
+
if (useUnderlying === void 0) { useUnderlying = true; }
|
|
2302
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
2303
|
+
var prices, _a, balancedAmounts, _c, balancedTotalAmountsUSD;
|
|
2304
|
+
return __generator(this, function (_d) {
|
|
2305
|
+
switch (_d.label) {
|
|
2306
|
+
case 0:
|
|
2307
|
+
if (!useUnderlying) return [3 /*break*/, 2];
|
|
2308
|
+
return [4 /*yield*/, this._underlyingPrices()];
|
|
2309
|
+
case 1:
|
|
2310
|
+
_a = _d.sent();
|
|
2311
|
+
return [3 /*break*/, 4];
|
|
2312
|
+
case 2: return [4 /*yield*/, this._wrappedPrices()];
|
|
2313
|
+
case 3:
|
|
2314
|
+
_a = _d.sent();
|
|
2315
|
+
_d.label = 4;
|
|
2316
|
+
case 4:
|
|
2317
|
+
prices = _a;
|
|
2318
|
+
if (!useUnderlying) return [3 /*break*/, 6];
|
|
2319
|
+
return [4 /*yield*/, this.removeLiquidityExpected(String(lpTokenAmount))];
|
|
2320
|
+
case 5:
|
|
2321
|
+
_c = _d.sent();
|
|
2322
|
+
return [3 /*break*/, 8];
|
|
2323
|
+
case 6: return [4 /*yield*/, this.removeLiquidityWrappedExpected(String(lpTokenAmount))];
|
|
2324
|
+
case 7:
|
|
2325
|
+
_c = _d.sent();
|
|
2326
|
+
_d.label = 8;
|
|
2327
|
+
case 8:
|
|
2328
|
+
balancedAmounts = _c;
|
|
2329
|
+
balancedTotalAmountsUSD = balancedAmounts.reduce(function (s, b, i) { return s + (Number(b) * prices[i]); }, 0);
|
|
2330
|
+
return [2 /*return*/, String((balancedTotalAmountsUSD - totalAmountUSD) / balancedTotalAmountsUSD)];
|
|
2331
|
+
}
|
|
2332
|
+
});
|
|
2333
|
+
});
|
|
2334
|
+
};
|
|
2164
2335
|
this._removeLiquiditySlippage = function (totalAmount, expected, useUnderlying) {
|
|
2165
2336
|
if (useUnderlying === void 0) { useUnderlying = true; }
|
|
2166
2337
|
return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -2202,7 +2373,8 @@ var Pool = /** @class */ (function () {
|
|
|
2202
2373
|
});
|
|
2203
2374
|
};
|
|
2204
2375
|
this._balancedAmounts = function (poolBalances, walletBalances, decimals) {
|
|
2205
|
-
var
|
|
2376
|
+
var poolTotalLiquidity = poolBalances.reduce(function (a, b) { return a + b; });
|
|
2377
|
+
var poolBalancesRatios = poolBalances.map(function (b) { return b / poolTotalLiquidity; });
|
|
2206
2378
|
// Cross factors for each wallet balance used as reference to see the
|
|
2207
2379
|
// max that can be used according to the lowest relative wallet balance
|
|
2208
2380
|
var balancedAmountsForEachScenario = walletBalances.map(function (_, i) { return (walletBalances.map(function (_, j) { return (poolBalancesRatios[j] * walletBalances[i] / poolBalancesRatios[i]); })); });
|