Skip to content

Commit

Permalink
fix: fixed curve lite fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
fedorovdg committed Nov 1, 2024
1 parent 85c6c2b commit 2c61ff8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Contract> = {};
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 13 additions & 12 deletions src/external-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ export const _getPoolsFromApi = memoize(
}
)

export const _getAllPoolsFromApi = async (network: INetworkName): Promise<IExtendedPoolDataFromApi[]> => {
export const _getAllPoolsFromApi = async (network: INetworkName, isLiteChain = false): Promise<IExtendedPoolDataFromApi[]> => {
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),
]);
}

Expand Down Expand Up @@ -242,9 +242,9 @@ export const _getDaoProposal = memoize(async (type: "PARAMETER" | "OWNERSHIP", i
// --- CURVE LITE ---

export const _getLiteNetworksData = memoize(
async (chainId: number): Promise<any> => {
async (networkName: string): Promise<any> => {
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) {
Expand Down Expand Up @@ -315,6 +315,7 @@ export const _getCurveLiteNetworks = memoize(
id: platformId,
name: metadata.name,
rpcUrl: metadata.rpcUrl,
chainId: metadata.chainId,
explorerUrl: metadata.explorerBaseUrl,
};
})
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IDict<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 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 Expand Up @@ -276,6 +277,7 @@ export interface IBasePoolShortItem {

export interface ICurveLiteNetwork {
id: string
chainId: number
name: string
rpcUrl: string
explorerUrl: string
Expand Down
10 changes: 5 additions & 5 deletions src/pools/subClasses/statsPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ export class StatsPool implements IStatsPool {
}

public totalLiquidity = async (useApi = true): Promise<string> => {
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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
22 changes: 18 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { NETWORK_CONSTANTS } from "./constants/network_constants.js";
import {
_getAllPoolsFromApi,
_getCurveLiteNetworks,
_getFactoryAPYs,
_getFactoryAPYs, _getLiteNetworksData,
_getSubgraphData,
_getVolumes

Check failure on line 22 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test

Missing trailing comma
} from "./external-api.js";
Expand Down Expand Up @@ -316,7 +316,7 @@ export const getPoolIdBySwapAddress = (swapAddress: string): string => {

export const _getUsdPricesFromApi = async (): Promise<IDict<number>> => {
const network = curve.constants.NETWORK_NAME;
const allTypesExtendedPoolData = await _getAllPoolsFromApi(network);
const allTypesExtendedPoolData = await _getAllPoolsFromApi(network, curve.isLiteChain);
const priceDict: IDict<Record<string, number>[]> = {};
const priceDictByMaxTvl: IDict<number> = {};

Expand Down Expand Up @@ -387,7 +387,7 @@ export const _getUsdPricesFromApi = async (): Promise<IDict<number>> => {

export const _getCrvApyFromApi = async (): Promise<IDict<[number, number]>> => {
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) {
Expand All @@ -407,7 +407,7 @@ export const _getCrvApyFromApi = async (): Promise<IDict<[number, number]>> => {

export const _getRewardsFromApi = async (): Promise<IDict<IRewardFromApi[]>> => {
const network = curve.constants.NETWORK_NAME;
const allTypesExtendedPoolData = await _getAllPoolsFromApi(network);
const allTypesExtendedPoolData = await _getAllPoolsFromApi(network, curve.isLiteChain);
const rewardsDict: IDict<IRewardFromApi[]> = {};

for (const extendedPoolData of allTypesExtendedPoolData) {
Expand Down Expand Up @@ -834,6 +834,20 @@ 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 2c61ff8

Please sign in to comment.