From 0fa1bacef9985e863cbba958f146055f35b70b74 Mon Sep 17 00:00:00 2001 From: Soil King <157099073+soilking@users.noreply.github.com> Date: Thu, 23 May 2024 13:29:40 -0700 Subject: [PATCH 1/3] continue updating season on dewhitelisted pools --- projects/subgraph-bean/schema.graphql | 3 + .../subgraph-bean/src/BeanstalkHandler.ts | 82 +++++++++++++++---- projects/subgraph-bean/src/utils/Bean.ts | 21 ++++- .../subgraph-bean/tests/Whitelist.test.ts | 30 ++++++- 4 files changed, 116 insertions(+), 20 deletions(-) diff --git a/projects/subgraph-bean/schema.graphql b/projects/subgraph-bean/schema.graphql index 9826bfadd9..65378d66bc 100644 --- a/projects/subgraph-bean/schema.graphql +++ b/projects/subgraph-bean/schema.graphql @@ -52,6 +52,9 @@ type Bean @entity { "Pools that include this Bean" pools: [Pool!]! + "Dewhitelisted pools that include this Bean" + dewhitelistedPools: [Pool!]! + "Hourly snapshot of Bean data" hourlySnapshot: [BeanHourlySnapshot!]! @derivedFrom(field: "bean") diff --git a/projects/subgraph-bean/src/BeanstalkHandler.ts b/projects/subgraph-bean/src/BeanstalkHandler.ts index d6e6931fd1..763085b757 100644 --- a/projects/subgraph-bean/src/BeanstalkHandler.ts +++ b/projects/subgraph-bean/src/BeanstalkHandler.ts @@ -1,7 +1,19 @@ import { Address, BigInt } from "@graphprotocol/graph-ts"; import { Chop, DewhitelistToken, Reward, Sunrise } from "../generated/Beanstalk/Beanstalk"; -import { getBeanTokenAddress, loadBean, updateBeanSeason, updateBeanSupplyPegPercent, updateBeanTwa, updateBeanValues } from "./utils/Bean"; -import { loadOrCreatePool, updatePoolPrice, updatePoolSeason, updatePoolValues } from "./utils/Pool"; +import { + getBeanTokenAddress, + loadBean, + updateBeanSeason, + updateBeanSupplyPegPercent, + updateBeanTwa, + updateBeanValues +} from "./utils/Bean"; +import { + loadOrCreatePool, + updatePoolPrice, + updatePoolSeason, + updatePoolValues +} from "./utils/Pool"; import { BeanstalkPrice } from "../generated/Beanstalk/BeanstalkPrice"; import { BEANSTALK_PRICE, @@ -33,9 +45,13 @@ export function handleSunrise(event: Sunrise): void { let bean = loadBean(beanToken); let oldBeanPrice = bean.price; - let oldBeanLiquidity = bean.liquidityUSD; for (let i = 0; i < bean.pools.length; i++) { - updatePoolSeason(bean.pools[i], event.block.timestamp, event.block.number, event.params.season.toI32()); + updatePoolSeason( + bean.pools[i], + event.block.timestamp, + event.block.number, + event.params.season.toI32() + ); } // Fetch price from price contract to capture any non-bean token price movevements @@ -51,7 +67,15 @@ export function handleSunrise(event: Sunrise): void { let beanCurve = loadOrCreatePool(BEAN_3CRV.toHexString(), event.block.number); if (!curve.reverted) { - updateBeanValues(BEAN_ERC20.toHexString(), event.block.timestamp, toDecimal(curve.value.price), ZERO_BI, ZERO_BI, ZERO_BD, ZERO_BD); + updateBeanValues( + BEAN_ERC20.toHexString(), + event.block.timestamp, + toDecimal(curve.value.price), + ZERO_BI, + ZERO_BI, + ZERO_BD, + ZERO_BD + ); updatePoolValues( BEAN_3CRV.toHexString(), event.block.timestamp, @@ -61,8 +85,19 @@ export function handleSunrise(event: Sunrise): void { toDecimal(curve.value.liquidity).minus(beanCurve.liquidityUSD), curve.value.deltaB ); - 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)); + 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) + ); } } } else { @@ -107,7 +142,13 @@ export function handleSunrise(event: Sunrise): void { ZERO_BD, totalLiquidity.minus(bean.liquidityUSD) ); - checkBeanCross(BEAN_ERC20_V1.toHexString(), event.block.timestamp, event.block.number, bean.price, totalPrice); + checkBeanCross( + BEAN_ERC20_V1.toHexString(), + event.block.timestamp, + event.block.number, + bean.price, + totalPrice + ); updateBeanTwa(event.block.timestamp, event.block.number); } } @@ -116,10 +157,14 @@ export function handleSunrise(event: Sunrise): void { export function handleDewhitelistToken(event: DewhitelistToken): void { let bean = loadBean(getBeanTokenAddress(event.block.number)); let index = bean.pools.indexOf(event.params.token.toHexString()); - const newPools = bean.pools; - newPools.splice(index, 1); - bean.pools = newPools; - bean.save(); + if (index >= 0) { + const newPools = bean.pools; + const newDewhitelistedPools = bean.dewhitelistedPools; + newDewhitelistedPools.push(newPools.splice(index, 1)[0]); + bean.pools = newPools; + bean.dewhitelistedPools = newDewhitelistedPools; + bean.save(); + } } // POST REPLANT TWA DELTAB // @@ -132,8 +177,17 @@ export function handleMetapoolOracle(event: MetapoolOracle): void { export function handleWellOracle(event: WellOracle): void { setRawWellReserves(event); - setTwaLast(event.params.well.toHexString(), decodeCumulativeWellReserves(event.params.cumulativeReserves), event.block.timestamp); - setWellTwa(event.params.well.toHexString(), event.params.deltaB, event.block.timestamp, event.block.number); + setTwaLast( + event.params.well.toHexString(), + decodeCumulativeWellReserves(event.params.cumulativeReserves), + event.block.timestamp + ); + setWellTwa( + event.params.well.toHexString(), + event.params.deltaB, + event.block.timestamp, + event.block.number + ); updateBeanTwa(event.block.timestamp, event.block.number); } diff --git a/projects/subgraph-bean/src/utils/Bean.ts b/projects/subgraph-bean/src/utils/Bean.ts index 13c95023ad..ead4f9c236 100644 --- a/projects/subgraph-bean/src/utils/Bean.ts +++ b/projects/subgraph-bean/src/utils/Bean.ts @@ -32,12 +32,17 @@ export function loadBean(token: string): Bean { bean.lastCross = ZERO_BI; bean.lastSeason = token == BEAN_ERC20.toHexString() ? 6074 : 0; bean.pools = []; + bean.dewhitelistedPools = []; bean.save(); } return bean as Bean; } -export function loadOrCreateBeanHourlySnapshot(token: string, timestamp: BigInt, season: i32): BeanHourlySnapshot { +export function loadOrCreateBeanHourlySnapshot( + token: string, + timestamp: BigInt, + season: i32 +): BeanHourlySnapshot { let id = token + "-" + season.toString(); let snapshot = BeanHourlySnapshot.load(id); if (snapshot == null) { @@ -183,7 +188,9 @@ export function calcLiquidityWeightedBeanPrice(token: string): BigDecimal { } export function getBeanTokenAddress(blockNumber: BigInt): string { - return blockNumber < BigInt.fromString("15278082") ? BEAN_ERC20_V1.toHexString() : BEAN_ERC20.toHexString(); + return blockNumber < BigInt.fromString("15278082") + ? BEAN_ERC20_V1.toHexString() + : BEAN_ERC20.toHexString(); } export function updateBeanSupplyPegPercent(blockNumber: BigInt): void { @@ -247,7 +254,15 @@ export function updateBeanAfterPoolSwap( } updateBeanSupplyPegPercent(blockNumber); - updateBeanValues(BEAN_ERC20.toHexString(), timestamp, beanPrice, ZERO_BI, volumeBean, volumeUSD, deltaLiquidityUSD); + updateBeanValues( + BEAN_ERC20.toHexString(), + timestamp, + beanPrice, + ZERO_BI, + volumeBean, + volumeUSD, + deltaLiquidityUSD + ); checkBeanCross(BEAN_ERC20.toHexString(), timestamp, blockNumber, oldBeanPrice, beanPrice); } } diff --git a/projects/subgraph-bean/tests/Whitelist.test.ts b/projects/subgraph-bean/tests/Whitelist.test.ts index 48b6ded964..141f2af384 100644 --- a/projects/subgraph-bean/tests/Whitelist.test.ts +++ b/projects/subgraph-bean/tests/Whitelist.test.ts @@ -1,6 +1,19 @@ -import { beforeEach, beforeAll, afterEach, assert, clearStore, describe, test } from "matchstick-as/assembly/index"; +import { + beforeEach, + beforeAll, + afterEach, + assert, + clearStore, + describe, + test +} from "matchstick-as/assembly/index"; import { loadBean } from "../src/utils/Bean"; -import { BEAN_3CRV, BEAN_ERC20, BEAN_WETH_CP2_WELL, BEAN_WETH_CP2_WELL_BLOCK } from "../../subgraph-core/utils/Constants"; +import { + BEAN_3CRV, + BEAN_ERC20, + BEAN_WETH_CP2_WELL, + BEAN_WETH_CP2_WELL_BLOCK +} from "../../subgraph-core/utils/Constants"; import { handleDewhitelistToken } from "../src/BeanstalkHandler"; import { createDewhitelistTokenEvent } from "./event-mocking/Beanstalk"; import { setWhitelistedPools } from "./entity-mocking/MockBean"; @@ -18,6 +31,17 @@ describe("Whitelisting", () => { event.block.number = BEAN_WETH_CP2_WELL_BLOCK; handleDewhitelistToken(event); - assert.fieldEquals("Bean", BEAN_ERC20.toHexString(), "pools", "[" + BEAN_WETH_CP2_WELL.toHexString() + "]"); + assert.fieldEquals( + "Bean", + BEAN_ERC20.toHexString(), + "pools", + "[" + BEAN_WETH_CP2_WELL.toHexString() + "]" + ); + assert.fieldEquals( + "Bean", + BEAN_ERC20.toHexString(), + "dewhitelistedPools", + "[" + BEAN_3CRV.toHexString() + "]" + ); }); }); From 336280a7623077aa65ec5c7f09ceebc862450a57 Mon Sep 17 00:00:00 2001 From: Soil King <157099073+soilking@users.noreply.github.com> Date: Thu, 23 May 2024 14:05:29 -0700 Subject: [PATCH 2/3] update pool season on dewhitelisted pool --- projects/subgraph-bean/src/BeanstalkHandler.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/projects/subgraph-bean/src/BeanstalkHandler.ts b/projects/subgraph-bean/src/BeanstalkHandler.ts index 763085b757..4322a372df 100644 --- a/projects/subgraph-bean/src/BeanstalkHandler.ts +++ b/projects/subgraph-bean/src/BeanstalkHandler.ts @@ -54,6 +54,15 @@ export function handleSunrise(event: Sunrise): void { ); } + for (let i = 0; i < bean.dewhitelistedPools.length; i++) { + updatePoolSeason( + bean.dewhitelistedPools[i], + event.block.timestamp, + event.block.number, + event.params.season.toI32() + ); + } + // Fetch price from price contract to capture any non-bean token price movevements if (event.params.season > BigInt.fromI32(6074)) { // Attempt to pull from Beanstalk Price contract first for the overall Bean price update From b83f07dc2bada6bbb39b4372a0ca36992809a608 Mon Sep 17 00:00:00 2001 From: Soil King <157099073+soilking@users.noreply.github.com> Date: Thu, 23 May 2024 14:52:13 -0700 Subject: [PATCH 3/3] adjust prettier settings for subgraphs --- .prettierrc | 10 ++- .../subgraph-bean/src/BeanstalkHandler.ts | 76 +++---------------- 2 files changed, 19 insertions(+), 67 deletions(-) diff --git a/.prettierrc b/.prettierrc index 80645c6ebc..43834780bb 100644 --- a/.prettierrc +++ b/.prettierrc @@ -2,5 +2,13 @@ "printWidth": 100, "singleQuote": false, "semi": true, - "trailingComma": "none" + "trailingComma": "none", + "overrides": [ + { + "files": "projects/subgraph-*/**", + "options": { + "printWidth": 140 + } + } + ] } diff --git a/projects/subgraph-bean/src/BeanstalkHandler.ts b/projects/subgraph-bean/src/BeanstalkHandler.ts index 4322a372df..32766f630f 100644 --- a/projects/subgraph-bean/src/BeanstalkHandler.ts +++ b/projects/subgraph-bean/src/BeanstalkHandler.ts @@ -1,19 +1,7 @@ import { Address, BigInt } from "@graphprotocol/graph-ts"; import { Chop, DewhitelistToken, Reward, Sunrise } from "../generated/Beanstalk/Beanstalk"; -import { - getBeanTokenAddress, - loadBean, - updateBeanSeason, - updateBeanSupplyPegPercent, - updateBeanTwa, - updateBeanValues -} from "./utils/Bean"; -import { - loadOrCreatePool, - updatePoolPrice, - updatePoolSeason, - updatePoolValues -} from "./utils/Pool"; +import { getBeanTokenAddress, loadBean, updateBeanSeason, updateBeanSupplyPegPercent, updateBeanTwa, updateBeanValues } from "./utils/Bean"; +import { loadOrCreatePool, updatePoolPrice, updatePoolSeason, updatePoolValues } from "./utils/Pool"; import { BeanstalkPrice } from "../generated/Beanstalk/BeanstalkPrice"; import { BEANSTALK_PRICE, @@ -46,21 +34,11 @@ export function handleSunrise(event: Sunrise): void { let bean = loadBean(beanToken); let oldBeanPrice = bean.price; for (let i = 0; i < bean.pools.length; i++) { - updatePoolSeason( - bean.pools[i], - event.block.timestamp, - event.block.number, - event.params.season.toI32() - ); + updatePoolSeason(bean.pools[i], event.block.timestamp, event.block.number, event.params.season.toI32()); } for (let i = 0; i < bean.dewhitelistedPools.length; i++) { - updatePoolSeason( - bean.dewhitelistedPools[i], - event.block.timestamp, - event.block.number, - event.params.season.toI32() - ); + updatePoolSeason(bean.dewhitelistedPools[i], event.block.timestamp, event.block.number, event.params.season.toI32()); } // Fetch price from price contract to capture any non-bean token price movevements @@ -76,15 +54,7 @@ export function handleSunrise(event: Sunrise): void { let beanCurve = loadOrCreatePool(BEAN_3CRV.toHexString(), event.block.number); if (!curve.reverted) { - updateBeanValues( - BEAN_ERC20.toHexString(), - event.block.timestamp, - toDecimal(curve.value.price), - ZERO_BI, - ZERO_BI, - ZERO_BD, - ZERO_BD - ); + updateBeanValues(BEAN_ERC20.toHexString(), event.block.timestamp, toDecimal(curve.value.price), ZERO_BI, ZERO_BI, ZERO_BD, ZERO_BD); updatePoolValues( BEAN_3CRV.toHexString(), event.block.timestamp, @@ -94,19 +64,8 @@ export function handleSunrise(event: Sunrise): void { toDecimal(curve.value.liquidity).minus(beanCurve.liquidityUSD), curve.value.deltaB ); - 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) - ); + 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)); } } } else { @@ -151,13 +110,7 @@ export function handleSunrise(event: Sunrise): void { ZERO_BD, totalLiquidity.minus(bean.liquidityUSD) ); - checkBeanCross( - BEAN_ERC20_V1.toHexString(), - event.block.timestamp, - event.block.number, - bean.price, - totalPrice - ); + checkBeanCross(BEAN_ERC20_V1.toHexString(), event.block.timestamp, event.block.number, bean.price, totalPrice); updateBeanTwa(event.block.timestamp, event.block.number); } } @@ -186,17 +139,8 @@ export function handleMetapoolOracle(event: MetapoolOracle): void { export function handleWellOracle(event: WellOracle): void { setRawWellReserves(event); - setTwaLast( - event.params.well.toHexString(), - decodeCumulativeWellReserves(event.params.cumulativeReserves), - event.block.timestamp - ); - setWellTwa( - event.params.well.toHexString(), - event.params.deltaB, - event.block.timestamp, - event.block.number - ); + setTwaLast(event.params.well.toHexString(), decodeCumulativeWellReserves(event.params.cumulativeReserves), event.block.timestamp); + setWellTwa(event.params.well.toHexString(), event.params.deltaB, event.block.timestamp, event.block.number); updateBeanTwa(event.block.timestamp, event.block.number); }