Skip to content

Commit

Permalink
subgraph: whitelist settings daily/hourly snapshot for gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking committed Apr 10, 2024
1 parent 51c08d7 commit 2ffdc8b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
6 changes: 4 additions & 2 deletions projects/subgraph-beanstalk/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ type WhitelistTokenSetting @entity {

type WhitelistTokenHourlySnapshot @entity {
"Token address - Season"
id: Bytes!
id: ID!
"The season for this snapshot"
season: Int!
"WhitelistTokenSetting associated with this snapshot"
token: WhitelistTokenSetting!
"Encoded BDV selector"
Expand All @@ -411,7 +413,7 @@ type WhitelistTokenHourlySnapshot @entity {

type WhitelistTokenDailySnapshot @entity {
"Token address - Unix Timestamp"
id: Bytes!
id: ID!
"WhitelistTokenSetting associated with this snapshot"
token: WhitelistTokenSetting!
"Encoded BDV selector"
Expand Down
23 changes: 21 additions & 2 deletions projects/subgraph-beanstalk/src/GaugeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import {
} from "../generated/BIP42-SeedGauge/Beanstalk";
import { handleRateChange } from "./utils/Field";
import { loadBeanstalk } from "./utils/Beanstalk";
import { loadSilo, loadSiloHourlySnapshot, loadSiloDailySnapshot, loadWhitelistTokenSetting } from "./utils/SiloEntities";
import {
loadSilo,
loadSiloHourlySnapshot,
loadSiloDailySnapshot,
loadWhitelistTokenSetting,
loadWhitelistTokenDailySnapshot,
loadWhitelistTokenHourlySnapshot
} from "./utils/SiloEntities";
import { Address } from "@graphprotocol/graph-ts";

function currentSeason(beanstalk: Address): i32 {
Expand Down Expand Up @@ -48,7 +55,13 @@ export function handleGaugePointChange(event: GaugePointChange): void {
siloSettings.gaugePoints = event.params.gaugePoints;
siloSettings.updatedAt = event.block.timestamp;
siloSettings.save();
// TODO: daily

let whitelistHourly = loadWhitelistTokenHourlySnapshot(event.params.token, event.params.season.toI32(), event.block.timestamp);
let whitelistDaily = loadWhitelistTokenDailySnapshot(event.params.token, event.block.timestamp);
whitelistHourly.gaugePoints = event.params.gaugePoints;
whitelistDaily.gaugePoints = event.params.gaugePoints;
whitelistHourly.save();
whitelistDaily.save();
}

export function handleUpdateAverageStalkPerBdvPerSeason(event: UpdateAverageStalkPerBdvPerSeason): void {
Expand Down Expand Up @@ -111,6 +124,9 @@ export function handleWhitelistToken_BIP42(event: WhitelistToken): void {
siloSettings.optimalPercentDepositedBdv = event.params.optimalPercentDepositedBdv;
siloSettings.updatedAt = event.block.timestamp;
siloSettings.save();

loadWhitelistTokenHourlySnapshot(event.params.token, currentSeason(event.address), event.block.timestamp);
loadWhitelistTokenDailySnapshot(event.params.token, event.block.timestamp);
}

export function handleUpdateGaugeSettings(event: UpdateGaugeSettings): void {
Expand All @@ -120,4 +136,7 @@ export function handleUpdateGaugeSettings(event: UpdateGaugeSettings): void {
siloSettings.optimalPercentDepositedBdv = event.params.optimalPercentDepositedBdv;
siloSettings.updatedAt = event.block.timestamp;
siloSettings.save();

loadWhitelistTokenHourlySnapshot(event.params.token, currentSeason(event.address), event.block.timestamp);
loadWhitelistTokenDailySnapshot(event.params.token, event.block.timestamp);
}
49 changes: 49 additions & 0 deletions projects/subgraph-beanstalk/src/utils/SiloEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
SiloAssetHourlySnapshot,
SiloAsset,
WhitelistTokenSetting,
WhitelistTokenHourlySnapshot,
WhitelistTokenDailySnapshot,
TokenYield
} from "../../generated/schema";
import { BEANSTALK, UNRIPE_BEAN, UNRIPE_BEAN_3CRV } from "../../../subgraph-core/utils/Constants";
Expand Down Expand Up @@ -205,6 +207,53 @@ export function loadWhitelistTokenSetting(token: Address): WhitelistTokenSetting
return setting as WhitelistTokenSetting;
}

export function loadWhitelistTokenHourlySnapshot(token: Address, season: i32, timestamp: BigInt): WhitelistTokenHourlySnapshot {
let hour = hourFromTimestamp(timestamp);
let id = token.toHexString() + "-" + season.toString();
let snapshot = WhitelistTokenHourlySnapshot.load(id);
if (snapshot == null) {
let setting = loadWhitelistTokenSetting(token);
snapshot = new WhitelistTokenHourlySnapshot(id);
snapshot.season = season;
snapshot.token = setting.id;
snapshot.selector = setting.selector;
snapshot.gpSelector = setting.gpSelector;
snapshot.lwSelector = setting.lwSelector;
snapshot.stalkEarnedPerSeason = setting.stalkEarnedPerSeason;
snapshot.stalkIssuedPerBdv = setting.stalkIssuedPerBdv;
snapshot.milestoneSeason = setting.milestoneSeason;
snapshot.gaugePoints = setting.gaugePoints;
snapshot.optimalPercentDepositedBdv = setting.optimalPercentDepositedBdv;
snapshot.createdAt = BigInt.fromString(hour);
snapshot.updatedAt = ZERO_BI;
snapshot.save();
}
return snapshot as WhitelistTokenHourlySnapshot;
}

export function loadWhitelistTokenDailySnapshot(token: Address, timestamp: BigInt): WhitelistTokenDailySnapshot {
let day = dayFromTimestamp(timestamp);
let id = token.toHexString() + "-" + day.toString();
let snapshot = WhitelistTokenDailySnapshot.load(id);
if (snapshot == null) {
let setting = loadWhitelistTokenSetting(token);
snapshot = new WhitelistTokenDailySnapshot(id);
snapshot.token = setting.id;
snapshot.selector = setting.selector;
snapshot.gpSelector = setting.gpSelector;
snapshot.lwSelector = setting.lwSelector;
snapshot.stalkEarnedPerSeason = setting.stalkEarnedPerSeason;
snapshot.stalkIssuedPerBdv = setting.stalkIssuedPerBdv;
snapshot.milestoneSeason = setting.milestoneSeason;
snapshot.gaugePoints = setting.gaugePoints;
snapshot.optimalPercentDepositedBdv = setting.optimalPercentDepositedBdv;
snapshot.createdAt = BigInt.fromString(day);
snapshot.updatedAt = ZERO_BI;
snapshot.save();
}
return snapshot as WhitelistTokenDailySnapshot;
}

/* ===== Deposit Entities ===== */

export function loadSiloDeposit(account: Address, token: Address, season: BigInt): SiloDeposit {
Expand Down

0 comments on commit 2ffdc8b

Please sign in to comment.