From bd8ca5829b13cbef9d46414b6ad36cc865e40f19 Mon Sep 17 00:00:00 2001 From: macket Date: Thu, 21 Nov 2024 11:37:37 +0400 Subject: [PATCH 1/4] fix: getTvl for lite chains --- src/utils.ts | 58 +++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 074a0435..c9d0483a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -613,19 +613,43 @@ 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)) { +export const getCurveLiteNetworks = async (): Promise => { + 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): Promise> => { + if(isLiteChain) { + const NAME = getNetworkNameByChainId(chainId, await _getCurveLiteNetworks()); + if (NAME === "Unknown Network") throw Error(`Wrong chain id: ${chainId}`); + return {... await _getLiteNetworksData(NAME), NAME }; + } else { + if (chainId in NETWORK_CONSTANTS) { + return NETWORK_CONSTANTS[chainId] || {}; + } else { + throw Error(`Wrong chain id: ${chainId}`); + } + } +} + +const _getNetworkName = async (network: INetworkName | IChainId = curve.chainId): Promise => { + if (typeof network === "number") { + const network_constants = await getNetworkConstants(network, curve.isLiteChain); + return network_constants.NAME; + } else if (Object.values(NETWORK_CONSTANTS).map((n) => n.NAME).includes(network)) { return network; } else { throw Error(`Wrong network name or id: ${network}`); } } -export const getTVL = async (network: INetworkName | IChainId = curve.chainId): Promise => { - network = _getNetworkName(network); - const allTypesExtendedPoolData = await _getAllPoolsFromApi(network); +export const getTVL = async (network: INetworkName | IChainId = curve.chainId, isLiteChain = curve.isLiteChain): Promise => { + network = await _getNetworkName(network); + const allTypesExtendedPoolData = await _getAllPoolsFromApi(network, isLiteChain); return allTypesExtendedPoolData.reduce((sum, data) => sum + (data.tvl ?? data.tvlAll ?? 0), 0) } @@ -653,7 +677,7 @@ export const getVolume = async (network: INetworkName | IChainId = curve.chainId throw Error('This method is not supported for the lite version') } - network = _getNetworkName(network); + network = await _getNetworkName(network); const { totalVolume, cryptoVolume, cryptoShare } = await getVolumeApiController(network); return { totalVolume, cryptoVolume, cryptoShare } } @@ -828,24 +852,6 @@ export function runWorker(code: string, syncFn }); } -export const getCurveLiteNetworks = async (): Promise => { - 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 From d67ca8bd71117708106a2d3b962d57bf2d9b8394 Mon Sep 17 00:00:00 2001 From: macket Date: Thu, 21 Nov 2024 11:39:13 +0400 Subject: [PATCH 2/4] build: v2.65.12 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6dbf030b..c14ee4fd 100644 --- a/package.json +++ b/package.json @@ -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", From e56977f71dc2a2d64d889fa6b2f7c279e33e949c Mon Sep 17 00:00:00 2001 From: macket Date: Thu, 21 Nov 2024 12:00:30 +0400 Subject: [PATCH 3/4] refactor: getNetworkConstants --- src/curve.ts | 2 +- src/utils.ts | 37 +++++++++++-------------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/curve.ts b/src/curve.ts index 1748af77..f14e28fe 100644 --- a/src/curve.ts +++ b/src/curve.ts @@ -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; diff --git a/src/utils.ts b/src/utils.ts index c9d0483a..d264077c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -622,34 +622,19 @@ export const getNetworkNameByChainId = (chainId: number, networks: ICurveLiteNet return network ? network.id : "Unknown Network"; } -export const getNetworkConstants = async (chainId: IChainId | number, isLiteChain: boolean): Promise> => { - if(isLiteChain) { +export const getNetworkConstants = async (chainId: IChainId | number): Promise> => { + if (chainId in NETWORK_CONSTANTS) { + return { ...NETWORK_CONSTANTS[chainId], IS_LITE_CHAIN: false}; + } else { const NAME = getNetworkNameByChainId(chainId, await _getCurveLiteNetworks()); if (NAME === "Unknown Network") throw Error(`Wrong chain id: ${chainId}`); - return {... await _getLiteNetworksData(NAME), NAME }; - } else { - if (chainId in NETWORK_CONSTANTS) { - return NETWORK_CONSTANTS[chainId] || {}; - } else { - throw Error(`Wrong chain id: ${chainId}`); - } - } -} - -const _getNetworkName = async (network: INetworkName | IChainId = curve.chainId): Promise => { - if (typeof network === "number") { - const network_constants = await getNetworkConstants(network, curve.isLiteChain); - return network_constants.NAME; - } else if (Object.values(NETWORK_CONSTANTS).map((n) => n.NAME).includes(network)) { - return network; - } else { - throw Error(`Wrong network name or id: ${network}`); + return {... await _getLiteNetworksData(NAME), NAME, IS_LITE_CHAIN: true }; } } -export const getTVL = async (network: INetworkName | IChainId = curve.chainId, isLiteChain = curve.isLiteChain): Promise => { - network = await _getNetworkName(network); - const allTypesExtendedPoolData = await _getAllPoolsFromApi(network, isLiteChain); +export const getTVL = async (chainId = curve.chainId): Promise => { + 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) } @@ -672,13 +657,13 @@ export const getVolumeApiController = async (network: INetworkName): Promise => { +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 = await _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 } } From 6ffdafd47781dd6f172d0141836a9a5b377c6460 Mon Sep 17 00:00:00 2001 From: macket Date: Thu, 21 Nov 2024 13:01:33 +0400 Subject: [PATCH 4/4] refactor: INetworkName = string, IChainId = number --- src/interfaces.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index dc9e6f4f..cb5f70e7 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -5,9 +5,8 @@ export interface IDict { [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;