@curvefi/api 1.10.0 → 1.14.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 (43) hide show
  1. package/README.md +38 -0
  2. package/lib/constants/abis/abis-ethereum.d.ts +1 -1
  3. package/lib/constants/abis/abis-ethereum.js +68 -2
  4. package/lib/constants/abis/abis-polygon.d.ts +4 -0
  5. package/lib/constants/abis/abis-polygon.js +154 -0
  6. package/lib/constants/abis/json/atricrypto3/swap.json +1269 -0
  7. package/lib/constants/abis/json/atricrypto3/zap.json +239 -0
  8. package/lib/constants/abis/json/busd/deposit.json +5 -10
  9. package/lib/constants/abis/json/busd/swap.json +18 -36
  10. package/lib/constants/abis/json/compound/deposit.json +5 -10
  11. package/lib/constants/abis/json/compound/migration.json +1 -2
  12. package/lib/constants/abis/json/compound/oldSwap.json +16 -32
  13. package/lib/constants/abis/json/compound/swap.json +18 -36
  14. package/lib/constants/abis/json/iearn/deposit.json +5 -10
  15. package/lib/constants/abis/json/iearn/swap.json +18 -36
  16. package/lib/constants/abis/json/paave/rewards.json +657 -0
  17. package/lib/constants/abis/json/pax/deposit.json +5 -10
  18. package/lib/constants/abis/json/pax/swap.json +19 -38
  19. package/lib/constants/abis/json/ren/swap.json +19 -38
  20. package/lib/constants/abis/json/ren-polygon/swap.json +1112 -0
  21. package/lib/constants/abis/json/sbtc/swap.json +19 -38
  22. package/lib/constants/abis/json/streamer.json +257 -0
  23. package/lib/constants/abis/json/susdv2/deposit.json +5 -10
  24. package/lib/constants/abis/json/susdv2/swap.json +16 -32
  25. package/lib/constants/abis/json/usdt/deposit.json +5 -10
  26. package/lib/constants/abis/json/usdt/swap.json +19 -37
  27. package/lib/constants/aliases.d.ts +16 -0
  28. package/lib/constants/aliases.js +19 -0
  29. package/lib/constants/coins-ethereum.d.ts +31 -0
  30. package/lib/constants/{coins.js → coins-ethereum.js} +48 -14
  31. package/lib/constants/coins-polygon.d.ts +31 -0
  32. package/lib/constants/coins-polygon.js +74 -0
  33. package/lib/curve.d.ts +23 -7
  34. package/lib/curve.js +133 -91
  35. package/lib/index.d.ts +2 -1
  36. package/lib/index.js +3 -0
  37. package/lib/interfaces.d.ts +3 -0
  38. package/lib/pools.d.ts +25 -4
  39. package/lib/pools.js +323 -101
  40. package/lib/utils.d.ts +1 -1
  41. package/lib/utils.js +49 -41
  42. package/package.json +6 -5
  43. package/lib/constants/coins.d.ts +0 -25
