@curvefi/api 2.70.3 → 2.70.5

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/cached.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { IDict, IExtendedPoolDataFromApi, INetworkName, IPoolType } from "./interfaces.js";
2
2
  export declare const _getPoolsFromApi: (network: INetworkName, poolType: IPoolType, isLiteChain?: boolean) => Promise<IExtendedPoolDataFromApi>;
3
+ export declare const _getPoolTotalLiquidityFromApi: (network: INetworkName, poolId: string, address: string, isCrypto: boolean, isLiteChain: boolean) => Promise<string | undefined>;
3
4
  export declare const _setPoolsFromApi: (network: INetworkName, isLiteChain: boolean, data: Record<IPoolType, IExtendedPoolDataFromApi>) => void;
4
5
  export declare const _getAllPoolsFromApi: (network: INetworkName, isLiteChain: boolean) => Promise<IExtendedPoolDataFromApi[]>;
5
6
  export declare const _getUsdPricesFromApi: (network: INetworkName, isLiteChain: boolean) => Promise<IDict<number>>;
package/lib/cached.js CHANGED
@@ -74,6 +74,13 @@ export const _getPoolsFromApi = (network_1, poolType_1, ...args_1) => __awaiter(
74
74
  const { poolsDict } = yield _getCachedData(network, isLiteChain);
75
75
  return poolsDict[poolType];
76
76
  });
77
+ export const _getPoolTotalLiquidityFromApi = (network, poolId, address, isCrypto, isLiteChain) => __awaiter(void 0, void 0, void 0, function* () {
78
+ const poolType = poolId.startsWith("factory") ? poolId.replace(/-\d+$/, '').replace(/-v2$/, '') : isCrypto ? "crypto" : "main";
79
+ const { poolData } = yield _getPoolsFromApi(network, poolType, isLiteChain);
80
+ const poolAddress = address.toLowerCase();
81
+ const poolEntry = poolData.find((data) => data.address.toLowerCase() === poolAddress);
82
+ return poolEntry ? String(poolEntry.usdTotal) : undefined;
83
+ });
77
84
  export const _setPoolsFromApi = (network, isLiteChain, data) => _getCachedData.set(createCache(data), network, isLiteChain);
78
85
  export const _getAllPoolsFromApi = (network, isLiteChain) => __awaiter(void 0, void 0, void 0, function* () {
79
86
  const { poolLists } = yield _getCachedData(network, isLiteChain);
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { _getPoolsFromApi, _getCrvApyFromApi } from '../../cached.js';
10
+ import { _getPoolTotalLiquidityFromApi, _getCrvApyFromApi } from '../../cached.js';
11
11
  import { _getUsdRate, BN, toBN, _getRewardsFromApi, getVolumeApiController, } from '../../utils.js';
12
12
  import { rewardNetworks } from '../../constants/rewardNetworks.js';
13
13
  import memoize from "memoizee";
@@ -107,17 +107,9 @@ export class StatsPool {
107
107
  return stablecoinTvlBN.plus(collateralTvlBN).toString();
108
108
  }
109
109
  if (useApi) {
110
- const network = curve.constants.NETWORK_NAME;
111
- let poolType = this.pool.isCrypto ? "crypto" : "main";
112
- if (this.pool.id.startsWith("factory")) {
113
- poolType = this.pool.id.replace(/-\d+$/, '');
114
- poolType = poolType.replace(/-v2$/, '');
115
- }
116
- const poolsData = (yield _getPoolsFromApi.call(curve, network, poolType, curve.isLiteChain)).poolData;
117
- const poolEntry = poolsData.find((data) => data.address.toLowerCase() === this.pool.address.toLowerCase());
118
- if (poolEntry) {
119
- return String(poolEntry.usdTotal);
120
- }
110
+ const totalLiquidity = yield _getPoolTotalLiquidityFromApi(curve.constants.NETWORK_NAME, this.pool.id, this.pool.address, this.pool.isCrypto, curve.isLiteChain);
111
+ if (totalLiquidity !== undefined)
112
+ return totalLiquidity;
121
113
  }
122
114
  const balances = yield this.underlyingBalances();
123
115
  const promises = [];
@@ -7,4 +7,3 @@ export type IRouterWorkerInput = {
7
7
  ybAssets: string[];
8
8
  };
9
9
  export declare function routeFinderWorker(): (({ inputCoinAddress, outputCoinAddress, routerGraph, poolData, ybAssets }: IRouterWorkerInput) => IRouteStep[][]) | undefined;
10
- export declare const routeFinderWorkerCode: string;
@@ -110,5 +110,3 @@ export function routeFinderWorker() {
110
110
  }
111
111
  });
