@curvefi/api 2.1.0 → 2.3.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.
Files changed (38) hide show
  1. package/README.md +2 -0
  2. package/lib/constants/abis/dusd/sCurveRewards.json +472 -1
  3. package/lib/constants/abis/gauge_child.json +1039 -0
  4. package/lib/constants/abis/{gauge_rewards_only.json → minter_child.json} +159 -312
  5. package/lib/constants/abis/musd/sCurveRewards.json +482 -1
  6. package/lib/constants/abis/rsv/sCurveRewards.json +472 -1
  7. package/lib/constants/abis/sbtc/sCurveRewards.json +566 -1
  8. package/lib/constants/abis/susdv2/sCurveRewards.json +457 -1
  9. package/lib/constants/abis/tbtc/sCurveRewards.json +472 -1
  10. package/lib/constants/aliases.d.ts +1 -0
  11. package/lib/constants/aliases.js +14 -2
  12. package/lib/constants/coins/avalanche.d.ts +7 -0
  13. package/lib/constants/coins/avalanche.js +33 -0
  14. package/lib/constants/pools/avalanche.d.ts +4 -0
  15. package/lib/constants/pools/avalanche.js +106 -0
  16. package/lib/constants/pools/ethereum.js +0 -33
  17. package/lib/constants/pools/index.d.ts +2 -1
  18. package/lib/constants/pools/index.js +3 -1
  19. package/lib/constants/pools/polygon.js +19 -24
  20. package/lib/constants/utils.js +0 -9
  21. package/lib/curve.d.ts +5 -1
  22. package/lib/curve.js +47 -29
  23. package/lib/external-api.d.ts +3 -3
  24. package/lib/factory/common.d.ts +2 -0
  25. package/lib/factory/common.js +45 -0
  26. package/lib/factory/constants.d.ts +11 -11
  27. package/lib/factory/constants.js +60 -58
  28. package/lib/factory/factory-api.js +20 -46
  29. package/lib/factory/factory-crypto.js +7 -4
  30. package/lib/factory/factory.js +31 -163
  31. package/lib/interfaces.d.ts +37 -29
  32. package/lib/pools/PoolTemplate.d.ts +11 -1
  33. package/lib/pools/PoolTemplate.js +175 -104
  34. package/lib/pools/poolConstructor.js +3 -3
  35. package/lib/router.js +10 -10
  36. package/lib/utils.d.ts +2 -0
  37. package/lib/utils.js +36 -14
  38. package/package.json +1 -1
package/lib/router.js CHANGED
@@ -173,7 +173,7 @@ var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __a
173
173
  // No input coin in this pool --> skip
174
174
  if (inCoinIndexes.wrapped_coin === -1 && inCoinIndexes.underlying_coin === -1 && inCoinIndexes.meta_coin === -1)
175
175
  return [3 /*break*/, 17];
176
- if (!(inCoinIndexes.wrapped_coin >= 0 && poolId !== "atricrypto3")) return [3 /*break*/, 7];
176
+ if (!(inCoinIndexes.wrapped_coin >= 0 && !poolData.is_fake)) return [3 /*break*/, 7];
177
177
  j = 0;
178
178
  _5.label = 4;
179
179
  case 4:
@@ -214,14 +214,14 @@ var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __a
214
214
  j++;
215
215
  return [3 /*break*/, 4];
216
216
  case 7:
217
- poolAddress = ["eurtusd", "xautusd", "atricrypto3"].includes(poolId) ||
218
- (curve_1.curve.chainId === 137 && poolData.is_factory) ? poolData.deposit_address : poolData.swap_address;
217
+ poolAddress = (poolData.is_crypto && poolData.is_meta) || ([137, 43114].includes(curve_1.curve.chainId) && poolData.is_factory) ?
218
+ poolData.deposit_address : poolData.swap_address;
219
219
  if (!(wrapped_coin_addresses.join("|") !== underlying_coin_addresses.join("|") && inCoinIndexes.underlying_coin >= 0)) return [3 /*break*/, 11];
