Skip to content

Commit

Permalink
chore(); eigenlayer; create and fill in snapshots for days/hours with…
Browse files Browse the repository at this point in the history
… no events (#2430)
  • Loading branch information
dhruv-chauhan authored Dec 5, 2023
1 parent 18a4325 commit 2ab2d26
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 44 deletions.
2 changes: 1 addition & 1 deletion deployment/deployment.json
Original file line number Diff line number Diff line change
Expand Up @@ -9826,7 +9826,7 @@
"status": "prod",
"versions": {
"schema": "2.1.1",
"subgraph": "1.1.0",
"subgraph": "1.2.0",
"methodology": "1.0.0"
},
"files": {
Expand Down
2 changes: 2 additions & 0 deletions subgraphs/eigenlayer/src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export const MAX_UINT = BigInt.fromI32(2).times(BigInt.fromI32(255));
export const SECONDS_PER_HOUR = 60 * 60;
export const SECONDS_PER_DAY = 60 * 60 * 24;

export const ETHEREUM_AVG_BLOCKS_PER_DAY = 7200;

////////////////////////
/// Protocol Specific //
////////////////////////
Expand Down
74 changes: 39 additions & 35 deletions subgraphs/eigenlayer/src/common/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PoolType,
ProtocolType,
} from "./constants";
import { getDaysSinceEpoch, getHoursSinceEpoch } from "./utils";
import { fetchTokenDecimals, fetchTokenName, fetchTokenSymbol } from "./tokens";
import { getUsdPricePerToken } from "../prices";
import { Versions } from "../versions";
Expand Down Expand Up @@ -107,14 +106,14 @@ export function getOrCreateProtocol(): Protocol {
}

export function getOrCreateUsageMetricsDailySnapshot(
event: ethereum.Event
day: i32,
block: ethereum.Block | null = null
): UsageMetricsDailySnapshot {
const dayId = getDaysSinceEpoch(event.block.timestamp.toI32());
let snapshot = UsageMetricsDailySnapshot.load(Bytes.fromI32(dayId));
let snapshot = UsageMetricsDailySnapshot.load(Bytes.fromI32(day));

if (!snapshot) {
snapshot = new UsageMetricsDailySnapshot(Bytes.fromI32(dayId));
snapshot.day = dayId;
snapshot = new UsageMetricsDailySnapshot(Bytes.fromI32(day));
snapshot.day = day;
snapshot.protocol = NetworkConfigs.getFactoryAddress();

snapshot.dailyActiveDepositors = INT_ZERO;
Expand All @@ -131,47 +130,49 @@ export function getOrCreateUsageMetricsDailySnapshot(
snapshot.cumulativeTransactionCount = INT_ZERO;
snapshot.totalPoolCount = INT_ZERO;

snapshot.timestamp = event.block.timestamp;
snapshot.blockNumber = event.block.number;
snapshot.save();
if (block) {
snapshot.timestamp = block.timestamp;
snapshot.blockNumber = block.number;
}
}

return snapshot;
}

export function getOrCreateUsageMetricsHourlySnapshot(
event: ethereum.Event
hour: i32,
block: ethereum.Block | null = null
): UsageMetricsHourlySnapshot {
const hourId = getHoursSinceEpoch(event.block.timestamp.toI32());
let snapshot = UsageMetricsHourlySnapshot.load(Bytes.fromI32(hourId));
let snapshot = UsageMetricsHourlySnapshot.load(Bytes.fromI32(hour));

if (!snapshot) {
snapshot = new UsageMetricsHourlySnapshot(Bytes.fromI32(hourId));
snapshot.hour = hourId;
snapshot = new UsageMetricsHourlySnapshot(Bytes.fromI32(hour));
snapshot.hour = hour;
snapshot.protocol = NetworkConfigs.getFactoryAddress();

snapshot.hourlyActiveUsers = INT_ZERO;
snapshot.cumulativeUniqueUsers = INT_ZERO;
snapshot.hourlyTransactionCount = INT_ZERO;
snapshot.cumulativeTransactionCount = INT_ZERO;

snapshot.timestamp = event.block.timestamp;
snapshot.blockNumber = event.block.number;
snapshot.save();
if (block) {
snapshot.timestamp = block.timestamp;
snapshot.blockNumber = block.number;
}
}

return snapshot;
}

export function getOrCreateFinancialsDailySnapshot(
event: ethereum.Event
day: i32,
block: ethereum.Block | null = null
): FinancialsDailySnapshot {
const dayId = getDaysSinceEpoch(event.block.timestamp.toI32());
let snapshot = FinancialsDailySnapshot.load(Bytes.fromI32(dayId));
let snapshot = FinancialsDailySnapshot.load(Bytes.fromI32(day));

if (!snapshot) {
snapshot = new FinancialsDailySnapshot(Bytes.fromI32(dayId));
snapshot.day = dayId;
snapshot = new FinancialsDailySnapshot(Bytes.fromI32(day));
snapshot.day = day;
snapshot.protocol = NetworkConfigs.getFactoryAddress();

snapshot.totalValueLockedUSD = BIGDECIMAL_ZERO;
Expand All @@ -190,9 +191,10 @@ export function getOrCreateFinancialsDailySnapshot(
snapshot.dailyNetVolumeUSD = BIGDECIMAL_ZERO;
snapshot.netVolumeUSD = BIGDECIMAL_ZERO;

snapshot.timestamp = event.block.timestamp;
snapshot.blockNumber = event.block.number;
snapshot.save();
if (block) {
snapshot.timestamp = block.timestamp;
snapshot.blockNumber = block.number;
}
}
return snapshot;
}
Expand Down Expand Up @@ -256,9 +258,9 @@ export function getPool(poolAddress: Address): Pool {

export function getOrCreatePoolDailySnapshot(
poolAddress: Address,
event: ethereum.Event
day: i32,
block: ethereum.Block | null = null
): PoolDailySnapshot {
const day = getDaysSinceEpoch(event.block.timestamp.toI32());
const id = Bytes.empty()
.concat(poolAddress)
.concat(Bytes.fromUTF8("-"))
Expand Down Expand Up @@ -306,18 +308,19 @@ export function getOrCreatePoolDailySnapshot(
snapshot.dailyTransactionCount = INT_ZERO;
snapshot.cumulativeTransactionCount = INT_ZERO;

snapshot.timestamp = event.block.timestamp;
snapshot.blockNumber = event.block.number;
snapshot.save();
if (block) {
snapshot.timestamp = block.timestamp;
snapshot.blockNumber = block.number;
}
}
return snapshot;
}

export function getOrCreatePoolHourlySnapshot(
poolAddress: Address,
event: ethereum.Event
hour: i32,
block: ethereum.Block | null = null
): PoolHourlySnapshot {
const hour = getHoursSinceEpoch(event.block.timestamp.toI32());
const id = Bytes.empty()
.concat(poolAddress)
.concat(Bytes.fromUTF8("-"))
Expand All @@ -340,9 +343,10 @@ export function getOrCreatePoolHourlySnapshot(
snapshot.inputTokenBalances = [BIGINT_ZERO];
snapshot.inputTokenBalancesUSD = [BIGDECIMAL_ZERO];

snapshot.timestamp = event.block.timestamp;
snapshot.blockNumber = event.block.number;
snapshot.save();
if (block) {
snapshot.timestamp = block.timestamp;
snapshot.blockNumber = block.number;
}
}
return snapshot;
}
Expand Down
27 changes: 20 additions & 7 deletions subgraphs/eigenlayer/src/common/snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import { INT_ONE } from "./constants";
import {
addToArrayAtIndex,
bigIntToBigDecimal,
fillInMissingPoolDailySnapshots,
fillInMissingUsageMetricsDailySnapshots,
fillInMissingFinancialsDailySnapshots,
getDaysSinceEpoch,
getHoursSinceEpoch,
} from "./utils";
Expand All @@ -25,7 +28,8 @@ export function updateFinancialsDailySnapshot(
amount: BigInt,
event: ethereum.Event
): void {
const snapshot = getOrCreateFinancialsDailySnapshot(event);
const day = getDaysSinceEpoch(event.block.timestamp.toI32());
const snapshot = getOrCreateFinancialsDailySnapshot(day, event.block);
const protocol = getOrCreateProtocol();
const token = getOrCreateToken(tokenAddress, event);
const amountUSD = bigIntToBigDecimal(amount, token.decimals).times(
Expand Down Expand Up @@ -56,17 +60,18 @@ export function updateFinancialsDailySnapshot(
snapshot.timestamp = event.block.timestamp;
snapshot.blockNumber = event.block.number;

fillInMissingFinancialsDailySnapshots(day);
snapshot.save();
}

export function updateUsageMetricsHourlySnapshot(
accountAddress: Address,
event: ethereum.Event
): void {
const snapshot = getOrCreateUsageMetricsHourlySnapshot(event);
const hour = getHoursSinceEpoch(event.block.timestamp.toI32());
const snapshot = getOrCreateUsageMetricsHourlySnapshot(hour, event.block);
const protocol = getOrCreateProtocol();

const hour = getHoursSinceEpoch(event.block.timestamp.toI32());
const id = Bytes.empty()
.concat(Bytes.fromUTF8("hourly"))
.concat(Bytes.fromI32(hour))
Expand All @@ -92,7 +97,8 @@ export function updateUsageMetricsDailySnapshot(
eventID: Bytes,
event: ethereum.Event
): void {
const snapshot = getOrCreateUsageMetricsDailySnapshot(event);
const day = getDaysSinceEpoch(event.block.timestamp.toI32());
const snapshot = getOrCreateUsageMetricsDailySnapshot(day, event.block);
const protocol = getOrCreateProtocol();

snapshot.cumulativeUniqueDepositors = protocol.cumulativeUniqueDepositors;
Expand All @@ -103,7 +109,6 @@ export function updateUsageMetricsDailySnapshot(
snapshot.cumulativeTransactionCount = protocol.cumulativeTransactionCount;
snapshot.totalPoolCount = protocol.totalPoolCount;

const day = getDaysSinceEpoch(event.block.timestamp.toI32());
const id = Bytes.empty()
.concat(Bytes.fromUTF8("daily"))
.concat(Bytes.fromI32(day))
Expand Down Expand Up @@ -133,6 +138,7 @@ export function updateUsageMetricsDailySnapshot(
snapshot.timestamp = event.block.timestamp;
snapshot.blockNumber = event.block.number;

fillInMissingUsageMetricsDailySnapshots(day);
snapshot.save();
account.save();
}
Expand All @@ -141,7 +147,12 @@ export function updatePoolHourlySnapshot(
poolAddress: Address,
event: ethereum.Event
): void {
const snapshot = getOrCreatePoolHourlySnapshot(poolAddress, event);
const hour = getHoursSinceEpoch(event.block.timestamp.toI32());
const snapshot = getOrCreatePoolHourlySnapshot(
poolAddress,
hour,
event.block
);
const pool = getPool(poolAddress);

snapshot.totalValueLockedUSD = pool.totalValueLockedUSD;
Expand All @@ -161,7 +172,8 @@ export function updatePoolDailySnapshot(
amount: BigInt,
event: ethereum.Event
): void {
const snapshot = getOrCreatePoolDailySnapshot(poolAddress, event);
const day = getDaysSinceEpoch(event.block.timestamp.toI32());
const snapshot = getOrCreatePoolDailySnapshot(poolAddress, day, event.block);
const pool = getPool(poolAddress);
const token = getOrCreateToken(tokenAddress, event);

Expand Down Expand Up @@ -215,5 +227,6 @@ export function updatePoolDailySnapshot(
snapshot.timestamp = event.block.timestamp;
snapshot.blockNumber = event.block.number;

fillInMissingPoolDailySnapshots(poolAddress, day);
snapshot.save();
}
Loading

0 comments on commit 2ab2d26

Please sign in to comment.