Skip to content

feat: allow pool data injection #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/api",
"version": "2.66.30",
"version": "2.66.31",
"description": "JavaScript library for curve.finance",
"main": "lib/index.js",
"author": "Macket",
Expand Down
89 changes: 74 additions & 15 deletions src/cached.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,91 @@
import memoize from "memoizee";
import {IDict, IExtendedPoolDataFromApi, INetworkName, IPoolType} from "./interfaces.js";
import {createCrvApyDict, createUsdPricesDict, uncached_getAllPoolsFromApi} from './external-api.js'

/**
* Memoizes a function that returns a promise.
* Custom function instead of `memoizee` because we want to be able to set the cache manually based on server data.
* @param fn The function that returns a promise and will be memoized
* @param maxAge The maximum age of the cache in milliseconds
* @param createKey A function that creates a key for the cache based on the arguments passed to the function
* @returns A memoized `fn` function that includes a `set` method to set the cache manually
*/
const memoize = <TResult, TParams extends any[], TFunc extends (...args: TParams) => Promise<TResult>>(fn: TFunc, {
maxAge,
createKey = (list) => list.toString(),
}: {
maxAge: number,
createKey?: (args: TParams) => string
}) => {
const cache: Record<string, Promise<TResult>> = {};
const timeouts: Record<string, ReturnType<typeof setTimeout>> = {};

const setCache = (key: string, promise?: Promise<TResult>) => {
if (promise) {
cache[key] = promise;
} else if (key in cache) {
delete cache[key];
}
if (key in timeouts) {
clearTimeout(timeouts[key]);
delete timeouts[key]
}
};

const scheduleCleanup = (key: string) => timeouts[key] = setTimeout(() => {
delete timeouts[key];
delete cache[key];
}, maxAge);

const cachedFn = async (...args: TParams): Promise<TResult> => {
const key = createKey(args);
if (key in cache) {
return cache[key];
}
const promise = fn(...args);
setCache(key, promise);
try {
const result = await promise;
scheduleCleanup(key)
return result;
} catch (e) {
delete cache[key];
throw e;
}
};

cachedFn.set = (result: TResult, ...args: TParams) => {
const key = createKey(args);
setCache(key, Promise.resolve(result));
scheduleCleanup(key);
}

return cachedFn as TFunc & { set: (result: TResult, ...args: TParams) => void };
}

const createCache = (poolsDict: Record<IPoolType, IExtendedPoolDataFromApi>) => {
const poolLists = Object.values(poolsDict)
const usdPrices = createUsdPricesDict(poolLists);
const crvApy = createCrvApyDict(poolLists)
return {poolsDict, poolLists, usdPrices, crvApy};
};

/**
* This function is used to cache the data fetched from the API and the data derived from it.
* Note: do not expose this function to the outside world, instead encapsulate it in a function that returns the data you need.
*/
const _getCachedData = memoize(
async (network: INetworkName, isLiteChain: boolean) => {
const poolsDict = await uncached_getAllPoolsFromApi(network, isLiteChain);
const poolLists = Object.values(poolsDict)
const usdPrices = createUsdPricesDict(poolLists);
const crvApy = createCrvApyDict(poolLists)
return { poolsDict, poolLists, usdPrices, crvApy };
},
{
promise: true,
maxAge: 5 * 60 * 1000, // 5m
primitive: true,
}
)
const _getCachedData = memoize(async (network: INetworkName, isLiteChain: boolean) =>
createCache(await uncached_getAllPoolsFromApi(network, isLiteChain)), {maxAge: 1000 * 60 * 5 /* 5 minutes */})

export const _getPoolsFromApi =
async (network: INetworkName, poolType: IPoolType, isLiteChain = false): Promise<IExtendedPoolDataFromApi> => {
const {poolsDict} = await _getCachedData(network, isLiteChain);
return poolsDict[poolType]
}

export const _setPoolsFromApi =
(network: INetworkName, isLiteChain: boolean, data: Record<IPoolType, IExtendedPoolDataFromApi>): void =>
_getCachedData.set(createCache(data), network, isLiteChain)

export const _getAllPoolsFromApi = async (network: INetworkName, isLiteChain: boolean): Promise<IExtendedPoolDataFromApi[]> => {
const {poolLists} = await _getCachedData(network, isLiteChain);
return poolLists
Expand Down
20 changes: 17 additions & 3 deletions src/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ import {getFactoryPoolData} from "./factory/factory.js";
import {getFactoryPoolsDataFromApi} from "./factory/factory-api.js";
import {getCryptoFactoryPoolData} from "./factory/factory-crypto.js";
import {getTricryptoFactoryPoolData} from "./factory/factory-tricrypto.js";
import {Abi, IChainId, ICurve, IDict, IFactoryPoolType, INetworkConstants, IPoolData} from "./interfaces";
import {
IPoolData,
IDict,
ICurve,
IChainId,
IFactoryPoolType,
Abi,
INetworkConstants,
IPoolType,
IExtendedPoolDataFromApi,
} from "./interfaces";
import ERC20Abi from './constants/abis/ERC20.json' with {type: 'json'};
import cERC20Abi from './constants/abis/cERC20.json' with {type: 'json'};
import yERC20Abi from './constants/abis/yERC20.json' with {type: 'json'};
Expand Down Expand Up @@ -61,7 +71,7 @@ import {_getHiddenPools} from "./external-api.js";
import {L2Networks} from "./constants/L2Networks.js";
import {getTwocryptoFactoryPoolData} from "./factory/factory-twocrypto.js";
import {getNetworkConstants} from "./utils.js";

import {_setPoolsFromApi} from "./cached";

export const OLD_CHAINS = [1, 10, 56, 100, 137, 250, 1284, 2222, 8453, 42161, 42220, 43114, 1313161554]; // these chains have non-ng pools

Expand Down Expand Up @@ -149,7 +159,7 @@ export class Curve implements ICurve {
async init(
providerType: 'JsonRpc' | 'Web3' | 'Infura' | 'Alchemy' | 'NoRPC',
providerSettings: { url?: string, privateKey?: string, batchMaxCount? : number } | { externalProvider: ethers.Eip1193Provider } | { network?: Networkish, apiKey?: string } | 'NoRPC',
options: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number, chainId?: number } = {} // gasPrice in Gwei
options: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number, chainId?: number, poolsData?: Record<IPoolType, IExtendedPoolDataFromApi> } = {} // gasPrice in Gwei
): Promise<void> {
this.provider = null!;
this.signer = null;
Expand Down Expand Up @@ -294,6 +304,10 @@ export class Curve implements ICurve {
}

this.feeData = { gasPrice: options.gasPrice, maxFeePerGas: options.maxFeePerGas, maxPriorityFeePerGas: options.maxPriorityFeePerGas };
if (options.poolsData) {
_setPoolsFromApi(this.constants.NETWORK_NAME, this.isLiteChain, options.poolsData);
}

await this.updateFeeData();

for (const pool of Object.values({...this.constants.POOLS_DATA, ...this.constants.LLAMMAS_DATA})) {
Expand Down
Loading