Skip to content

Commit

Permalink
Merge pull request #249 from curvefi/fix/fixed-getVolume
Browse files Browse the repository at this point in the history
fix: fixed getVolume
  • Loading branch information
fedorovdg authored Jan 5, 2024
2 parents 2e9a425 + 03d91cf commit 47cd668
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 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.53.6",
"version": "2.53.7",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"author": "Macket",
Expand Down
19 changes: 17 additions & 2 deletions src/external-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ export const _getLegacyAPYsAndVolumes = memoize(

// Base, Bsc, ZkSync, Moonbeam, Kava and Celo only
export const _getFactoryAPYsAndVolumes = memoize(
async (network: string): Promise<{ poolAddress: string, apy: number, volume: number }[]> => {
async (network: string, mode: 'stable' | 'crypto' = 'stable'): Promise<{ poolAddress: string, apy: number, volume: number }[]> => {
if (network === "aurora") return []; // Exclude Aurora

const url = `https://api.curve.fi/api/getFactoryAPYs-${network}`;
const url = `https://api.curve.fi/api/getFactoryAPYs/${network}/${mode}`;
const response = await axios.get(url, { validateStatus: () => true });

return response.data.data.poolDetails ?? [];
Expand All @@ -92,6 +92,21 @@ export const _getFactoryAPYsAndVolumes = memoize(
}
)

export const _getTotalVolumes = memoize(
async (network: string, mode: 'stable' | 'crypto' = 'stable'): Promise<{ totalVolumeUsd: number}> => {
if (network === "aurora") return {totalVolumeUsd: 0}; // Exclude Aurora

const url = `https://api.curve.fi/api/getFactoryAPYs/${network}/${mode}`;
const response = await axios.get(url, { validateStatus: () => true });

return response.data.data;
},
{
promise: true,
maxAge: 5 * 60 * 1000, // 5m
}
)

export const _getAllGauges = memoize(
async (): Promise<IDict<IGaugesDataFromApi>> => {
const url = `https://api.curve.fi/api/getAllGauges`;
Expand Down
12 changes: 8 additions & 4 deletions src/pools/PoolTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,12 @@ export class PoolTemplate {

private statsVolume = async (): Promise<string> => {
if ([56, 324, 1284, 2222, 8453, 42220, 1313161554].includes(curve.chainId)) { // Bsc || ZkSync || Moonbeam || Kava || Base || Celo || Aurora || Bsc
const [mainPoolsData, factoryPoolsData] = await Promise.all([
const _response = await Promise.all([
_getLegacyAPYsAndVolumes(curve.constants.NETWORK_NAME),
_getFactoryAPYsAndVolumes(curve.constants.NETWORK_NAME),
_getFactoryAPYsAndVolumes(curve.constants.NETWORK_NAME, 'stable'),
_getFactoryAPYsAndVolumes(curve.constants.NETWORK_NAME, 'crypto'),
]);
const [mainPoolsData, factoryPoolsData] = [_response[0], [..._response[1], ..._response[2]]];
if (this.id in mainPoolsData) {
return (mainPoolsData[this.id].volume ?? 0).toString();
}
Expand All @@ -441,10 +443,12 @@ export class PoolTemplate {

private statsBaseApy = async (): Promise<{ day: string, week: string }> => {
if ([56, 324, 1284, 2222, 8453, 42220, 1313161554].includes(curve.chainId)) { // Bsc || ZkSync || Moonbeam || Kava || Base || Celo || Aurora
const [mainPoolsData, factoryPoolsData] = await Promise.all([
const _response = await Promise.all([
_getLegacyAPYsAndVolumes(curve.constants.NETWORK_NAME),
_getFactoryAPYsAndVolumes(curve.constants.NETWORK_NAME),
_getFactoryAPYsAndVolumes(curve.constants.NETWORK_NAME, 'stable'),
_getFactoryAPYsAndVolumes(curve.constants.NETWORK_NAME, 'crypto'),
]);
const [mainPoolsData, factoryPoolsData] = [_response[0], [..._response[1], ..._response[2]]];
if (this.id in mainPoolsData) {
return {
day: mainPoolsData[this.id].apy.day.toString(),
Expand Down
29 changes: 15 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { Contract as MulticallContract } from "ethcall";
import BigNumber from 'bignumber.js';
import {IChainId, IDict, INetworkName, IRewardFromApi, REFERENCE_ASSET} from './interfaces';
import { curve, NETWORK_CONSTANTS } from "./curve.js";
import { _getFactoryAPYsAndVolumes, _getLegacyAPYsAndVolumes, _getAllPoolsFromApi, _getSubgraphData } from "./external-api.js";
import {
_getFactoryAPYsAndVolumes,
_getLegacyAPYsAndVolumes,
_getAllPoolsFromApi,
_getSubgraphData,
_getTotalVolumes,
} from "./external-api.js";
import ERC20Abi from './constants/abis/ERC20.json' assert { type: 'json' };
import { L2Networks } from './constants/L2Networks.js';

Expand Down Expand Up @@ -557,21 +563,16 @@ export const getVolume = async (network: INetworkName | IChainId = curve.chainId
if (["zksync", "moonbeam", "kava", "base", "celo", "aurora", "bsc"].includes(network)) {
const chainId = _getChainId(network);
if (curve.chainId !== chainId) throw Error("To get volume for ZkSync, Moonbeam, Kava, Base, Celo, Aurora or Bsc connect to the network first");
const [mainPoolsData, factoryPoolsData] = await Promise.all([
_getLegacyAPYsAndVolumes(network),
_getFactoryAPYsAndVolumes(network),


const [factoryPoolsData, cryptoPoolsData] = await Promise.all([
_getTotalVolumes(network, 'stable'),
_getTotalVolumes(network, 'crypto'),
]);
let volume = 0;
for (const id in mainPoolsData) {
volume += mainPoolsData[id].volume ?? 0;
}
for (const pool of factoryPoolsData) {
const lpToken = _getTokenAddressBySwapAddress(pool.poolAddress);
const lpPrice = lpToken ? await _getUsdRate(lpToken) : 0;
volume += pool.volume * lpPrice;
}
const stableVolume = factoryPoolsData.totalVolumeUsd;
const cryptoVolume = cryptoPoolsData.totalVolumeUsd;

return { totalVolume: volume, cryptoVolume: 0, cryptoShare: 0 }
return { totalVolume: stableVolume + cryptoVolume, cryptoVolume: cryptoVolume, cryptoShare: cryptoVolume/(stableVolume + cryptoVolume) }
}

const { totalVolume, cryptoVolume, cryptoShare } = await _getSubgraphData(network);
Expand Down

0 comments on commit 47cd668

Please sign in to comment.