Skip to content

Commit

Permalink
Merge pull request #418 from curvefi/fix/fix-tvl-for-lite
Browse files Browse the repository at this point in the history
Fix: tvl for lite chains
  • Loading branch information
Macket authored Nov 21, 2024
2 parents 66827e0 + 6ffdafd commit 5a90bc9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
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.65.11",
"version": "2.65.12",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"author": "Macket",
Expand Down
2 changes: 1 addition & 1 deletion src/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class Curve implements ICurve {

this.isLiteChain = !(this.chainId in NETWORK_CONSTANTS);

const network_constants = await getNetworkConstants(this.chainId, this.isLiteChain);
const network_constants = await getNetworkConstants(this.chainId);
this.constants.NATIVE_TOKEN = network_constants.NATIVE_COIN;
this.constants.NETWORK_NAME = network_constants.NAME;
this.constants.ALIASES = network_constants.ALIASES;
Expand Down
5 changes: 2 additions & 3 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ export interface IDict<T> {
[index: string]: T,
}

export type INetworkName = "ethereum" | "optimism" | "bsc" | "xdai" | "polygon" | "x-layer" | "fantom" | "fraxtal" | "zksync" | "moonbeam" | "kava" | "mantle" | "base" | "arbitrum" | "celo" | "avalanche" | "aurora";
export type IChainId = 1 | 10 | 56 | 100 | 137 | 196 | 250 | 252 | 324 | 1284 | 2222 | 5000 | 8453 | 42161 | 42220 | 43114 | 1313161554;
export type IChainIdLite = number
export type INetworkName = string;
export type IChainId = number;
export type IFactoryPoolType = "factory" | "factory-crvusd" | "factory-eywa" | "factory-crypto" | "factory-twocrypto" | "factory-tricrypto" | "factory-stable-ng";
export type IPoolType = "main" | "crypto" | IFactoryPoolType;
export type ISwapType = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
Expand Down
51 changes: 21 additions & 30 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,19 +613,28 @@ export const getTxCostsUsd = (ethUsdRate: number, gasPrice: number, gas: number
}
}

const _getNetworkName = (network: INetworkName | IChainId = curve.chainId): INetworkName => {
if (typeof network === "number" && NETWORK_CONSTANTS[network]) {
return NETWORK_CONSTANTS[network].NAME;
} else if (typeof network === "string" && Object.values(NETWORK_CONSTANTS).map((n) => n.NAME).includes(network)) {
return network;
export const getCurveLiteNetworks = async (): Promise<ICurveLiteNetwork[]> => {
return await _getCurveLiteNetworks()
}

export const getNetworkNameByChainId = (chainId: number, networks: ICurveLiteNetwork[]): string => {
const network = networks.find((network: ICurveLiteNetwork) => network.chainId === chainId);
return network ? network.id : "Unknown Network";
}

export const getNetworkConstants = async (chainId: IChainId | number): Promise<IDict<any>> => {
if (chainId in NETWORK_CONSTANTS) {
return { ...NETWORK_CONSTANTS[chainId], IS_LITE_CHAIN: false};
} else {
throw Error(`Wrong network name or id: ${network}`);
const NAME = getNetworkNameByChainId(chainId, await _getCurveLiteNetworks());
if (NAME === "Unknown Network") throw Error(`Wrong chain id: ${chainId}`);
return {... await _getLiteNetworksData(NAME), NAME, IS_LITE_CHAIN: true };
}
}

export const getTVL = async (network: INetworkName | IChainId = curve.chainId): Promise<number> => {
network = _getNetworkName(network);
const allTypesExtendedPoolData = await _getAllPoolsFromApi(network);
export const getTVL = async (chainId = curve.chainId): Promise<number> => {
const networkConstants = await getNetworkConstants(chainId);
const allTypesExtendedPoolData = await _getAllPoolsFromApi(networkConstants.NAME, networkConstants.IS_LITE_CHAIN);

return allTypesExtendedPoolData.reduce((sum, data) => sum + (data.tvl ?? data.tvlAll ?? 0), 0)
}
Expand All @@ -648,13 +657,13 @@ export const getVolumeApiController = async (network: INetworkName): Promise<IVo
throw Error(`Can't get volume for network: ${network}`);
}

export const getVolume = async (network: INetworkName | IChainId = curve.chainId): Promise<{ totalVolume: number, cryptoVolume: number, cryptoShare: number }> => {
export const getVolume = async (chainId = curve.chainId): Promise<{ totalVolume: number, cryptoVolume: number, cryptoShare: number }> => {
if(curve.isLiteChain) {
throw Error('This method is not supported for the lite version')
}

network = _getNetworkName(network);
const { totalVolume, cryptoVolume, cryptoShare } = await getVolumeApiController(network);
const networkConstants = await getNetworkConstants(chainId);
const { totalVolume, cryptoVolume, cryptoShare } = await getVolumeApiController(networkConstants.NAME);
return { totalVolume, cryptoVolume, cryptoShare }
}

Expand Down Expand Up @@ -828,24 +837,6 @@ export function runWorker<In extends { type: string }, Out>(code: string, syncFn
});
}

export const getCurveLiteNetworks = async (): Promise<ICurveLiteNetwork[]> => {
return await _getCurveLiteNetworks()
}

export const getNetworkNameByChainId = (chainId: number, networks: ICurveLiteNetwork[]): string => {
const network = networks.find((network: ICurveLiteNetwork) => network.chainId === chainId);
return network ? network.id : "Unknown Network";
}

export const getNetworkConstants = async (chainId: IChainId | number, isLiteChain: boolean) => {
if(isLiteChain) {
const NAME = getNetworkNameByChainId(chainId, await _getCurveLiteNetworks())
return {... await _getLiteNetworksData(NAME), NAME };
} else {
return NETWORK_CONSTANTS[chainId];
}
}

export const PERIODS = {
DAY: 86400,
WEEK: 604800, // 7 * 86400
Expand Down

0 comments on commit 5a90bc9

Please sign in to comment.