|
1 | | -import memoize from "memoizee"; |
2 | 1 | import {IDict, IExtendedPoolDataFromApi, INetworkName, IPoolType} from "./interfaces.js"; |
3 | 2 | import {uncached_getAllPoolsFromApi, uncached_getCrvApyFromApi, uncached_getUsdPricesFromApi} from './external-api.js' |
4 | 3 | import {curve} from "./curve"; |
5 | 4 |
|
6 | | -const _getCachedData = memoize( |
7 | | - async (network: INetworkName, isLiteChain: boolean) => { |
8 | | - const poolsDict = await uncached_getAllPoolsFromApi(network, isLiteChain); |
9 | | - const poolLists = Object.values(poolsDict) |
10 | | - const usdPrices = uncached_getUsdPricesFromApi(poolLists); |
11 | | - const crvApy = uncached_getCrvApyFromApi(poolLists) |
12 | | - return { poolsDict, poolLists, usdPrices, crvApy }; |
13 | | - }, |
14 | | - { |
15 | | - promise: true, |
16 | | - maxAge: 5 * 60 * 1000, // 5m |
17 | | - primitive: true, |
| 5 | +const memoize = <TResult, TParams extends any[], TFunc extends (...args: TParams) => Promise<TResult>>(fn: TFunc, {maxAge}: { |
| 6 | + maxAge: number |
| 7 | +}) => { |
| 8 | + const cache: Record<string, Promise<TResult>> = {}; |
| 9 | + const cachedFn = async (...args: TParams): Promise<TResult> => { |
| 10 | + const key = JSON.stringify(args); |
| 11 | + if (key in cache) { |
| 12 | + return cache[key]; |
| 13 | + } |
| 14 | + const promise = fn(...args); |
| 15 | + cache[key] = promise; |
| 16 | + try { |
| 17 | + const result = await promise; |
| 18 | + setTimeout(() => delete cache[key], maxAge); |
| 19 | + return result; |
| 20 | + } catch (e) { |
| 21 | + delete cache[key]; |
| 22 | + throw e; |
| 23 | + } |
| 24 | + }; |
| 25 | + cachedFn.set = (result: TResult, ...args: TParams) => { |
| 26 | + const key = JSON.stringify(args); |
| 27 | + setTimeout(() => delete cache[key], maxAge); |
| 28 | + cache[key] = Promise.resolve(result); |
18 | 29 | } |
19 | | -) |
| 30 | + return cachedFn as TFunc & { set: (result: TResult, ...args: TParams) => void }; |
| 31 | +} |
| 32 | + |
| 33 | +function createCache(poolsDict: Record<"main" | "crypto" | "factory" | "factory-crvusd" | "factory-eywa" | "factory-crypto" | "factory-twocrypto" | "factory-tricrypto" | "factory-stable-ng", IExtendedPoolDataFromApi>) { |
| 34 | + const poolLists = Object.values(poolsDict) |
| 35 | + const usdPrices = uncached_getUsdPricesFromApi(poolLists); |
| 36 | + const crvApy = uncached_getCrvApyFromApi(poolLists) |
| 37 | + return {poolsDict, poolLists, usdPrices, crvApy}; |
| 38 | +} |
| 39 | + |
| 40 | +const _getCachedData = memoize( |
| 41 | + async (network: INetworkName, isLiteChain: boolean) => createCache(await uncached_getAllPoolsFromApi(network, isLiteChain)) |
| 42 | + , { |
| 43 | + maxAge: 1000 * 60 * 5, // 5 minutes |
| 44 | + }) |
20 | 45 |
|
21 | 46 | export const _getPoolsFromApi = |
22 | 47 | async (network: INetworkName, poolType: IPoolType, isLiteChain = false): Promise<IExtendedPoolDataFromApi> => { |
23 | 48 | const {poolsDict} = await _getCachedData(network, isLiteChain); |
24 | 49 | return poolsDict[poolType] |
25 | 50 | } |
26 | 51 |
|
| 52 | +export const _setPoolsFromApi = |
| 53 | + (network: INetworkName, isLiteChain: boolean, data: Record<IPoolType, IExtendedPoolDataFromApi>): void => |
| 54 | + _getCachedData.set( |
| 55 | + createCache(data), |
| 56 | + network, |
| 57 | + isLiteChain |
| 58 | + ) |
| 59 | + |
27 | 60 | export const _getAllPoolsFromApi = async (network: INetworkName, isLiteChain = false): Promise<IExtendedPoolDataFromApi[]> => { |
28 | 61 | const {poolLists} = await _getCachedData(network, isLiteChain); |
29 | 62 | return poolLists |
|
0 commit comments