Skip to content
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
630 changes: 304 additions & 326 deletions package-lock.json

Large diffs are not rendered by default.

43 changes: 22 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/api",
"version": "2.66.26",
"version": "2.66.27",
"description": "JavaScript library for curve.finance",
"main": "lib/index.js",
"author": "Macket",
Expand All @@ -17,33 +17,34 @@
"url": "https://github.com/curvefi/curve-js/issues"
},
"scripts": {
"build": "rm -rf lib && tsc -p tsconfig.build.json",
"lint": "eslint src",
"build": "rm -rf lib && tsc --project tsconfig.build.json",
"lint": "eslint src --ext .ts",
"lint:fix": "eslint src --fix",
"tsc": "tsc -w"
"watch": "tsc --watch",
"watch:lib": "rm -rf lib && tsc --watch --project tsconfig.build.json"
},
"type": "module",
"devDependencies": {
"@babel/eslint-parser": "^7.25.9",
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.16.0",
"@types/chai": "^5.0.1",
"@types/memoizee": "^0.4.11",
"@babel/eslint-parser": "^7.27.1",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.27.0",
"@types/chai": "^5.2.2",
"@types/memoizee": "^0.4.12",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.2",
"@typescript-eslint/eslint-plugin": "^8.18.0",
"@typescript-eslint/parser": "^8.18.0",
"chai": "^5.1.2",
"eslint": "^9.16.0",
"globals": "^15.13.0",
"mocha": "^11.0.1",
"typescript": "^5.7.2",
"vue-eslint-parser": "^9.4.3"
"@types/node": "^22.15.21",
"@typescript-eslint/eslint-plugin": "^8.32.1",
"@typescript-eslint/parser": "^8.32.1",
"chai": "^5.2.0",
"eslint": "^9.27.0",
"globals": "^16.1.0",
"mocha": "^11.4.0",
"typescript": "^5.8.3",
"vue-eslint-parser": "^10.1.3"
},
"dependencies": {
"@curvefi/ethcall": "6.0.12",
"bignumber.js": "^9.1.2",
"ethers": "^6.13.4",
"@curvefi/ethcall": "^6.0.13",
"bignumber.js": "^9.3.0",
"ethers": "^6.14.1",
"memoizee": "^0.4.17"
}
}
46 changes: 46 additions & 0 deletions src/cached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import memoize from "memoizee";
import {IDict, IExtendedPoolDataFromApi, INetworkName, IPoolType} from "./interfaces.js";
import {uncached_getAllPoolsFromApi, createCrvApyDict, createUsdPricesDict} from './external-api.js'
import {curve} from "./curve";