220
220
  j = 0;
221
221
  _5.label = 8;
222
222
  case 8:
223
223
  if (!(j < underlying_coin_addresses.length)) return [3 /*break*/, 11];
224
- if (poolId === "atricrypto3" && inCoinIndexes.meta_coin >= 0 && meta_coin_addresses.includes(underlying_coin_addresses[j]))
224
+ if (poolData.is_fake && inCoinIndexes.meta_coin >= 0 && meta_coin_addresses.includes(underlying_coin_addresses[j]))
225
225
  return [3 /*break*/, 10];
226
226
  // If this coin already marked or will be marked on the current step, no need to consider it on the next step
227
227
  if (markedCoins.includes(underlying_coin_addresses[j]) || curCoins.includes(underlying_coin_addresses[j]))
@@ -259,7 +259,7 @@ var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __a
259
259
  j++;
260
260
  return [3 /*break*/, 8];
261
261
  case 11:
262
- if (!(inCoinIndexes.wrapped_coin === 0 && meta_coin_addresses.length > 0 && poolId !== "atricrypto3")) return [3 /*break*/, 15];
262
+ if (!(inCoinIndexes.wrapped_coin === 0 && meta_coin_addresses.length > 0 && !poolData.is_fake)) return [3 /*break*/, 15];
263
263
  j = 0;
264
264
  _5.label = 12;
265
265
  case 12:
@@ -279,7 +279,7 @@ var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __a
279
279
  // Skip imbalanced pools
280
280
  if (IMBALANCED_POOLS.includes(poolId))
281
281
  return [3 /*break*/, 14];
