@curvefi/api 2.15.0 → 2.15.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.
@@ -99,6 +99,7 @@ export declare class PoolTemplate {
99
99
  private _calcLpTokenAmount;
100
100
  private calcLpTokenAmount;
101
101
  private calcLpTokenAmountWrapped;
102
+ cryptoSeedAmounts(amount1: number | string): Promise<string[]>;
102
103
  depositBalancedAmounts(): Promise<string[]>;
103
104
  depositExpected(amounts: (number | string)[]): Promise<string>;
104
105
  depositBonus(amounts: (number | string)[]): Promise<string>;
@@ -365,7 +365,7 @@ var PoolTemplate = /** @class */ (function () {
365
365
  if (isDeposit === void 0) { isDeposit = true; }
366
366
  if (useUnderlying === void 0) { useUnderlying = true; }
367
367
  return __awaiter(_this, void 0, void 0, function () {
368
- var _rates_1, N_coins, decimals, calcContractAddress, calcContract, poolContract, lpContract, calls, res, _c, _totalSupply, _fee, _lpTokenAmount, balances, _d, totalSupplyBN, feeBN, lpTokenAmountBN, balancesBN, amountsBN, feesBN, i, _fees, _lpTokenFee;
368
+ var _rates_1, e_1, lpContract, _lpTotalSupply, decimals_1, amounts, seedAmounts_1, N_coins, decimals_2, calcContractAddress, calcContract, poolContract, lpContract, calls, res, _c, _totalSupply, _fee, _lpTokenAmount, balances, _d, totalSupplyBN, feeBN, lpTokenAmountBN, balancesBN, amountsBN, feesBN, i, _fees, _lpTokenFee, e_2, lpContract, _lpTotalSupply, decimals_3, amounts_1, _amounts18Decimals;
369
369
  return __generator(this, function (_e) {
370
370
  switch (_e.label) {
371
371
  case 0:
@@ -378,12 +378,36 @@ var PoolTemplate = /** @class */ (function () {
378
378
  });
379
379
  _e.label = 2;
380
380
  case 2:
381
- if (!this.isCrypto) return [3 /*break*/, 4];
381
+ if (!this.isCrypto) return [3 /*break*/, 8];
382
+ _e.label = 3;
383
+ case 3:
384
+ _e.trys.push([3, 5, , 8]);
382
385
  return [4 /*yield*/, this._pureCalcLpTokenAmount(_amounts, isDeposit, useUnderlying)];
383
- case 3: return [2 /*return*/, _e.sent()];
384
- case 4:
386
+ case 4: return [2 /*return*/, _e.sent()];
387
+ case 5:
388
+ e_1 = _e.sent();
389
+ lpContract = curve_1.curve.contracts[this.lpToken].contract;
390
+ return [4 /*yield*/, lpContract.totalSupply(curve_1.curve.constantOptions)];
391
+ case 6:
392
+ _lpTotalSupply = _e.sent();
393
+ if (_lpTotalSupply.gt(0))
394
+ throw e_1; // Already seeded
395
+ if (this.isMeta && useUnderlying)
396
+ throw Error("Initial deposit for crypto meta pools must be in wrapped coins");
397
+ decimals_1 = useUnderlying ? this.underlyingDecimals : this.wrappedDecimals;
398
+ amounts = _amounts.map(function (_a, i) { return ethers_1.ethers.utils.formatUnits(_a, decimals_1[i]); });
399
+ return [4 /*yield*/, this.cryptoSeedAmounts(amounts[0])];
400
+ case 7:
401
+ seedAmounts_1 = _e.sent();
402
+ amounts.forEach(function (a, i) {
403
+ if (!(0, utils_1.BN)(a).eq((0, utils_1.BN)(seedAmounts_1[i])))
404
+ throw Error("Amounts must be = ".concat(seedAmounts_1));
405
+ });
406
+ return [2 /*return*/, (0, utils_1.parseUnits)(Math.sqrt(Number(amounts[0]) * Number(amounts[1])))];
407
+ case 8:
408
+ _e.trys.push([8, 11, , 13]);
385
409
  N_coins = useUnderlying ? this.underlyingCoins.length : this.wrappedCoins.length;
386
- decimals = useUnderlying ? this.underlyingDecimals : this.wrappedDecimals;
410
+ decimals_2 = useUnderlying ? this.underlyingDecimals : this.wrappedDecimals;
387
411
  calcContractAddress = this.isMeta && useUnderlying ? this.zap : this.address;
388
412
  calcContract = curve_1.curve.contracts[calcContractAddress].multicallContract;
389
413
  poolContract = curve_1.curve.contracts[this.address].multicallContract;
@@ -403,13 +427,13 @@ var PoolTemplate = /** @class */ (function () {
403
427
  curve_1.curve.multicallProvider.all(calls),
404
428
  useUnderlying ? this.stats.underlyingBalances() : this.stats.wrappedBalances(),
405
429
  ])];
406
- case 5:
430
+ case 9:
407
431
  res = _e.sent();
408
432
  _c = res[0], _totalSupply = _c[0], _fee = _c[1], _lpTokenAmount = _c[2];
409
433
  balances = res[1];
410
434
  _d = [(0, utils_1.toBN)(_totalSupply), (0, utils_1.toBN)(_fee, 10).times(N_coins).div(4 * (N_coins - 1)), (0, utils_1.toBN)(_lpTokenAmount)], totalSupplyBN = _d[0], feeBN = _d[1], lpTokenAmountBN = _d[2];
411
435
  balancesBN = balances.map(function (b) { return (0, utils_1.BN)(b); });
412
- amountsBN = _amounts.map(function (_a, i) { return (0, utils_1.toBN)(_a, decimals[i]); });
436
+ amountsBN = _amounts.map(function (_a, i) { return (0, utils_1.toBN)(_a, decimals_2[i]); });
413
437
  feesBN = Array(N_coins).fill((0, utils_1.BN)(0));
414
438
  if (totalSupplyBN.gt(0)) {
415
439
  for (i = 0; i < N_coins; i++) {
@@ -418,13 +442,34 @@ var PoolTemplate = /** @class */ (function () {
418
442
  feesBN[i] = feesBN[i].times(-1);
419
443
  }
420
444
  }
421
- _fees = feesBN.map(function (fBN, i) { return (0, utils_1.fromBN)(fBN, decimals[i]); });
445
+ _fees = feesBN.map(function (fBN, i) { return (0, utils_1.fromBN)(fBN, decimals_2[i]); });
422
446
  return [4 /*yield*/, this._pureCalcLpTokenAmount(_fees, !isDeposit, useUnderlying)];
423
- case 6:
447
+ case 10:
424
448
  _lpTokenFee = _e.sent();
425
449
  if (isDeposit)
426
450
  _lpTokenFee = _lpTokenFee.mul(-1);
427
451
  return [2 /*return*/, _lpTokenAmount.add(_lpTokenFee)];
452
+ case 11:
453
+ e_2 = _e.sent();
454
+ if (!isDeposit)
455
+ throw e_2; // Seeding is only for deposit
456
+ lpContract = curve_1.curve.contracts[this.lpToken].contract;
457
+ return [4 /*yield*/, lpContract.totalSupply(curve_1.curve.constantOptions)];
458
+ case 12:
459
+ _lpTotalSupply = _e.sent();
460
+ if (_lpTotalSupply.gt(0))
461
+ throw e_2; // Already seeded
462
+ decimals_3 = useUnderlying ? this.underlyingDecimals : this.wrappedDecimals;
463
+ amounts_1 = _amounts.map(function (_a, i) { return ethers_1.ethers.utils.formatUnits(_a, decimals_3[i]); });
464
+ amounts_1.forEach(function (a) {
465
+ if (a !== amounts_1[0])
466
+ throw Error("Initial deposit amounts must be the same");
467
+ });
468
+ if (_amounts[0].lte(0))
469
+ throw Error("Initial deposit amounts must be >0");
470
+ _amounts18Decimals = amounts_1.map(function (a) { return (0, utils_1.parseUnits)(a); });
471
+ return [2 /*return*/, _amounts18Decimals.reduce(function (_a, _b) { return _a.add(_b); })];
472
+ case 13: return [2 /*return*/];
428
473
  }
429
474
  });
430
475
  });
@@ -877,7 +922,7 @@ var PoolTemplate = /** @class */ (function () {
877
922
  _this.wrappedCoinAddresses.map(function (c) { return c.toLowerCase(); });
878
923
  var idx = lowerCaseCoinAddresses.indexOf(coinAddress.toLowerCase());
879
924
  if (idx === -1) {
880
- throw Error("There is no ".concat(coin, " in ").concat(_this.name, " pool")); // TODO add wrapped or underlying
925
+ throw Error("There is no ".concat(coin, " among ").concat(_this.name, " pool ").concat(useUnderlying ? 'underlying' : 'wrapped', " coins"));
881
926
  }
882
927
  return idx;
883
928
  };
@@ -1168,6 +1213,29 @@ var PoolTemplate = /** @class */ (function () {
1168
1213
  });
1169
1214
  };
1170
1215
  // ---------------- DEPOSIT ----------------
1216
+ PoolTemplate.prototype.cryptoSeedAmounts = function (amount1) {
1217
+ return __awaiter(this, void 0, void 0, function () {
1218
+ var decimals, amount1BN, priceScaleBN, _c;
1219
+ return __generator(this, function (_d) {
1220
+ switch (_d.label) {
1221
+ case 0:
1222
+ if (!this.isCrypto)
1223
+ throw Error("cryptoSeedAmounts method doesn't exist for stable pools");
1224
+ decimals = this.isMeta ? this.wrappedDecimals : this.underlyingDecimals;
1225
+ if (decimals.length > 2)
1226
+ throw Error("cryptoSeedAmounts method doesn't exist for pools with N coins > 2");
1227
+ amount1BN = (0, utils_1.BN)(amount1);
1228
+ if (amount1BN.lte(0))
1229
+ throw Error("Initial deposit amounts must be > 0");
1230
+ _c = utils_1.toBN;
1231
+ return [4 /*yield*/, curve_1.curve.contracts[this.address].contract.price_scale(curve_1.curve.constantOptions)];
1232
+ case 1:
1233
+ priceScaleBN = _c.apply(void 0, [_d.sent()]);
1234
+ return [2 /*return*/, [amount1BN.toFixed(decimals[0]), amount1BN.div(priceScaleBN).toFixed(decimals[1])]];
1235
+ }
1236
+ });
1237
+ });
1238
+ };
1171
1239
  PoolTemplate.prototype.depositBalancedAmounts = function () {
1172
1240
  return __awaiter(this, void 0, void 0, function () {
1173
1241
  return __generator(this, function (_c) {
@@ -141,7 +141,7 @@ exports.withdrawOneCoinWrappedBonusMixin = {
141
141
  return [4 /*yield*/, this.withdrawOneCoinWrappedExpected(lpTokenAmount, coin)];
142
142
  case 3:
143
143
  coinAmount = _d.apply(void 0, [_e.sent()]);
144
- totalValue = coinAmount * prices[this._getCoinIdx(coin)];
144
+ totalValue = coinAmount * prices[this._getCoinIdx(coin, false)];
145
145
  return [4 /*yield*/, this.withdrawWrappedExpected(lpTokenAmount)];
146
146
  case 4:
147
147
  balancedAmounts = _e.sent();
@@ -198,7 +198,7 @@ exports.withdrawOneCoinWrappedCryptoBonusMixin = {
198
198
  case 2: return [4 /*yield*/, this._wrappedPrices()];
199
199
  case 3:
200
200
  prices = (_e.sent()).map(function (p, i, arr) { return i === arr.length - 1 ? p * vp : p; });
201
- coinPrice = prices[this._getCoinIdx(coin)];
201
+ coinPrice = prices[this._getCoinIdx(coin, false)];
202
202
  _d = Number;
203
203
  return [4 /*yield*/, this.withdrawOneCoinWrappedExpected(lpTokenAmount, coin)];
204
204
  case 4:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "2.15.0",
3
+ "version": "2.15.1",
4
4
  "description": "JavaScript library for curve.fi",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",