112
112
  }
113
- // this is a workaround to avoid importing web-worker in the main bundle (nextjs will try to inject invalid hot-reloading code)
114
- export const routeFinderWorkerCode = `${routeFinderWorker.toString()}; ${routeFinderWorker.name}();`;
@@ -10,4 +10,3 @@ export type IRouteGraphInput = {
10
10
  blacklist?: string[];
11
11
  };
12
12
  export declare function routeGraphWorker(): (({ constants, chainId, isLiteChain, allPools, amplificationCoefficientDict, poolTvlDict, blacklist }: IRouteGraphInput) => IDict<IDict<IRouteStep[]>>) | undefined;
13
- export declare const routeGraphWorkerCode: string;
@@ -541,5 +541,3 @@ export function routeGraphWorker() {
541
541
  }
542
542
  });
543
543
  }
544
- // this is a workaround to avoid importing web-worker in the main bundle (nextjs will try to inject invalid hot-reloading code)
545
- export const routeGraphWorkerCode = `${routeGraphWorker.toString()}; ${routeGraphWorker.name}();`;
package/lib/router.js CHANGED
@@ -13,10 +13,11 @@ import { _cutZeros, _get_price_impact, _get_small_x, _getCoinAddresses, _getCoin
13
13
  import { getPool } from "./pools/index.js";
14
14
  import { _getAmplificationCoefficientsFromApi } from "./pools/utils.js";
15
15
  import { L2Networks } from "./constants/L2Networks.js";
16
- import { routeFinderWorker, routeFinderWorkerCode } from "./route-finder.worker.js";
17
- import { routeGraphWorker, routeGraphWorkerCode } from "./route-graph.worker.js";
16
+ import { routeFinderWorker } from "./route-finder.worker.js";
17
+ import { routeGraphWorker } from "./route-graph.worker.js";
18
18
  import { memoizeMethod } from "./constants/utils.js";
19
19
  import { YB_ASSETS } from "./constants/ybPools.js";
20
+ import { _getPoolTotalLiquidityFromApi } from "./cached.js";
20
21
  const MAX_STEPS = 5;
21
22
  const ROUTE_LENGTH = (MAX_STEPS * 2) + 1;
22
23
  export function setRouterBlacklist(blacklist) {
@@ -25,8 +26,16 @@ export function setRouterBlacklist(blacklist) {
25
26
  delete this._buildRouteGraph;
26
27
  delete this._getBestRoute;
27
28
  }
28
- function _getTVL(poolId) {
29
- return __awaiter(this, void 0, void 0, function* () { return Number(yield getPool.call(this, poolId).stats.totalLiquidityMemoized()); });
29
+ function _getTVL(poolId_1, _a) {
30
+ return __awaiter(this, arguments, void 0, function* (poolId, { is_crypto, is_llamma, swap_address }) {
31
+ // LLAMMA liquidity must be calculated on-chain, even when the API has an entry.
32
+ if (!is_llamma) {
33
+ const totalLiquidity = yield _getPoolTotalLiquidityFromApi(this.constants.NETWORK_NAME, poolId, swap_address, is_crypto !== null && is_crypto !== void 0 ? is_crypto : false, this.isLiteChain);
34
+ if (totalLiquidity !== undefined)
35
+ return Number(totalLiquidity);
36
+ }
37
+ return Number(yield getPool.call(this, poolId).stats.totalLiquidityMemoized());
38
+ });
30
39
  }
31
40
  function entriesToDictAsync(entries, mapper) {
32
41
  return __awaiter(this, void 0, void 0, function* () {
@@ -47,7 +56,7 @@ function _buildRouteGraphImpl(chainId, isLiteChain) {
47
56
  const amplificationCoefficientDict = yield _getAmplificationCoefficientsFromApi.call(this);
48
57
  const poolTvlDict = yield entriesToDictAsync(allPools, _getTVL.bind(this));
49
58
  const input = { constants, chainId, isLiteChain, allPools, amplificationCoefficientDict, poolTvlDict, blacklist: this.routerBlacklist };
50
- return runWorker(routeGraphWorkerCode, routeGraphWorker, Object.assign({ type: 'createRouteGraph' }, input));
59
+ return runWorker(routeGraphWorker, Object.assign({ type: 'createRouteGraph' }, input));
51
60
  });
52
61
  }
53
62
  function _findRoutes(inputCoinAddress, outputCoinAddress) {
@@ -57,7 +66,7 @@ function _findRoutes(inputCoinAddress, outputCoinAddress) {
57
66
  // extract only the fields we need for the worker
58
67
  const poolData = mapDict(this.getPoolsData(), (_, { is_lending, wrapped_coin_addresses, underlying_coin_addresses, token_address }) => ({ is_lending, wrapped_coin_addresses, underlying_coin_addresses, token_address }));
59
68
  const input = { inputCoinAddress, outputCoinAddress, routerGraph, poolData, ybAssets: YB_ASSETS };
60
- return runWorker(routeFinderWorkerCode, routeFinderWorker, Object.assign({ type: 'findRoutes' }, input));
69
+ return runWorker(routeFinderWorker, Object.assign({ type: 'findRoutes' }, input));
61
70
  });
62
71
  }
63
72
  const _getRouteKey = (route, inputCoinAddress, outputCoinAddress) => {
package/lib/utils.d.ts CHANGED
@@ -6,14 +6,14 @@ import { Curve } from "./curve.js";
6
6
  export declare const ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
7
7
  export declare const MAX_ALLOWANCE: bigint;
8
8
  export declare const abiInfoBuild: (address: string, method: string, abi: any) => IMethodInfo;
9
- export declare const _cutZeros: (strn: string) => string;
10
9
  export declare const checkNumber: (n: number | string) => number | string;
11
- export declare const formatNumber: (n: number | string, decimals?: number) => string;
12
- export declare function parseUnits(n: number | string, decimals?: number): bigint;
13
10
  export declare const BN: (val: number | string) => BigNumber;
14
11
  export declare const toBN: (n: bigint, decimals?: number) => BigNumber;
15
12
  export declare const toStringFromBN: (bn: BigNumber, decimals?: number) => string;
16
13
  export declare const fromBN: (bn: BigNumber, decimals?: number) => bigint;
14
+ export declare const _cutZeros: (strn: string) => string;
15
+ export declare const formatNumber: (n: number | string, decimals?: number) => string;
16
+ export declare function parseUnits(n: number | string, decimals?: number): bigint;
17
17
  export declare const mulBy1_3: (n: bigint) => bigint;
18
18
  export declare const smartNumber: (abstractNumber: bigint | bigint[]) => number | number[];
19
19
  export declare const DIGas: (gas: bigint | Array<bigint>) => bigint;
@@ -108,7 +108,7 @@ export declare function getBasePools(this: Curve): Promise<IBasePoolShortItem[]>
108
108
  export declare function log(fnName: string, ...args: unknown[]): void;
109
109
  export declare function runWorker<In extends {
110
110
  type: string;
111
- }, Out>(code: string, syncFn: () => ((val: In) => Out) | undefined, inputData: In, timeout?: number): Promise<Out>;
111
+ }, Out>(workerFn: () => ((val: In) => Out) | undefined, inputData: In, timeout?: number): Promise<Out>;
112
112
  export declare const PERIODS: {
113
113
  DAY: number;
114
114
  WEEK: number;
package/lib/utils.js CHANGED
@@ -28,25 +28,12 @@ export const abiInfoBuild = (address, method, abi) => ({
28
28
  method,
29
29
  abi,
30
30
  });
31
- // Formatting numbers
32
- export const _cutZeros = (strn) => {
33
- return strn.replace(/(\.\d*[1-9])0+$/gi, '$1').replace(/\.0+$/gi, '');
34
- };
31
+ // bignumber.js
35
32
  export const checkNumber = (n) => {
36
33
  if (Number(n) !== Number(n))
37
34
  throw Error(`${n} is not a number`); // NaN
38
35
  return n;
39
36
  };
40
- export const formatNumber = (n, decimals = 18) => {
41
- if (Number(n) !== Number(n))
42
- throw Error(`${n} is not a number`); // NaN
43
- const [integer, fractional] = String(n).split(".");
44
- return !fractional ? integer : integer + "." + fractional.slice(0, decimals);
45
- };
46
- export function parseUnits(n, decimals = 18) {
47
- return ethers.parseUnits(formatNumber(n, decimals), decimals);
48
- }
49
- // bignumber.js
50
37
  export const BN = (val) => new BigNumber(checkNumber(val));
51
38
  export const toBN = (n, decimals = 18) => {
52
39
  return BN(formatUnits(n, decimals));
@@ -57,6 +44,14 @@ export const toStringFromBN = (bn, decimals = 18) => {
57
44
  export const fromBN = (bn, decimals = 18) => {
58
45
  return parseUnits(toStringFromBN(bn, decimals), decimals);
59
46
  };
47
+ // Formatting numbers
48
+ export const _cutZeros = (strn) => {
49
+ return strn.replace(/(\.\d*[1-9])0+$/gi, '$1').replace(/\.0+$/gi, '');
50
+ };
51
+ export const formatNumber = (n, decimals = 18) => BN(n).toFixed(decimals, BigNumber.ROUND_DOWN);
52
+ export function parseUnits(n, decimals = 18) {
53
+ return ethers.parseUnits(formatNumber(n, decimals), decimals);
54
+ }
60
55
  // -------------------
61
56
  export const mulBy1_3 = (n) => n * parseUnits("130", 0) / parseUnits("100", 0);
62
57
  export const smartNumber = (abstractNumber) => {
@@ -828,11 +823,12 @@ export function log(fnName, ...args) {
828
823
  console.log(`curve-js@${new Date().toISOString()} -> ${fnName}:`, ...args);
829
824
  }
830
825
  }
831
- export function runWorker(code, syncFn, inputData, timeout = 30000) {
826
+ export function runWorker(workerFn, inputData, timeout = 30000) {
832
827
  if (typeof Worker === 'undefined') {
833
828
  // in nodejs run worker in main thread
834
- return Promise.resolve(syncFn()(inputData));
829
+ return Promise.resolve(workerFn()(inputData));
835
830
  }
831
+ const code = `${workerFn.toString()}; ${workerFn.name}();`;
836
832
  const blob = new Blob([code], { type: 'application/javascript' });
837
833
  const blobUrl = URL.createObjectURL(blob);
838
834
  const worker = new Worker(blobUrl, { type: 'module' });
@@ -840,7 +836,7 @@ export function runWorker(code, syncFn, inputData, timeout = 30000) {
840
836
  const timer = setTimeout(() => reject(new Error('Timeout')), timeout);
841
837
  worker.onerror = (e) => {
842
838
  clearTimeout(timer);
843
- console.error(code, inputData, e);
839
+ console.error(workerFn.name, inputData, e);
844
840
  reject(e);
845
841
  };
846
842
  worker.onmessage = (e) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/api",
3
- "version": "2.70.3",
3
+ "version": "2.70.5",
4
4
  "description": "JavaScript library for curve.finance",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",