282
- swapType = (curve_1.curve.chainId === 137 && poolData.is_factory) ? 5 : poolData.is_crypto ? 4 : 2;
282
+ swapType = ([137, 43114].includes(curve_1.curve.chainId) && poolData.is_factory) ? 5 : poolData.is_crypto ? 4 : 2;
283
283
  for (_r = 0, _s = routes[inCoin]; _r < _s.length; _r++) {
284
284
  inCoinRoute = _s[_r];
285
285
  routes[meta_coin_addresses[j]] = ((_2 = routes[meta_coin_addresses[j]]) !== null && _2 !== void 0 ? _2 : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
@@ -300,7 +300,7 @@ var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __a
300
300
  j++;
301
301
  return [3 /*break*/, 12];
302
302
  case 15:
303
- if (!(inCoinIndexes.meta_coin >= 0 && poolId !== "atricrypto3")) return [3 /*break*/, 17];
303
+ if (!(inCoinIndexes.meta_coin >= 0 && !poolData.is_fake)) return [3 /*break*/, 17];
304
304
  // If this coin already marked or will be marked on the current step, no need to consider it on the next step
305
305
  if (markedCoins.includes(wrapped_coin_addresses[0]) || curCoins.includes(wrapped_coin_addresses[0]))
306
306
  return [3 /*break*/, 17];
@@ -316,7 +316,7 @@ var _findAllRoutes = function (inputCoinAddress, outputCoinAddress) { return __a
316
316
  // Skip imbalanced pools
317
317
  if (IMBALANCED_POOLS.includes(poolId))
318
318
  return [3 /*break*/, 17];
319
- swapType = (curve_1.curve.chainId === 137 && poolData.is_factory) ? 5 : poolData.is_crypto ? 4 : 2;
319
+ swapType = ([137, 43114].includes(curve_1.curve.chainId) && poolData.is_factory) ? 5 : poolData.is_crypto ? 4 : 2;
320
320
  for (_u = 0, _v = routes[inCoin]; _u < _v.length; _u++) {
321
321
  inCoinRoute = _v[_u];
322
322
  routes[wrapped_coin_addresses[0]] = ((_3 = routes[wrapped_coin_addresses[0]]) !== null && _3 !== void 0 ? _3 : []).concat([__spreadArray(__spreadArray([], inCoinRoute, true), [
@@ -488,7 +488,7 @@ var _getBestRouteAndOutput = (0, memoizee_1.default)(function (inputCoinAddress,
488
488
  _estimateGasForDifferentRoutes(routes, inputCoinAddress, outputCoinAddress, _amount),
489
489
  (0, utils_1._getUsdRate)(outputCoinAddress),
490
490
  axios_1.default.get("https://api.curve.fi/api/getGas"),
491
- (0, utils_1._getUsdRate)(curve_1.curve.chainId === 137 ? curve_1.curve.constants.COINS.matic : curve_1.curve.constants.COINS.eth),
491
+ (0, utils_1._getUsdRate)(utils_1.ETH_ADDRESS),
492
492
  ])];
493
493
  case 7:
494
494
  _e = _f.sent(), gasAmounts = _e[0], outputCoinUsdRate = _e[1], gasData = _e[2], ethUsdRate = _e[3];
@@ -541,7 +541,7 @@ var swapPriceImpact = function (inputCoin, outputCoin, amount) { return __awaite
541
541
  return [4 /*yield*/, _getBestRouteAndOutput(inputCoinAddress, outputCoinAddress, amount)];
542
542
  case 1:
543
543
  route = _d.sent();
544
- target = (0, utils_1.BN)(Math.pow(10, 12));
544
+ target = (0, utils_1.BN)(Math.pow(10, 15));
545
545
  amountIntBN = (0, utils_1.BN)(amount).times(Math.pow(10, inputCoinDecimals));
546
546
  outputIntBN = (0, utils_1.toBN)(route._output, 0);
547
547
  k = bignumber_js_1.default.min(bignumber_js_1.default.max(target.div(amountIntBN), target.div(outputIntBN)), 0.2);
package/lib/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ethers } from 'ethers';
2
2
  import BigNumber from 'bignumber.js';
3
3
  import { IDict } from './interfaces';
4
+ export declare const ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
4
5
  export declare const MAX_ALLOWANCE: ethers.BigNumber;
5
6
  export declare const BN: (val: number | string) => BigNumber;
6
7
  export declare const toBN: (n: ethers.BigNumber, decimals?: number) => BigNumber;
@@ -28,3 +29,4 @@ export declare const _getUsdPricesFromApi: () => Promise<IDict<number>>;
28
29
  export declare const _getUsdRate: (assetId: string) => Promise<number>;
29
30
  export declare const getUsdRate: (coin: string) => Promise<number>;
30
31
  export declare const getTVL: (chainId?: number) => Promise<number>;
32
+ export declare const _setContracts: (address: string, abi: any) => void;
package/lib/utils.js CHANGED
@@ -59,13 +59,14 @@ 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.getTVL = exports.getUsdRate = exports._getUsdRate = exports._getUsdPricesFromApi = exports.getPoolNameBySwapAddress = exports.ensureAllowance = exports.ensureAllowanceEstimateGas = exports._ensureAllowance = exports.hasAllowance = exports.getAllowance = exports._getAllowance = exports.getBalances = exports._prepareAddresses = exports._getBalances = exports._getCoinDecimals = exports._getCoinAddresses = exports.getEthIndex = exports.isEth = exports.parseUnits = exports.formatNumber = exports.checkNumber = exports._cutZeros = exports.fromBN = exports.toStringFromBN = exports.toBN = exports.BN = exports.MAX_ALLOWANCE = void 0;
62
+ exports._setContracts = exports.getTVL = exports.getUsdRate = exports._getUsdRate = exports._getUsdPricesFromApi = exports.getPoolNameBySwapAddress = exports.ensureAllowance = exports.ensureAllowanceEstimateGas = exports._ensureAllowance = exports.hasAllowance = exports.getAllowance = exports._getAllowance = exports.getBalances = exports._prepareAddresses = exports._getBalances = exports._getCoinDecimals = exports._getCoinAddresses = exports.getEthIndex = exports.isEth = exports.parseUnits = exports.formatNumber = exports.checkNumber = exports._cutZeros = exports.fromBN = exports.toStringFromBN = exports.toBN = exports.BN = exports.MAX_ALLOWANCE = exports.ETH_ADDRESS = void 0;
63
63
  var axios_1 = __importDefault(require("axios"));
64
64
  var ethers_1 = require("ethers");
65
65
  var bignumber_js_1 = __importDefault(require("bignumber.js"));
66
66
  var curve_1 = require("./curve");
67
67
  var external_api_1 = require("./external-api");
68
- var ETH_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
68
+ var ethcall_1 = require("ethcall");
69
+ exports.ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
69
70
  exports.MAX_ALLOWANCE = ethers_1.ethers.BigNumber.from(2).pow(ethers_1.ethers.BigNumber.from(256)).sub(ethers_1.ethers.BigNumber.from(1));
70
71
  // bignumber.js
71
72
  var BN = function (val) { return new bignumber_js_1.default(val); };
@@ -110,9 +111,9 @@ var parseUnits = function (n, decimals) {
110
111
  };
111
112
  exports.parseUnits = parseUnits;
112
113
  // -------------------
113
- var isEth = function (address) { return address.toLowerCase() === ETH_ADDRESS.toLowerCase(); };
114
+ var isEth = function (address) { return address.toLowerCase() === exports.ETH_ADDRESS.toLowerCase(); };
114
115
  exports.isEth = isEth;
115
- var getEthIndex = function (addresses) { return addresses.map(function (address) { return address.toLowerCase(); }).indexOf(ETH_ADDRESS.toLowerCase()); };
116
+ var getEthIndex = function (addresses) { return addresses.map(function (address) { return address.toLowerCase(); }).indexOf(exports.ETH_ADDRESS.toLowerCase()); };
116
117
  exports.getEthIndex = getEthIndex;
117
118
  // coins can be either addresses or symbols
118
119
  var _getCoinAddresses = function () {
@@ -404,7 +405,7 @@ var _getUsdPricesFromApi = function () { return __awaiter(void 0, void 0, void 0
404
405
  return __generator(this, function (_h) {
405
406
  switch (_h.label) {
406
407
  case 0:
407
- network = curve_1.curve.chainId === 137 ? "polygon" : "ethereum";
408
+ network = curve_1.curve.constants.NETWORK_NAME;
408
409
  promises = [
409
410
  (0, external_api_1._getPoolsFromApi)(network, "main"),
410
411
  (0, external_api_1._getPoolsFromApi)(network, "crypto"),
@@ -438,7 +439,7 @@ var _getUsdPricesFromApi = function () { return __awaiter(void 0, void 0, void 0
438
439
  exports._getUsdPricesFromApi = _getUsdPricesFromApi;
439
440
  var _usdRatesCache = {};
440
441
  var _getUsdRate = function (assetId) { return __awaiter(void 0, void 0, void 0, function () {
441
- var pricesFromApi, chainName, url, response;
442
+ var pricesFromApi, chainName, nativeTokenName, url, response;
442
443
  var _a, _b;
443
444
  return __generator(this, function (_c) {
444
445
  switch (_c.label) {
@@ -452,7 +453,12 @@ var _getUsdRate = function (assetId) { return __awaiter(void 0, void 0, void 0,
452
453
  chainName = {
453
454
  1: 'ethereum',
454
455
  137: 'polygon-pos',
455
- 1337: 'ethereum',
456
+ 43114: 'avalanche',
457
+ }[curve_1.curve.chainId];
458
+ nativeTokenName = {
459
+ 1: 'ethereum',
460
+ 137: 'matic-network',
461
+ 43114: 'avalanche-2',
456
462
  }[curve_1.curve.chainId];
457
463
  if (chainName === undefined) {
458
464
  throw Error('curve object is not initialized');
@@ -463,14 +469,18 @@ var _getUsdRate = function (assetId) { return __awaiter(void 0, void 0, void 0,
463
469
  'ETH': 'ethereum',
464
470
  'LINK': 'link',
465
471
  }[assetId] || assetId;
466
- assetId = (0, exports.isEth)(assetId) ? "ethereum" : assetId.toLowerCase();
472
+ assetId = (0, exports.isEth)(assetId) ? nativeTokenName : assetId.toLowerCase();
467
473
  // No EURT on Coingecko Polygon
468
- if (assetId.toLowerCase() === curve_1.curve.constants.COINS.eurt.toLowerCase()) {
474
+ if (curve_1.curve.chainId === 137 && assetId.toLowerCase() === curve_1.curve.constants.COINS.eurt) {
469
475
  chainName = 'ethereum';
470
476
  assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
471
477
  }
478
+ // CRV
479
+ if (assetId.toLowerCase() === curve_1.curve.constants.ALIASES.crv) {
480
+ assetId = 'curve-dao-token';
481
+ }
472
482
  if (!((((_a = _usdRatesCache[assetId]) === null || _a === void 0 ? void 0 : _a.time) || 0) + 600000 < Date.now())) return [3 /*break*/, 3];
473
- url = ['bitcoin', 'ethereum', 'link'].includes(assetId.toLowerCase()) ?
483
+ url = [nativeTokenName, 'bitcoin', 'link', 'curve-dao-token'].includes(assetId.toLowerCase()) ?
474
484
  "https://api.coingecko.com/api/v3/simple/price?ids=".concat(assetId, "&vs_currencies=usd") :
475
485
  "https://api.coingecko.com/api/v3/simple/token_price/".concat(chainName, "?contract_addresses=").concat(assetId, "&vs_currencies=usd");
476
486
  return [4 /*yield*/, axios_1.default.get(url)];
@@ -504,10 +514,15 @@ var getTVL = function (chainId) {
504
514
  if (chainId === void 0) { chainId = curve_1.curve.chainId; }
505
515
  return __awaiter(void 0, void 0, void 0, function () {
506
516
  var network, promises, allTypesExtendedPoolData;
507
- return __generator(this, function (_a) {
508
- switch (_a.label) {
517
+ var _a;
518
+ return __generator(this, function (_b) {
519
+ switch (_b.label) {
509
520
  case 0:
510
- network = chainId === 137 ? "polygon" : "ethereum";
521
+ network = (_a = {
522
+ 1: "ethereum",
523
+ 137: "polygon",
524
+ 43114: "avalanche",
525
+ }[chainId]) !== null && _a !== void 0 ? _a : "ethereum";
511
526
  promises = [
512
527
  (0, external_api_1._getPoolsFromApi)(network, "main"),
513
528
  (0, external_api_1._getPoolsFromApi)(network, "crypto"),
@@ -516,10 +531,17 @@ var getTVL = function (chainId) {
516
531
  ];
517
532
  return [4 /*yield*/, Promise.all(promises)];
518
533
  case 1:
519
- allTypesExtendedPoolData = _a.sent();
534
+ allTypesExtendedPoolData = _b.sent();
520
535
  return [2 /*return*/, allTypesExtendedPoolData.reduce(function (sum, data) { var _a; return sum + ((_a = data.tvl) !== null && _a !== void 0 ? _a : data.tvlAll); }, 0)];
521
536
  }
522
537
  });
523
538
  });
524
539
  };
525
540
  exports.getTVL = getTVL;
541
+ var _setContracts = function (address, abi) {
542
+ curve_1.curve.contracts[address] = {
543
+ contract: new ethers_1.Contract(address, abi, curve_1.curve.signer || curve_1.curve.provider),
544
+ multicallContract: new ethcall_1.Contract(address, abi),
545
+ };
546
+ };
547
+ exports._setContracts = _setContracts;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "JavaScript library for curve.fi",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",