/**
* 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,
}
)

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

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

export const _getUsdPricesFromApi = async (): Promise<IDict<number>> => {
const network = curve.constants.NETWORK_NAME;
const {usdPrices} = await _getCachedData(network, false);
return usdPrices
}

export const _getCrvApyFromApi = async (): Promise<IDict<[number, number]>> => {
const network = curve.constants.NETWORK_NAME;
const {crvApy} = await _getCachedData(network, false);
return crvApy
}
130 changes: 100 additions & 30 deletions src/external-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,108 @@ import {
} from "./interfaces";


export const _getPoolsFromApi = memoize(
async (network: INetworkName, poolType: IPoolType, isLiteChain = false): Promise<IExtendedPoolDataFromApi> => {
const api = isLiteChain ? "https://api-core.curve.finance/v1/" : "https://api.curve.finance/api";
const url = `${api}/getPools/${network}/${poolType}`;
return await fetchData(url) ?? { poolData: [], tvl: 0, tvlAll: 0 };
},
{
promise: true,
maxAge: 5 * 60 * 1000, // 5m
const uncached_getPoolsFromApi = async (network: INetworkName, poolType: IPoolType, isLiteChain = false): Promise<IExtendedPoolDataFromApi> => {
const api = isLiteChain ? "https://api-core.curve.finance/v1/" : "https://api.curve.finance/api";
const url = `${api}/getPools/${network}/${poolType}`;
return await fetchData(url) ?? { poolData: [], tvl: 0, tvlAll: 0 };
}

const getPoolTypes = (isLiteChain: boolean) => isLiteChain ? ["factory-twocrypto", "factory-tricrypto", "factory-stable-ng"] as const :
["main", "crypto", "factory", "factory-crvusd", "factory-eywa", "factory-crypto", "factory-twocrypto", "factory-tricrypto", "factory-stable-ng"] as const;

export const uncached_getAllPoolsFromApi = async (network: INetworkName, isLiteChain = false): Promise<Record<IPoolType, IExtendedPoolDataFromApi>> =>
Object.fromEntries(
await Promise.all(getPoolTypes(isLiteChain).map(async (poolType) => {
const data = await uncached_getPoolsFromApi(network, poolType, isLiteChain);
return [poolType, data];
}))
)

export const createUsdPricesDict = (allTypesExtendedPoolData: IExtendedPoolDataFromApi[]): IDict<number> => {
const priceDict: IDict<Record<string, number>[]> = {};
const priceDictByMaxTvl: IDict<number> = {};

for (const extendedPoolData of allTypesExtendedPoolData) {
for (const pool of extendedPoolData.poolData) {
const lpTokenAddress = pool.lpTokenAddress ?? pool.address;
const totalSupply = pool.totalSupply / (10 ** 18);
if(lpTokenAddress.toLowerCase() in priceDict) {
priceDict[lpTokenAddress.toLowerCase()].push({
price: pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0,
tvl: pool.usdTotal,
})
} else {
priceDict[lpTokenAddress.toLowerCase()] = []
priceDict[lpTokenAddress.toLowerCase()].push({
price: pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0,
tvl: pool.usdTotal,
})
}

for (const coin of pool.coins) {
if (typeof coin.usdPrice === "number") {
if(coin.address.toLowerCase() in priceDict) {
priceDict[coin.address.toLowerCase()].push({
price: coin.usdPrice,
tvl: pool.usdTotal,
})
} else {
priceDict[coin.address.toLowerCase()] = []
priceDict[coin.address.toLowerCase()].push({
price: coin.usdPrice,
tvl: pool.usdTotal,
})
}
}
}

for (const coin of pool.gaugeRewards ?? []) {
if (typeof coin.tokenPrice === "number") {
if(coin.tokenAddress.toLowerCase() in priceDict) {
priceDict[coin.tokenAddress.toLowerCase()].push({
price: coin.tokenPrice,
tvl: pool.usdTotal,
});
} else {
priceDict[coin.tokenAddress.toLowerCase()] = []
priceDict[coin.tokenAddress.toLowerCase()].push({
price: coin.tokenPrice,
tvl: pool.usdTotal,
});
}
}
}
}
}
)

export const _getAllPoolsFromApi = async (network: INetworkName, isLiteChain = false): Promise<IExtendedPoolDataFromApi[]> => {
if(isLiteChain) {
return await Promise.all([
_getPoolsFromApi(network, "factory-twocrypto", isLiteChain),
_getPoolsFromApi(network, "factory-tricrypto", isLiteChain),
_getPoolsFromApi(network, "factory-stable-ng", isLiteChain),
]);
} else {
return await Promise.all([
_getPoolsFromApi(network, "main", isLiteChain),
_getPoolsFromApi(network, "crypto", isLiteChain),
_getPoolsFromApi(network, "factory", isLiteChain),
_getPoolsFromApi(network, "factory-crvusd", isLiteChain),
_getPoolsFromApi(network, "factory-eywa", isLiteChain),
_getPoolsFromApi(network, "factory-crypto", isLiteChain),
_getPoolsFromApi(network, "factory-twocrypto", isLiteChain),
_getPoolsFromApi(network, "factory-tricrypto", isLiteChain),
_getPoolsFromApi(network, "factory-stable-ng", isLiteChain),
]);
for(const address in priceDict) {
if (priceDict[address].length) {
const maxTvlItem = priceDict[address].reduce((prev, current) => +current.tvl > +prev.tvl ? current : prev);
priceDictByMaxTvl[address] = maxTvlItem.price
} else {
priceDictByMaxTvl[address] = 0
}
}

return priceDictByMaxTvl
}

export const createCrvApyDict = (allTypesExtendedPoolData: IExtendedPoolDataFromApi[]): IDict<[number, number]> => {
const apyDict: IDict<[number, number]> = {};

for (const extendedPoolData of allTypesExtendedPoolData) {
for (const pool of extendedPoolData.poolData) {
if (pool.gaugeAddress) {
if (!pool.gaugeCrvApy) {
apyDict[pool.gaugeAddress.toLowerCase()] = [0, 0];
} else {
apyDict[pool.gaugeAddress.toLowerCase()] = [pool.gaugeCrvApy[0] ?? 0, pool.gaugeCrvApy[1] ?? 0];
}
}
}
}

return apyDict
}

export const _getSubgraphData = memoize(
Expand Down Expand Up @@ -287,4 +357,4 @@ async function fetchJson(url: string) {
async function fetchData(url: string) {
const {data} = await fetchJson(url);
return data;
}
}
2 changes: 1 addition & 1 deletion src/factory/factory-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import twocryptoFactorySwapABI from "../constants/abis/factory-twocrypto/factory
import tricryptoFactorySwapABI from "../constants/abis/factory-tricrypto/factory-tricrypto-pool.json" with { type: 'json' };
import tricryptoFactoryEthDisabledSwapABI from "../constants/abis/factory-tricrypto/factory-tricrypto-pool-eth-disabled.json" with { type: 'json' };
import { getPoolIdByAddress, setFactoryZapContracts } from "./common.js";
import { _getPoolsFromApi } from "../external-api.js";
import { _getPoolsFromApi } from "../cached.js";
import {assetTypeNameHandler, getPoolName, isStableNgPool} from "../utils.js";
import StableNgBasePoolZapABI from "../constants/abis/stable-ng-base-pool-zap.json" with { type: 'json' };
import MetaStableSwapNGABI from "../constants/abis/factory-stable-ng/meta-stableswap-ng.json" with { type: 'json' };
Expand Down
6 changes: 5 additions & 1 deletion src/pools/subClasses/corePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ export class CorePool implements ICorePool {
inApi: boolean;

constructor(id: string) {
const poolData = curve.getPoolsData()[id];
const poolsData = curve.getPoolsData();
if (!poolsData[id]) {
throw new Error(`Pool ${id} not found. Available pools: ${Object.keys(poolsData).join(', ')}`);
}
const poolData = poolsData[id];

this.id = id;
this.name = poolData.name;
Expand Down
3 changes: 1 addition & 2 deletions src/pools/subClasses/statsPool.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { curve } from "../../curve.js";
import {IPoolType, IReward} from '../../interfaces.js';
import {_getPoolsFromApi} from '../../external-api.js';
import {_getPoolsFromApi,_getCrvApyFromApi} from '../../cached.js';
import {
_getUsdRate,
BN,
toBN,
_getCrvApyFromApi,
_getRewardsFromApi,
getVolumeApiController,
} from '../../utils.js';
Expand Down
2 changes: 1 addition & 1 deletion src/pools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getPool } from "./poolConstructor.js";
import { IDict } from "../interfaces";
import { curve } from "../curve.js";
import { _getRewardsFromApi, _getUsdRate, _setContracts, toBN } from "../utils.js";
import { _getAllPoolsFromApi } from "../external-api.js";
import { _getAllPoolsFromApi } from "../cached.js";
import ERC20Abi from "../constants/abis/ERC20.json" with { type: 'json' };

const BATCH_SIZE = 50;
Expand Down
Loading
Loading