Skip to content

Commit

Permalink
fix: calculate pool revenue
Browse files Browse the repository at this point in the history
The daily/weekly pool revenue was showing as 0 up until now.
This was first due to it being in USD terms (and lacking a stable USD pair on testnet),
but also because the revenue values were based purely on the AMM pools, whereas most of NFTX
is based around vault fees rather than AMM fees.
So for the 0.3% pool we're now going to calculate the revenue by looking at the vault fees subgraph.
  • Loading branch information
jackmellis committed Nov 28, 2023
1 parent 6168a0d commit 1f43a47
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
14 changes: 14 additions & 0 deletions packages/core/src/pools/fetchLiquidityPools/fetchLiquidityPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import config from '@nftx/config';
import type { Address, LiquidityPool, Provider, Vault } from '@nftx/types';
import fetchPoolsSet from './fetchPoolsSet';
import stubMissingPools from './stubMissingPools';
import { fetchFeeReceipts } from '../../vaults';
import fetchPremiumPaids from '../fetchPremiumPaids';

const fetchLiquidityPools = async ({
network = config.network,
Expand All @@ -26,6 +28,16 @@ const fetchLiquidityPools = async ({
>[];
provider: Provider;
}): Promise<LiquidityPool[]> => {
const allVaultAddresses = vaults.map((v) => v.id);
const feeReceipts = await fetchFeeReceipts({
network,
vaultAddresses: allVaultAddresses,
});
const premiumPaids = await fetchPremiumPaids({
network,
vaultAddresses: allVaultAddresses,
});

const pools: LiquidityPool[] = [];
let lastId: Address | undefined;

Expand All @@ -38,6 +50,8 @@ const fetchLiquidityPools = async ({
poolIds,
vaultAddresses,
vaultIds,
feeReceipts,
premiumPaids,
});

pools.push(...morePools);
Expand Down
30 changes: 14 additions & 16 deletions packages/core/src/pools/fetchLiquidityPools/fetchPoolsSet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type {
Address,
LiquidityPool,
Vault,
VaultFeeReceipt,
import {
type Address,
type LiquidityPool,
type Vault,
type VaultFeeReceipt,
} from '@nftx/types';
import queryPoolData from './queryPoolData';
import transformPool from './transformPool';
import { addressEqual } from '@nftx/utils';
import fetchFeeReceipts from '../../vaults/fetchFeeReceipts';

const getVaultByTokens = <V extends Pick<Vault, 'id'>>({
inputTokens,
Expand All @@ -29,22 +28,17 @@ const fetchPoolsSet = async ({
poolIds,
vaultAddresses,
vaultIds,
feeReceipts: givenFeeReceipts,
feeReceipts,
premiumPaids: allPremiumPaids,
}: {
network: number;
vaults: Pick<Vault, 'id' | 'vaultId' | 'vTokenToEth' | 'createdAt'>[];
poolIds?: Address[];
vaultAddresses?: Address[];
vaultIds?: string[];
feeReceipts?: VaultFeeReceipt[];
feeReceipts: VaultFeeReceipt[];
premiumPaids: { vaultId: string; amount: bigint; date: number }[];
}) => {
const feeReceipts =
givenFeeReceipts ??
(await fetchFeeReceipts({
network,
vaultAddresses: vaults.map((v) => v.id),
}));

const data = await queryPoolData({
network,
vaults,
Expand All @@ -64,12 +58,16 @@ const fetchPoolsSet = async ({
const receipts = feeReceipts.filter(
(receipt) => receipt.vaultId === vault.vaultId
);
const premiumPaids = allPremiumPaids.filter(
(p) => p.vaultId === vault.vaultId
);
const p = transformPool(
network,
pool,
vault,
pool.openPositionCount,
receipts
receipts,
premiumPaids
);
return [...acc, p];
}, [] as LiquidityPool[]);
Expand Down
41 changes: 34 additions & 7 deletions packages/core/src/pools/fetchLiquidityPools/transformPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { LiquidityPoolsResponse } from './types';
import calcualateAprs from './calculateAprs';
import {
FeePercentage,
VAULT_FEE_TIER,
WETH_TOKEN,
WeiPerEther,
Zero,
Expand Down Expand Up @@ -41,7 +42,8 @@ const transformPool = (
pool: Pool,
vault: Pick<Vault, 'vaultId' | 'id' | 'vTokenToEth' | 'createdAt'>,
totalPositions: number,
receipts: VaultFeeReceipt[]
receipts: VaultFeeReceipt[],
premiumPaids: { amount: bigint; date: number }[]
): LiquidityPool => {
const fees = transformFees(pool.fees);
const tokens = pool.inputTokens.map(({ id, symbol, name }, i) => ({
Expand Down Expand Up @@ -79,18 +81,43 @@ const transformPool = (
);
return total + value;
}, Zero);
const dailyRevenue = pool.hourlySnapshots.reduce((total, snapshot) => {
return total + BigInt(snapshot.hourlyTotalRevenueETH ?? '0');
}, Zero);

const weeklyVolume = pool.dailySnapshots.reduce((total, snapshot) => {
const value = BigInt(
snapshot.dailyVolumeByTokenAmount[isWeth0 ? 0 : 1] ?? '0'
);
return total + value;
}, Zero);
const weeklyRevenue = pool.dailySnapshots.reduce((total, snapshot) => {
return total + BigInt(snapshot.dailyTotalRevenueETH ?? '0');
}, Zero);

const now = Math.floor(Date.now() / 1000);
const oneDayAgo = now - 60 * 60 * 24;
const oneWeekAgo = now - 60 * 60 * 24 * 7;
let dailyRevenue = Zero;
let weeklyRevenue = Zero;

// Only the 0.3% pool gets vault fees
if (feeTier === VAULT_FEE_TIER) {
dailyRevenue = [...receipts, ...premiumPaids].reduce((total, receipt) => {
if (receipt.date >= oneDayAgo) {
return total + receipt.amount;
}
return total;
}, Zero);
weeklyRevenue = [...receipts, ...premiumPaids].reduce((total, receipt) => {
if (receipt.date >= oneWeekAgo) {
return total + receipt.amount;
}
return total;
}, Zero);
// All other pools only get the AMM fees
} else {
dailyRevenue = pool.hourlySnapshots.reduce((total, snapshot) => {
return total + BigInt(snapshot.hourlyTotalRevenueETH ?? '0');
}, Zero);
weeklyRevenue = pool.dailySnapshots.reduce((total, snapshot) => {
return total + BigInt(snapshot.dailyTotalRevenueETH ?? '0');
}, Zero);
}

const eth = tokens[isWeth0 ? 0 : 1].balance;
const vToken = tokens[isWeth0 ? 1 : 0].balance;
Expand Down

0 comments on commit 1f43a47

Please sign in to comment.