Skip to content

Commit

Permalink
fix(@nftx/core): AMM apr logic requires all historical snapshots to work
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmellis committed Mar 18, 2024
1 parent 23df28d commit 860a9aa
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 341 deletions.
2 changes: 0 additions & 2 deletions packages/core/src/pools/fetchLiquidityPools/queryPoolData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const makeQueryPoolData =
s.inputTokens((token) => [token.id, token.symbol, token.name]),
s.hourlySnapshots(
q.liquidityPoolHourlySnapshots
.where((w) => [w.timestamp.gte(`${oneDayAgo}`)])
.first(1000)
.orderBy('hour')
.orderDirection('desc')
Expand All @@ -79,7 +78,6 @@ export const makeQueryPoolData =
),
s.dailySnapshots(
q.liquidityPoolDailySnapshots
.where((w) => [w.timestamp.gte(`${oneWeekAgo}`)])
.first(1000)
.orderBy('day')
.orderDirection('desc')
Expand Down
41 changes: 27 additions & 14 deletions packages/core/src/pools/fetchLiquidityPools/transformPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,35 @@ const transformPool = (

const periodFees = calculatePoolPeriodFees(receipts);

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

const dailyVolume = pool.hourlySnapshots.reduce((total, snapshot) => {
const value = BigInt(
snapshot.hourlyVolumeByTokenAmount[isWeth0 ? 0 : 1] ?? '0'
);
return total + value;
if (Number(snapshot.timestamp) >= oneDayAgo) {
const value = BigInt(
snapshot.hourlyVolumeByTokenAmount[isWeth0 ? 0 : 1] ?? '0'
);
return total + value;
}
return total;
}, Zero);

const weeklyVolume = pool.dailySnapshots.reduce((total, snapshot) => {
const value = BigInt(
snapshot.dailyVolumeByTokenAmount[isWeth0 ? 0 : 1] ?? '0'
);
return total + value;
if (Number(snapshot.timestamp) >= oneWeekAgo) {
const value = BigInt(
snapshot.dailyVolumeByTokenAmount[isWeth0 ? 0 : 1] ?? '0'
);
return total + value;
}
return total;
}, 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) {
// Only the 0.3% pool gets vault fees
dailyRevenue = [...receipts, ...premiumPaids].reduce((total, receipt) => {
if (receipt.date >= oneDayAgo) {
return total + receipt.amount;
Expand All @@ -111,12 +118,18 @@ const transformPool = (
}
return total;
}, Zero);
// All other pools only get the AMM fees
} else {
// All other pools only get the AMM fees
dailyRevenue = pool.hourlySnapshots.reduce((total, snapshot) => {
return total + parseEther(snapshot.hourlyTotalRevenueETH ?? '0');
if (Number(snapshot.timestamp) >= oneDayAgo) {
return total + parseEther(snapshot.hourlyTotalRevenueETH ?? '0');
}
return total;
}, Zero);
weeklyRevenue = pool.dailySnapshots.reduce((total, snapshot) => {
if (Number(snapshot.timestamp) >= oneWeekAgo) {
return total + parseEther(snapshot.dailyTotalRevenueETH ?? '0');
}
return total + parseEther(snapshot.dailyTotalRevenueETH ?? '0');
}, Zero);
}
Expand Down
Loading

0 comments on commit 860a9aa

Please sign in to comment.