@curvefi/api 2.70.4 → 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 +1 -0
- package/lib/cached.js +7 -0
- package/lib/pools/subClasses/statsPool.js +4 -12
- package/lib/route-finder.worker.d.ts +0 -1
- package/lib/route-finder.worker.js +0 -2
- package/lib/route-graph.worker.d.ts +0 -1
- package/lib/route-graph.worker.js +0 -2
- package/lib/router.js +15 -6
- package/lib/utils.d.ts +1 -1
- package/lib/utils.js +4 -3
- package/package.json +1 -1
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 {
|
|
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
|
|
111
|
-
|
|
112
|
-
|
|
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
|
|
17
|
-
import { routeGraphWorker
|
|
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(
|
|
29
|
-
return __awaiter(this,
|
|
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(
|
|
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(
|
|
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
|
@@ -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>(
|
|
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
|
@@ -823,11 +823,12 @@ export function log(fnName, ...args) {
|
|
|
823
823
|
console.log(`curve-js@${new Date().toISOString()} -> ${fnName}:`, ...args);
|
|
824
824
|
}
|
|
825
825
|
}
|
|
826
|
-
export function runWorker(
|
|
826
|
+
export function runWorker(workerFn, inputData, timeout = 30000) {
|
|
827
827
|
if (typeof Worker === 'undefined') {
|
|
828
828
|
// in nodejs run worker in main thread
|
|
829
|
-
return Promise.resolve(
|
|
829
|
+
return Promise.resolve(workerFn()(inputData));
|
|
830
830
|
}
|
|
831
|
+
const code = `${workerFn.toString()}; ${workerFn.name}();`;
|
|
831
832
|
const blob = new Blob([code], { type: 'application/javascript' });
|
|
832
833
|
const blobUrl = URL.createObjectURL(blob);
|
|
833
834
|
const worker = new Worker(blobUrl, { type: 'module' });
|
|
@@ -835,7 +836,7 @@ export function runWorker(code, syncFn, inputData, timeout = 30000) {
|
|
|
835
836
|
const timer = setTimeout(() => reject(new Error('Timeout')), timeout);
|
|
836
837
|
worker.onerror = (e) => {
|
|
837
838
|
clearTimeout(timer);
|
|
838
|
-
console.error(
|
|
839
|
+
console.error(workerFn.name, inputData, e);
|
|
839
840
|
reject(e);
|
|
840
841
|
};
|
|
841
842
|
worker.onmessage = (e) => {
|