Skip to content

Commit

Permalink
Add instantaneous deltaB to Bean entity
Browse files Browse the repository at this point in the history
  • Loading branch information
cujowolf committed Mar 6, 2024
1 parent 4f5921d commit 4bc6abc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
8 changes: 8 additions & 0 deletions projects/subgraph-bean/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type Bean @entity {
"Percent of supply in LP used for peg maintenance"
supplyInPegLP: BigDecimal!

"Instantaneous deltaB across all pools"
instantaneousDeltaB: BigInt!

"Cumulative volume of beans traded"
volume: BigInt!

Expand Down Expand Up @@ -72,6 +75,9 @@ type BeanHourlySnapshot @entity {
"Percent of supply in LP used for peg maintenance"
supplyInPegLP: BigDecimal!

"Instantaneous deltaB across all pools"
instantaneousDeltaB: BigInt!

"Cumulative volume in BEAN"
volume: BigInt!

Expand Down Expand Up @@ -123,6 +129,8 @@ type BeanDailySnapshot @entity {

"Percent of supply in LP used for peg maintenance"
supplyInPegLP: BigDecimal!
"Instantaneous deltaB across all pools"
instantaneousDeltaB: BigInt!
volume: BigInt!
volumeUSD: BigDecimal!
liquidityUSD: BigDecimal!
Expand Down
25 changes: 25 additions & 0 deletions projects/subgraph-bean/src/utils/Bean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function loadBean(token: string): Bean {
bean.supply = ZERO_BI;
bean.marketCap = ZERO_BD;
bean.supplyInPegLP = ZERO_BD;
bean.instantaneousDeltaB = ZERO_BI;
bean.volume = ZERO_BI;
bean.volumeUSD = ZERO_BD;
bean.liquidityUSD = ZERO_BD;
Expand All @@ -45,6 +46,7 @@ export function loadOrCreateBeanHourlySnapshot(token: string, timestamp: BigInt,
snapshot.supply = bean.supply;
snapshot.marketCap = bean.marketCap;
snapshot.supplyInPegLP = bean.supplyInPegLP;
snapshot.instantaneousDeltaB = ZERO_BI;
snapshot.volume = bean.volume;
snapshot.volumeUSD = bean.volumeUSD;
snapshot.liquidityUSD = bean.liquidityUSD;
Expand Down Expand Up @@ -73,6 +75,7 @@ export function loadOrCreateBeanDailySnapshot(token: string, timestamp: BigInt):
snapshot.supply = bean.supply;
snapshot.marketCap = bean.marketCap;
snapshot.supplyInPegLP = bean.supplyInPegLP;
snapshot.instantaneousDeltaB = ZERO_BI;
snapshot.volume = bean.volume;
snapshot.volumeUSD = bean.volumeUSD;
snapshot.liquidityUSD = bean.liquidityUSD;
Expand Down Expand Up @@ -196,3 +199,25 @@ export function updateBeanSupplyPegPercent(blockNumber: BigInt): void {
bean.save();
}
}

export function updateBeanDeltaB(token: string, blockNumber: BigInt, timestamp: BigInt): void {
let bean = loadBean(token);
let beanHourly = loadOrCreateBeanHourlySnapshot(token, timestamp, bean.lastSeason);
let beanDaily = loadOrCreateBeanDailySnapshot(token, timestamp);

let cumulativeDeltaB = ZERO_BI;

for (let i = 0; i < bean.pools.length; i++) {
let pool = loadOrCreatePool(bean.pools[i], blockNumber);
cumulativeDeltaB = cumulativeDeltaB.plus(pool.deltaBeans);
}

bean.instantaneousDeltaB = cumulativeDeltaB;
bean.save();

beanHourly.instantaneousDeltaB = cumulativeDeltaB;
beanHourly.save();

beanDaily.instantaneousDeltaB = cumulativeDeltaB;
beanDaily.save();
}
4 changes: 3 additions & 1 deletion projects/subgraph-bean/src/utils/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { Pool, PoolDailySnapshot, PoolHourlySnapshot } from "../../generated/schema";
import { dayFromTimestamp, hourFromTimestamp } from "../../../subgraph-core/utils/Dates";
import { emptyBigIntArray, ZERO_BD, ZERO_BI } from "../../../subgraph-core/utils/Decimals";
import { getBeanTokenAddress, loadBean } from "./Bean";
import { getBeanTokenAddress, loadBean, updateBeanDeltaB } from "./Bean";
import { checkPoolCross } from "./Cross";

export function loadOrCreatePool(poolAddress: string, blockNumber: BigInt): Pool {
Expand Down Expand Up @@ -132,6 +132,8 @@ export function updatePoolValues(
poolDaily.utilization = poolDaily.deltaVolumeUSD.div(poolDaily.liquidityUSD);
poolDaily.updatedAt = timestamp;
poolDaily.save();

updateBeanDeltaB(pool.bean, blockNumber, timestamp);
}

export function incrementPoolCross(poolAddress: string, timestamp: BigInt, blockNumber: BigInt): void {
Expand Down

0 comments on commit 4bc6abc

Please sign in to comment.