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 - 2.2.1 #890

Merged
merged 6 commits into from
May 24, 2024
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
10 changes: 9 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@
"printWidth": 100,
"singleQuote": false,
"semi": true,
"trailingComma": "none"
"trailingComma": "none",
"overrides": [
{
"files": "projects/subgraph-*/**",
"options": {
"printWidth": 140
}
}
]
}
3 changes: 3 additions & 0 deletions projects/subgraph-bean/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
17 changes: 12 additions & 5 deletions projects/subgraph-bean/src/BeanstalkHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ 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());
}

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
Expand Down Expand Up @@ -116,10 +119,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 //
Expand Down
21 changes: 18 additions & 3 deletions projects/subgraph-bean/src/utils/Bean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
Expand Down
30 changes: 27 additions & 3 deletions projects/subgraph-bean/tests/Whitelist.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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() + "]"
);
});
});
Loading