diff --git a/src/curve.ts b/src/curve.ts index 7a575f55..e8cda3b9 100644 --- a/src/curve.ts +++ b/src/curve.ts @@ -51,9 +51,10 @@ import circulatingSupplyABI from './constants/abis/circulating_supply.json' asse import rootGaugeFactoryABI from "./constants/abis/gauge_factory/root_gauge_factory.json"; import { lowerCasePoolDataAddresses, extractDecimals, extractGauges } from "./constants/utils.js"; -import { _getHiddenPools, _getLiteNetworksData } from "./external-api.js"; +import {_getCurveLiteNetworks, _getHiddenPools, _getLiteNetworksData} from "./external-api.js"; import { L2Networks } from "./constants/L2Networks.js"; import { getTwocryptoFactoryPoolData } from "./factory/factory-twocrypto.js"; +import {getNetworkConstants, getNetworkNameByChainId} from "./utils"; export const memoizedContract = (): (address: string, abi: any, provider: BrowserProvider | JsonRpcProvider | Signer) => Contract => { const cache: Record = {}; @@ -251,7 +252,10 @@ class Curve implements ICurve { this.chainId = Number(network.chainId) === 133 || Number(network.chainId) === 31337 ? 1 : Number(network.chainId) as IChainId; this.isLiteChain = !(this.chainId in NETWORK_CONSTANTS); - const network_constants = this.isLiteChain ? await _getLiteNetworksData(this.chainId) : NETWORK_CONSTANTS[this.chainId]; + + const network_constants = await getNetworkConstants(this.chainId, this.isLiteChain); + + console.log('NETWORK DATA',network_constants) 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/external-api.ts b/src/external-api.ts index ed3a6167..9e2435e4 100644 --- a/src/external-api.ts +++ b/src/external-api.ts @@ -26,17 +26,17 @@ export const _getPoolsFromApi = memoize( } ) -export const _getAllPoolsFromApi = async (network: INetworkName): Promise => { +export const _getAllPoolsFromApi = async (network: INetworkName, isLiteChain = false): Promise => { return await Promise.all([ - _getPoolsFromApi(network, "main"), - _getPoolsFromApi(network, "crypto"), - _getPoolsFromApi(network, "factory"), - _getPoolsFromApi(network, "factory-crvusd"), - _getPoolsFromApi(network, "factory-eywa"), - _getPoolsFromApi(network, "factory-crypto"), - _getPoolsFromApi(network, "factory-twocrypto"), - _getPoolsFromApi(network, "factory-tricrypto"), - _getPoolsFromApi(network, "factory-stable-ng"), + _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), ]); } @@ -242,9 +242,9 @@ export const _getDaoProposal = memoize(async (type: "PARAMETER" | "OWNERSHIP", i // --- CURVE LITE --- export const _getLiteNetworksData = memoize( - async (chainId: number): Promise => { + async (networkName: string): Promise => { try { - const url = `https://api-core.curve.fi/v1/getNetworkData/${chainId}`; + const url = `https://api-core.curve.fi/v1/getDeployment/${networkName}`; const response = await axios.get(url, { validateStatus: () => true }); if (response.status !== 200 || !response.data?.data) { @@ -315,6 +315,7 @@ export const _getCurveLiteNetworks = memoize( id: platformId, name: metadata.name, rpcUrl: metadata.rpcUrl, + chainId: metadata.chainId, explorerUrl: metadata.explorerBaseUrl, }; }) diff --git a/src/interfaces.ts b/src/interfaces.ts index 406dd146..f490abc6 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -7,6 +7,7 @@ export interface IDict { 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 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; @@ -276,6 +277,7 @@ export interface IBasePoolShortItem { export interface ICurveLiteNetwork { id: string + chainId: number name: string rpcUrl: string explorerUrl: string diff --git a/src/pools/subClasses/statsPool.ts b/src/pools/subClasses/statsPool.ts index 38847a75..3999c864 100644 --- a/src/pools/subClasses/statsPool.ts +++ b/src/pools/subClasses/statsPool.ts @@ -140,10 +140,6 @@ export class StatsPool implements IStatsPool { } public totalLiquidity = async (useApi = true): Promise => { - if(curve.isLiteChain) { - throw Error('totalLiquidity is not supported for the lite version') - } - if (curve.chainId === 1 && this.pool.id === "crveth") return "0" if (this.pool.isLlamma) { @@ -213,7 +209,7 @@ export class StatsPool implements IStatsPool { public baseApy = async (): Promise<{ day: string, week: string }> => { if(curve.isLiteChain) { - throw Error('This method is not supported for the lite version') + throw Error('baseApy is not supported for the lite version') } const network = curve.constants.NETWORK_NAME; @@ -230,6 +226,10 @@ export class StatsPool implements IStatsPool { } public tokenApy = async (useApi = true): Promise<[baseApy: number, boostedApy: number]> => { + if(curve.isLiteChain) { + throw Error('tokenApy is not supported for the lite version') + } + if (this.pool.rewardsOnly()) throw Error(`${this.pool.name} has Rewards-Only Gauge. Use stats.rewardsApy instead`); const isDisabledChain = [1313161554].includes(curve.chainId); // Disable Aurora diff --git a/src/utils.ts b/src/utils.ts index 1275a693..fd7b4455 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -17,7 +17,7 @@ import { NETWORK_CONSTANTS } from "./constants/network_constants.js"; import { _getAllPoolsFromApi, _getCurveLiteNetworks, - _getFactoryAPYs, + _getFactoryAPYs, _getLiteNetworksData, _getSubgraphData, _getVolumes } from "./external-api.js"; @@ -316,7 +316,7 @@ export const getPoolIdBySwapAddress = (swapAddress: string): string => { export const _getUsdPricesFromApi = async (): Promise> => { const network = curve.constants.NETWORK_NAME; - const allTypesExtendedPoolData = await _getAllPoolsFromApi(network); + const allTypesExtendedPoolData = await _getAllPoolsFromApi(network, curve.isLiteChain); const priceDict: IDict[]> = {}; const priceDictByMaxTvl: IDict = {}; @@ -387,7 +387,7 @@ export const _getUsdPricesFromApi = async (): Promise> => { export const _getCrvApyFromApi = async (): Promise> => { const network = curve.constants.NETWORK_NAME; - const allTypesExtendedPoolData = await _getAllPoolsFromApi(network); + const allTypesExtendedPoolData = await _getAllPoolsFromApi(network, curve.isLiteChain); const apyDict: IDict<[number, number]> = {}; for (const extendedPoolData of allTypesExtendedPoolData) { @@ -407,7 +407,7 @@ export const _getCrvApyFromApi = async (): Promise> => { export const _getRewardsFromApi = async (): Promise> => { const network = curve.constants.NETWORK_NAME; - const allTypesExtendedPoolData = await _getAllPoolsFromApi(network); + const allTypesExtendedPoolData = await _getAllPoolsFromApi(network, curve.isLiteChain); const rewardsDict: IDict = {}; for (const extendedPoolData of allTypesExtendedPoolData) { @@ -834,6 +834,20 @@ 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