Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bean Subgraph - Split Cross Entity #642

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions projects/subgraph-bean/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type Bean @entity {
"Cumulative number of crosses"
crosses: Int!

"Detailed cross events during this snapshot"
crossEvents: [BeanCross!]! @derivedFrom(field: "bean")

"Last timestamp a cross was seen"
lastCross: BigInt!

Expand Down Expand Up @@ -100,7 +103,7 @@ type BeanHourlySnapshot @entity {
deltaCrosses: Int!

"Detailed cross events during this snapshot"
crossEvents: [Cross!]! @derivedFrom(field: "hourlySnapshot")
crossEvents: [BeanCross!]! @derivedFrom(field: "hourlySnapshot")

"Season associated with this snapshot"
season: Int!
Expand Down Expand Up @@ -130,24 +133,32 @@ type BeanDailySnapshot @entity {
deltaVolumeUSD: BigDecimal!
deltaLiquidityUSD: BigDecimal!
deltaCrosses: Int!
crossEvents: [Cross!]! @derivedFrom(field: "dailySnapshot")
crossEvents: [BeanCross!]! @derivedFrom(field: "dailySnapshot")
season: Int!
timestamp: BigInt!
blockNumber: BigInt!
}

type Cross @entity {
type BeanCross @entity {
id: ID!
pool: Pool!
bean: Bean!
price: BigDecimal!
timestamp: BigInt!
timeSinceLastCross: BigInt!
above: Boolean!
overall: Boolean!
hourlySnapshot: BeanHourlySnapshot!
dailySnapshot: BeanDailySnapshot!
poolHourlySnapshot: PoolHourlySnapshot!
poolDailySnapshot: PoolDailySnapshot!
}

type PoolCross @entity {
id: ID!
pool: Pool!
price: BigDecimal!
timestamp: BigInt!
timeSinceLastCross: BigInt!
above: Boolean!
hourlySnapshot: PoolHourlySnapshot!
dailySnapshot: PoolDailySnapshot!
}

type Pool @entity {
Expand All @@ -160,7 +171,9 @@ type Pool @entity {
volumeUSD: BigDecimal!
liquidityUSD: BigDecimal!
crosses: Int!
crossEvents: [Cross!]! @derivedFrom(field: "pool")
"Last timestamp a cross was seen for this pool"
lastCross: BigInt!
crossEvents: [PoolCross!]! @derivedFrom(field: "pool")
deltaBeans: BigInt!
hourlySnapshot: [PoolHourlySnapshot!]! @derivedFrom(field: "pool")
dailySnapshot: [PoolDailySnapshot!]! @derivedFrom(field: "pool")
Expand All @@ -182,7 +195,7 @@ type PoolHourlySnapshot @entity {
deltaVolumeUSD: BigDecimal!
deltaLiquidityUSD: BigDecimal!
deltaCrosses: Int!
crossEvents: [Cross!]! @derivedFrom(field: "poolHourlySnapshot")
crossEvents: [PoolCross!]! @derivedFrom(field: "hourlySnapshot")
season: Int!
createdAt: BigInt!
updatedAt: BigInt!
Expand All @@ -204,7 +217,7 @@ type PoolDailySnapshot @entity {
deltaVolumeUSD: BigDecimal!
deltaLiquidityUSD: BigDecimal!
deltaCrosses: Int!
crossEvents: [Cross!]! @derivedFrom(field: "poolDailySnapshot")
crossEvents: [PoolCross!]! @derivedFrom(field: "dailySnapshot")
season: Int!
createdAt: BigInt!
updatedAt: BigInt!
Expand Down
7 changes: 5 additions & 2 deletions projects/subgraph-bean/src/Bean3CRVHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { BEANSTALK_PRICE, BEAN_ERC20, CURVE_PRICE } from "../../subgraph-core/ut
import { toDecimal, ZERO_BD, ZERO_BI } from "../../subgraph-core/utils/Decimals";
import { getPoolLiquidityUSD, loadOrCreatePool, setPoolReserves, updatePoolPrice, updatePoolValues } from "./utils/Pool";
import { BeanstalkPrice } from "../generated/Bean3CRV/BeanstalkPrice";
import { checkBeanCross } from "./utils/Cross";

export function handleTokenExchange(event: TokenExchange): void {
handleSwap(
Expand Down Expand Up @@ -120,7 +121,8 @@ function handleLiquidityChange(
updateBeanValues(BEAN_ERC20.toHexString(), timestamp, beanPrice, ZERO_BI, volumeBean, volumeUSD, deltaLiquidityUSD);

updatePoolValues(poolAddress, timestamp, blockNumber, volumeBean, volumeUSD, deltaLiquidityUSD, curve.value.deltaB);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice, oldBeanPrice, beanPrice);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice);
checkBeanCross(BEAN_ERC20.toHexString(), timestamp, blockNumber, oldBeanPrice, beanPrice);
}

function handleSwap(
Expand Down Expand Up @@ -170,5 +172,6 @@ function handleSwap(
updateBeanValues(BEAN_ERC20.toHexString(), timestamp, beanPrice, ZERO_BI, volumeBean, volumeUSD, deltaLiquidityUSD);

updatePoolValues(poolAddress, timestamp, blockNumber, volumeBean, volumeUSD, deltaLiquidityUSD, curve.value.deltaB);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice, oldBeanPrice, beanPrice);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice);
checkBeanCross(BEAN_ERC20.toHexString(), timestamp, blockNumber, oldBeanPrice, beanPrice);
}
7 changes: 5 additions & 2 deletions projects/subgraph-bean/src/Bean3CRVHandler_V1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { loadOrCreatePool, setPoolReserves, updatePoolPrice, updatePoolValues }
import { CalculationsCurve } from "../generated/Bean3CRV-V1/CalculationsCurve";
import { Bean3CRV } from "../generated/Bean3CRV-V1/Bean3CRV";
import { ERC20 } from "../generated/Bean3CRV-V1/ERC20";
import { checkBeanCross } from "./utils/Cross";

export function handleTokenExchange(event: TokenExchange): void {
// Do not index post-exploit data
Expand Down Expand Up @@ -180,7 +181,8 @@ function handleLiquidityChange(

updateBeanValues(BEAN_ERC20_V1.toHexString(), timestamp, newPrice, ZERO_BI, volumeBean, volumeUSD, deltaLiquidityUSD);
updatePoolValues(poolAddress, timestamp, blockNumber, volumeBean, volumeUSD, deltaLiquidityUSD, deltaB);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice, oldBeanPrice, newPrice);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice);
checkBeanCross(BEAN_ERC20_V1.toHexString(), timestamp, blockNumber, oldBeanPrice, newPrice);
}

function handleSwap(
Expand Down Expand Up @@ -262,5 +264,6 @@ function handleSwap(
let volumeUSD = toDecimal(volumeBean).times(newPrice);
updateBeanValues(BEAN_ERC20_V1.toHexString(), timestamp, newPrice, ZERO_BI, volumeBean, volumeUSD, deltaLiquidityUSD);
updatePoolValues(poolAddress, timestamp, blockNumber, volumeBean, volumeUSD, deltaLiquidityUSD, deltaB);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice, oldBeanPrice, newPrice);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice);
checkBeanCross(BEAN_ERC20_V1.toHexString(), timestamp, blockNumber, oldBeanPrice, newPrice);
}
21 changes: 10 additions & 11 deletions projects/subgraph-bean/src/BeanWellHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BeanstalkPrice } from "../generated/BeanWETHCP2w/BeanstalkPrice";
import { AddLiquidity, RemoveLiquidity, RemoveLiquidityOneToken, Shift, Swap, Sync } from "../generated/BeanWETHCP2w/Well";
import { loadBean, updateBeanSupplyPegPercent, updateBeanValues } from "./utils/Bean";
import { getPoolLiquidityUSD, loadOrCreatePool, setPoolReserves, updatePoolPrice, updatePoolValues } from "./utils/Pool";
import { checkBeanCross } from "./utils/Cross";

export function handleAddLiquidity(event: AddLiquidity): void {
handleLiquidityChange(
Expand Down Expand Up @@ -94,15 +95,11 @@ function handleLiquidityChange(
let newPrice = toDecimal(wellPrice.value.price);
let deltaLiquidityUSD = toDecimal(wellPrice.value.liquidity).minus(startingLiquidity);

let volumeUSD =
deltaLiquidityUSD < ZERO_BD
? deltaLiquidityUSD.div(BigDecimal.fromString("2")).times(BigDecimal.fromString("-1"))
: deltaLiquidityUSD.div(BigDecimal.fromString("2"));
let volumeBean = BigInt.fromString(volumeUSD.div(newPrice).times(BigDecimal.fromString("1000000")).truncate(0).toString());

if (token0Amount !== ZERO_BI && token1Amount !== ZERO_BI) {
volumeUSD = ZERO_BD;
volumeBean = ZERO_BI;
let volumeUSD = ZERO_BD;
let volumeBean = ZERO_BI;
if (token0Amount == ZERO_BI || token1Amount == ZERO_BI) {
volumeUSD = deltaLiquidityUSD;
volumeBean = BigInt.fromString(volumeUSD.div(newPrice).times(BigDecimal.fromString("1000000")).truncate(0).toString());
}

setPoolReserves(poolAddress, wellPrice.value.balances, blockNumber);
Expand All @@ -119,7 +116,8 @@ function handleLiquidityChange(
);

updatePoolValues(poolAddress, timestamp, blockNumber, volumeBean, volumeUSD, deltaLiquidityUSD, wellPrice.value.deltaB);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice, oldBeanPrice, toDecimal(beanPrice.value.price));
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice);
checkBeanCross(BEAN_ERC20.toHexString(), timestamp, blockNumber, oldBeanPrice, toDecimal(beanPrice.value.price));
}

function handleSwapEvent(
Expand Down Expand Up @@ -164,5 +162,6 @@ function handleSwapEvent(
);

updatePoolValues(poolAddress, timestamp, blockNumber, volumeBean, volumeUSD, deltaLiquidityUSD, wellPrice.value.deltaB);
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice, oldBeanPrice, toDecimal(beanPrice.value.price));
updatePoolPrice(poolAddress, timestamp, blockNumber, newPrice);
checkBeanCross(BEAN_ERC20.toHexString(), timestamp, blockNumber, oldBeanPrice, toDecimal(beanPrice.value.price));
}
24 changes: 7 additions & 17 deletions projects/subgraph-bean/src/BeanstalkHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BeanstalkPrice } from "../generated/Beanstalk/BeanstalkPrice";
import { BEANSTALK_PRICE, BEAN_3CRV, BEAN_ERC20, BEAN_WETH_CP2_WELL, CURVE_PRICE } from "../../subgraph-core/utils/Constants";
import { ZERO_BD, ZERO_BI, toDecimal } from "../../subgraph-core/utils/Decimals";
import { CurvePrice } from "../generated/Beanstalk/CurvePrice";
import { checkBeanCross } from "./utils/Cross";

export function handleSunrise(event: Sunrise): void {
// Update the season for hourly and daily liquidity metrics
Expand Down Expand Up @@ -35,33 +36,22 @@ export function handleSunrise(event: Sunrise): void {

// Curve pool update
let curvePrice = beanstalkPrice.getCurve().price;
updatePoolPrice(BEAN_3CRV.toHexString(), event.block.timestamp, event.block.number, toDecimal(curvePrice), oldBeanPrice, beanPrice);
updatePoolPrice(BEAN_3CRV.toHexString(), event.block.timestamp, event.block.number, toDecimal(curvePrice));

// Curve pool update
let wellPrice = beanstalkPrice.getConstantProductWell(BEAN_WETH_CP2_WELL).price;
updatePoolPrice(
BEAN_WETH_CP2_WELL.toHexString(),
event.block.timestamp,
event.block.number,
toDecimal(wellPrice),
oldBeanPrice,
beanPrice
);
updatePoolPrice(BEAN_WETH_CP2_WELL.toHexString(), event.block.timestamp, event.block.number, toDecimal(wellPrice));

checkBeanCross(BEAN_ERC20.toHexString(), event.block.timestamp, event.block.number, oldBeanPrice, beanPrice);
} else {
// Pre Basin deployment - Use original Curve price contract to update on each season.
let curvePrice = CurvePrice.bind(CURVE_PRICE);
let curve = curvePrice.try_getCurve();

if (!curve.reverted) {
updateBeanValues(BEAN_ERC20.toHexString(), event.block.timestamp, toDecimal(curve.value.price), ZERO_BI, ZERO_BI, ZERO_BD, ZERO_BD);
updatePoolPrice(
BEAN_3CRV.toHexString(),
event.block.timestamp,
event.block.number,
toDecimal(curve.value.price),
oldBeanPrice,
toDecimal(curve.value.price)
);
updatePoolPrice(BEAN_3CRV.toHexString(), event.block.timestamp, event.block.number, toDecimal(curve.value.price));
checkBeanCross(BEAN_ERC20.toHexString(), event.block.timestamp, event.block.number, oldBeanPrice, toDecimal(curve.value.price));
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion projects/subgraph-bean/src/UniswapV2Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BEAN_ERC20_V1, WETH, WETH_USDC_PAIR } from "../../subgraph-core/utils/C
import { toBigInt, toDecimal, ZERO_BD, ZERO_BI } from "../../subgraph-core/utils/Decimals";
import { loadOrCreatePool, setPoolReserves, updatePoolPrice, updatePoolReserves, updatePoolValues } from "./utils/Pool";
import { loadOrCreateToken } from "./utils/Token";
import { checkBeanCross } from "./utils/Cross";

// export function handleMint(event: Mint): void {
// updatePoolReserves(event.address.toHexString(), event.params.amount0, event.params.amount1, event.block.number);
Expand Down Expand Up @@ -93,7 +94,9 @@ export function handleSync(event: Sync): void {

let currentBeanPrice = wethBalance.times(weth.lastPriceUSD).div(beanBalance);

updatePoolPrice(event.address.toHexString(), event.block.timestamp, event.block.number, currentBeanPrice, oldBeanPrice, currentBeanPrice);
updatePoolPrice(event.address.toHexString(), event.block.timestamp, event.block.number, currentBeanPrice);

checkBeanCross(BEAN_ERC20_V1.toHexString(), event.block.timestamp, event.block.number, oldBeanPrice, currentBeanPrice);

setPoolReserves(event.address.toHexString(), [reserves.value.value0, reserves.value.value1], event.block.number);

Expand Down
Loading
Loading