diff --git a/projects/subgraph-beanstalk/schema.graphql b/projects/subgraph-beanstalk/schema.graphql index ed24c2e57c..1b8d066345 100644 --- a/projects/subgraph-beanstalk/schema.graphql +++ b/projects/subgraph-beanstalk/schema.graphql @@ -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" @@ -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" diff --git a/projects/subgraph-beanstalk/src/GaugeHandler.ts b/projects/subgraph-beanstalk/src/GaugeHandler.ts index ade0c189c2..9981d29e81 100644 --- a/projects/subgraph-beanstalk/src/GaugeHandler.ts +++ b/projects/subgraph-beanstalk/src/GaugeHandler.ts @@ -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 { @@ -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 { @@ -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 { @@ -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); } diff --git a/projects/subgraph-beanstalk/src/utils/SiloEntities.ts b/projects/subgraph-beanstalk/src/utils/SiloEntities.ts index 11be8b384a..9a6b9d2fde 100644 --- a/projects/subgraph-beanstalk/src/utils/SiloEntities.ts +++ b/projects/subgraph-beanstalk/src/utils/SiloEntities.ts @@ -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"; @@ -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 {