Skip to content

Commit

Permalink
subgraph: snapshot entity test
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking committed Apr 11, 2024
1 parent 2ffdc8b commit 6358684
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion projects/subgraph-beanstalk/src/GaugeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function handleGaugePointChange(event: GaugePointChange): void {
export function handleUpdateAverageStalkPerBdvPerSeason(event: UpdateAverageStalkPerBdvPerSeason): void {
let silo = loadSilo(event.address);

// grownStalkPerBdvPerSeason variable currently stores overall, not per bdv
// grownStalkPerBdvPerSeason variable currently stores overall, not per bdv as the name suggests
silo.grownStalkPerBdvPerSeason = silo.depositedBDV.times(event.params.newStalkPerBdvPerSeason);
silo.save();
let siloHourly = loadSiloHourlySnapshot(event.address, currentSeason(event.address), event.block.timestamp);
Expand Down
1 change: 1 addition & 0 deletions projects/subgraph-beanstalk/src/SiloHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ export function handleWhitelistToken(event: WhitelistToken): void {
rawEvent.save();
}

// TODO: replace this implementation with simply updating the whitelist token settings + hourly/daily snapshots
export function handleUpdatedStalkPerBdvPerSeason(event: UpdatedStalkPerBdvPerSeason): void {
let siloSettings = loadWhitelistTokenSetting(event.params.token);

Expand Down
30 changes: 27 additions & 3 deletions projects/subgraph-beanstalk/tests/SeedGauge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
import { createWhitelistTokenEventBIP42 } from "./event-mocking/Whitelist";
import { createTemperatureChangeEvent } from "./event-mocking/Field";
import { simpleMockPrice } from "../../subgraph-core/tests/event-mocking/Prices";
import { loadSilo } from "../src/utils/SiloEntities";
import { mockBlock } from "../../subgraph-core/tests/event-mocking/Block";
import { dayFromTimestamp } from "../src/utils/Dates";

const ANVIL_ADDR_1 = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266".toLowerCase();

Expand Down Expand Up @@ -89,9 +92,12 @@ describe("Seed Gauge", () => {
});

test("event: UpdateAverageStalkPerBdvPerSeason", () => {
const initialSilo = loadSilo(BEANSTALK);
initialSilo.depositedBDV = BigInt.fromU32(1000);
initialSilo.save();

handleUpdateAverageStalkPerBdvPerSeason(createUpdateAverageStalkPerBdvPerSeasonEvent(BigInt.fromU32(3456)));
// TODO: Update this to expect that value times total bdv deposited
assert.fieldEquals("Silo", BEANSTALK.toHexString(), "grownStalkPerBdvPerSeason", "3456");
assert.fieldEquals("Silo", BEANSTALK.toHexString(), "grownStalkPerBdvPerSeason", "3456000");
});
});

Expand All @@ -105,7 +111,6 @@ describe("Seed Gauge", () => {
assert.fieldEquals("Silo", ANVIL_ADDR_1, "germinatingStalk", initialGerminating.plus(adjustment).toString());
});

// TODO: germinating by asset?
test("event: TotalGerminatingBalanceChanged", () => {
const initialGerminating = BigInt.fromI32(123456789);
handleTotalGerminatingBalanceChanged(
Expand Down Expand Up @@ -161,5 +166,24 @@ describe("Seed Gauge", () => {
BigInt.fromU32(66).times(ratioDecimals).toString()
);
});

test("WhitelistToken Snapshots Get Created", () => {
const timestamp = BigInt.fromU32(1712793374);
const day = dayFromTimestamp(timestamp);
assert.notInStore("WhitelistTokenHourlySnapshot", BEAN_ERC20.toHexString() + "-1");
assert.notInStore("WhitelistTokenDailySnapshot", BEAN_ERC20.toHexString() + "-" + day);

let event = createUpdateGaugeSettingsEvent(
BEAN_ERC20.toHexString(),
"0x12341234",
"0xabcabcde",
BigInt.fromU32(66).times(ratioDecimals)
);
event.block = mockBlock(BigInt.fromU32(19628585), timestamp);
handleUpdateGaugeSettings(event);

assert.fieldEquals("WhitelistTokenHourlySnapshot", BEAN_ERC20.toHexString() + "-1", "gpSelector", "0x12341234");
assert.fieldEquals("WhitelistTokenDailySnapshot", BEAN_ERC20.toHexString() + "-" + day, "gpSelector", "0x12341234");
});
});
});
3 changes: 0 additions & 3 deletions projects/subgraph-beanstalk/tests/event-mocking/SeedGauge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import {
UpdateGaugeSettings
} from "../../generated/BIP42-SeedGauge/Beanstalk";

import { AddDeposit, RemoveDeposit, RemoveDeposits } from "../../generated/Silo-Replanted/Beanstalk";
import { handleAddDeposit } from "../../src/SiloHandler";
import { BEAN_DECIMALS } from "../../../subgraph-core/utils/Constants";
import { BEANSTALK } from "../../../subgraph-core/utils/Constants";

// Default mock to include beanstalk address
Expand Down
10 changes: 10 additions & 0 deletions projects/subgraph-core/tests/event-mocking/Block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import { newMockEvent } from "matchstick-as/assembly/index";

// In practice, block number and timestamp shouldnt matter, but some value must be provided
export function mockBlock(number: BigInt = BigInt.fromI32(19579092), timestamp: BigInt = BigInt.fromU32(1712193759)): ethereum.Block {
const newBlock = changetype<ethereum.Block>(newMockEvent());
newBlock.number = number;
newBlock.timestamp = timestamp;
return newBlock;
}

0 comments on commit 6358684

Please sign in to comment.