package/lib/utils.d.ts CHANGED
@@ -20,5 +20,5 @@ export declare const _ensureAllowance: (coins: string[], amounts: ethers.BigNumb
20
20
  export declare const ensureAllowanceEstimateGas: (coins: string[], amounts: string[], spender: string) => Promise<number>;
21
21
  export declare const ensureAllowance: (coins: string[], amounts: string[], spender: string) => Promise<string[]>;
22
22
  export declare const getPoolNameBySwapAddress: (swapAddress: string) => string;
23
- export declare const getCrvRate: () => Promise<number>;
24
23
  export declare const _getUsdRate: (assetId: string) => Promise<number>;
24
+ export declare const _getStatsUrl: (isCrypto?: boolean) => string;
package/lib/utils.js CHANGED
@@ -59,15 +59,12 @@ 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._getUsdRate = exports.getCrvRate = 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.fromBN = exports.toStringFromBN = exports.toBN = exports.BN = exports.MAX_ALLOWANCE = void 0;
62
+ exports._getStatsUrl = exports._getUsdRate = 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.fromBN = exports.toStringFromBN = exports.toBN = exports.BN = exports.MAX_ALLOWANCE = 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
- var abis_ethereum_1 = require("./constants/abis/abis-ethereum");
68
- var coins_1 = require("./constants/coins");
69
- var LP_TOKENS = Object.values(abis_ethereum_1.poolsData).map(function (data) { return data.token_address.toLowerCase(); });
70
- var GAUGES = Object.values(abis_ethereum_1.poolsData).map(function (data) { return data.gauge_address.toLowerCase(); });
67
+ var curve_2 = require("./curve");
71
68
  var ETH_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
72
69
  exports.MAX_ALLOWANCE = ethers_1.ethers.BigNumber.from(2).pow(ethers_1.ethers.BigNumber.from(256)).sub(ethers_1.ethers.BigNumber.from(1));
73
70
  // bignumber.js
@@ -75,12 +72,12 @@ var BN = function (val) { return new bignumber_js_1.default(val); };
75
72
  exports.BN = BN;
76
73
  var toBN = function (n, decimals) {
77
74
  if (decimals === void 0) { decimals = 18; }
78
- return (0, exports.BN)(ethers_1.ethers.utils.formatUnits(n, decimals)).times(decimals);
75
+ return (0, exports.BN)(ethers_1.ethers.utils.formatUnits(n, decimals));
79
76
  };
80
77
  exports.toBN = toBN;
81
78
  var toStringFromBN = function (bn, decimals) {
82
79
  if (decimals === void 0) { decimals = 18; }
83
- return bn.div(decimals).toFixed(decimals);
80
+ return bn.toFixed(decimals);
84
81
  };
85
82
  exports.toStringFromBN = toStringFromBN;
86
83
  var fromBN = function (bn, decimals) {
@@ -102,8 +99,8 @@ var _getCoinAddresses = function () {
102
99
  if (coins.length == 1 && Array.isArray(coins[0]))
103
100
  coins = coins[0];
104
101
  coins = coins;
105
- var coinAddresses = coins.map(function (c) { return coins_1.COINS[c.toLowerCase()] || c; });
106
- var availableAddresses = __spreadArray(__spreadArray(__spreadArray([], Object.keys(coins_1.LOWER_CASE_DECIMALS).filter(function (c) { return c !== coins_1.COINS['snx'].toLowerCase(); }), true), LP_TOKENS, true), GAUGES, true);
102
+ var coinAddresses = coins.map(function (c) { return curve_2.COINS[c.toLowerCase()] || c; });
103
+ var availableAddresses = __spreadArray(__spreadArray(__spreadArray([], Object.keys(curve_2.DECIMALS_LOWER_CASE).filter(function (c) { var _a; return c !== ((_a = curve_2.COINS['snx']) === null || _a === void 0 ? void 0 : _a.toLowerCase()); }), true), curve_1.LP_TOKENS, true), curve_1.GAUGES, true);
107
104
  for (var _a = 0, coinAddresses_1 = coinAddresses; _a < coinAddresses_1.length; _a++) {
108
105
  var coinAddr = coinAddresses_1[_a];
109
106
  if (!availableAddresses.includes(coinAddr.toLowerCase()))
@@ -120,11 +117,11 @@ var _getCoinDecimals = function () {
120
117
  if (coinAddresses.length == 1 && Array.isArray(coinAddresses[0]))
121
118
  coinAddresses = coinAddresses[0];
122
119
  coinAddresses = coinAddresses;
123
- return coinAddresses.map(function (coinAddr) { return coins_1.LOWER_CASE_DECIMALS[coinAddr.toLowerCase()] || 18; });
120
+ return coinAddresses.map(function (coinAddr) { return curve_2.DECIMALS_LOWER_CASE[coinAddr.toLowerCase()] || 18; });
124
121
  };
125
122
  exports._getCoinDecimals = _getCoinDecimals;
126
123
  var _getBalances = function (coins, addresses) { return __awaiter(void 0, void 0, void 0, function () {
127
- var coinAddresses, decimals, ethIndex, contractCalls, _loop_1, _i, coinAddresses_2, coinAddr, response, ethBalances, _a, addresses_1, address, _b, _c, _balances, balances, _d, addresses_2, address;
124
+ var coinAddresses, decimals, ethIndex, contractCalls, _loop_1, _i, coinAddresses_2, coinAddr, _response, ethBalances, _a, addresses_1, address, _b, _c, _balances, balances, _d, addresses_2, address;
128
125
  return __generator(this, function (_e) {
129
126
  switch (_e.label) {
130
127
  case 0:
@@ -144,7 +141,7 @@ var _getBalances = function (coins, addresses) { return __awaiter(void 0, void 0
144
141
  }
145
142
  return [4 /*yield*/, curve_1.curve.multicallProvider.all(contractCalls)];
146
143
  case 1:
147
- response = _e.sent();
144
+ _response = _e.sent();
148
145
  if (!(ethIndex !== -1)) return [3 /*break*/, 6];
149
146
  ethBalances = [];
150
147
  _a = 0, addresses_1 = addresses;
@@ -161,12 +158,12 @@ var _getBalances = function (coins, addresses) { return __awaiter(void 0, void 0
161
158
  _a++;
162
159
  return [3 /*break*/, 2];
163
160
  case 5:
164
- response.splice.apply(response, __spreadArray([ethIndex * addresses.length, 0], ethBalances, false));
161
+ _response.splice.apply(_response, __spreadArray([ethIndex * addresses.length, 0], ethBalances, false));
165
162
  _e.label = 6;
166
163
  case 6:
167
164
  _balances = {};
168
165
  addresses.forEach(function (address, i) {
169
- _balances[address] = coins.map(function (_, j) { return response[i + (j * addresses.length)]; });
166
+ _balances[address] = coins.map(function (_, j) { return _response[i + (j * addresses.length)]; });
170
167
  });
171
168
  balances = {};
172
169
  for (_d = 0, addresses_2 = addresses; _d < addresses_2.length; _d++) {
@@ -370,47 +367,45 @@ var ensureAllowance = function (coins, amounts, spender) { return __awaiter(void
370
367
  }); };
371
368
  exports.ensureAllowance = ensureAllowance;
372
369
  var getPoolNameBySwapAddress = function (swapAddress) {
373
- return Object.entries(abis_ethereum_1.poolsData).filter(function (_a) {
370
+ return Object.entries(curve_1.POOLS_DATA).filter(function (_a) {
374
371
  var _ = _a[0], poolData = _a[1];
375
372
  return poolData.swap_address.toLowerCase() === swapAddress.toLowerCase();
376
373
  })[0][0];
377
374
  };
378
375
  exports.getPoolNameBySwapAddress = getPoolNameBySwapAddress;
379
- var _crvRateCache = {
380
- 'rate': 0,
381
- 'time': 0,
382
- };
383
- var getCrvRate = function () { return __awaiter(void 0, void 0, void 0, function () {
384
- var crvAddress, response;
385
- return __generator(this, function (_a) {
386
- switch (_a.label) {
387
- case 0:
388
- crvAddress = "0xd533a949740bb3306d119cc777fa900ba034cd52";
389
- crvAddress = crvAddress.toLowerCase();
390
- if (!(_crvRateCache.time + 60000 < Date.now())) return [3 /*break*/, 2];
391
- return [4 /*yield*/, axios_1.default.get("https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=".concat(crvAddress, "&vs_currencies=usd"))];
392
- case 1:
393
- response = _a.sent();
394
- _crvRateCache['rate'] = response.data[crvAddress]['usd'];
395
- _crvRateCache['time'] = Date.now();
396
- _a.label = 2;
397
- case 2: return [2 /*return*/, _crvRateCache['rate']];
398
- }
399
- });
400
- }); };
401
- exports.getCrvRate = getCrvRate;
402
376
  var _usdRatesCache = {};
403
377
  var _getUsdRate = function (assetId) { return __awaiter(void 0, void 0, void 0, function () {
404
- var url, response;
378
+ var chainName, url, response;
405
379
  var _a;
406
380
  return __generator(this, function (_b) {
407
381
  switch (_b.label) {
408
382
  case 0:
383
+ if (assetId === 'USD' || (curve_1.curve.chainId === 137 && (assetId.toLowerCase() === curve_2.COINS.am3crv.toLowerCase())))
384
+ return [2 /*return*/, 1];
385
+ chainName = {
386
+ 1: 'ethereum',
387
+ 137: 'polygon-pos',
388
+ 1337: 'ethereum',
389
+ }[curve_1.curve.chainId];
390
+ if (chainName === undefined) {
391
+ throw Error('curve object is not initialized');
392
+ }
393
+ assetId = {
394
+ 'EUR': curve_2.COINS.eurt,
395
+ 'BTC': 'bitcoin',
396
+ 'ETH': 'ethereum',
397
+ 'LINK': 'link',
398
+ }[assetId] || assetId;
409
399
  assetId = (0, exports.isEth)(assetId) ? "ethereum" : assetId.toLowerCase();
400
+ // No EURT on Coingecko Polygon
401
+ if (assetId.toLowerCase() === curve_2.COINS.eurt.toLowerCase()) {
402
+ chainName = 'ethereum';
403
+ assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
404
+ }
410
405
  if (!((((_a = _usdRatesCache[assetId]) === null || _a === void 0 ? void 0 : _a.time) || 0) + 600000 < Date.now())) return [3 /*break*/, 2];
411
- url = assetId.toLowerCase() === "ethereum" ?
406
+ url = ['bitcoin', 'ethereum', 'link'].includes(assetId.toLowerCase()) ?
412
407
  "https://api.coingecko.com/api/v3/simple/price?ids=".concat(assetId, "&vs_currencies=usd") :
413
- "https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=".concat(assetId, "&vs_currencies=usd");
408
+ "https://api.coingecko.com/api/v3/simple/token_price/".concat(chainName, "?contract_addresses=").concat(assetId, "&vs_currencies=usd");
414
409
  return [4 /*yield*/, axios_1.default.get(url)];
415
410
  case 1:
416
411
  response = _b.sent();
@@ -421,3 +416,16 @@ var _getUsdRate = function (assetId) { return __awaiter(void 0, void 0, void 0,
421
416
  });
422
417
  }); };
423
418
  exports._getUsdRate = _getUsdRate;
419
+ var _getStatsUrl = function (isCrypto) {
420
+ if (isCrypto === void 0) { isCrypto = false; }
421
+ if (curve_1.curve.chainId === 1 || curve_1.curve.chainId === 1337) {
422
+ return isCrypto ? "http://stats.curve.fi/raw-stats-crypto/apys.json" : "http://stats.curve.fi/raw-stats/apys.json";
423
+ }
424
+ else if (curve_1.curve.chainId === 137) {
425
+ return "http://stats.curve.fi/raw-stats-polygon/apys.json";
426
+ }
427
+ else {
428
+ throw Error("Unsupported network id".concat(curve_1.curve.chainId));
429
+ }
430
+ };
431
+ exports._getStatsUrl = _getStatsUrl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "1.10.0",
3
+ "version": "1.14.0",
4
4
  "description": "JavaScript library for curve.fi",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -12,18 +12,19 @@
12
12
  "@types/chai": "^4.2.18",
13
13
  "@types/mocha": "^8.2.2",
14
14
  "@types/node": "^14.14.37",
15
- "@typescript-eslint/eslint-plugin": "^4.20.0",
15
+ "@typescript-eslint/eslint-plugin": "^4.33.0",
16
16
  "@typescript-eslint/parser": "^4.20.0",
17
17
  "babel-eslint": "^10.1.0",
18
18
  "chai": "^4.3.4",
19
- "eslint": "^7.23.0",
19
+ "eslint": "^7.32.0",
20
20
  "mocha": "^8.4.0",
21
+ "typescript": "^4.5.2",
21
22
  "vue-eslint-parser": "^7.6.0"
22
23
  },
23
24
  "dependencies": {
24
25
  "axios": "^0.21.1",
25
26
  "bignumber.js": "^9.0.1",
26
- "ethers": "^5.4.6",
27
- "ethers-multicall": "^0.1.6"
27
+ "ethcall": "^4.2.5",
28
+ "ethers": "^5.4.6"
28
29
  }
29
30
  }
@@ -1,25 +0,0 @@
1
- export declare const BTC_COINS: {
2
- [index: string]: string;
3
- };
4
- export declare const BTC_COINS_LOWER_CASE: any;
5
- export declare const ETH_COINS: {
6
- [index: string]: string;
7
- };
8
- export declare const ETH_COINS_LOWER_CASE: any;
9
- export declare const LINK_COINS: {
10
- [index: string]: string;
11
- };
12
- export declare const LINK_COINS_LOWER_CASE: any;
13
- export declare const EUR_COINS: {
14
- [index: string]: string;
15
- };
16
- export declare const USD_COINS: {
17
- [index: string]: string;
18
- };
19
- export declare const COINS: {
20
- [index: string]: string;
21
- };
22
- export declare const DECIMALS: {
23
- [index: string]: number;
24
- };
25
- export declare const LOWER_CASE_DECIMALS: any;