Skip to content

Commit

Permalink
Remove old fields and historical vAPY cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cujowolf committed Dec 14, 2023
1 parent 54d4f7c commit fd43ed0
Show file tree
Hide file tree
Showing 6 changed files with 39,678 additions and 153 deletions.
26 changes: 3 additions & 23 deletions projects/subgraph-beanstalk/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -304,32 +304,10 @@ type SiloYield @entity {
u: Int!
"Bean EMA for season"
beansPerSeasonEMA: BigDecimal!
"Bean APY for 0 seeds per BDV"
zeroSeedBeanAPY: BigDecimal!
"Bean APY for 2 seeds per BDV"
twoSeedBeanAPY: BigDecimal!
"Stalk APY for 2 seeds per BDV"
twoSeedStalkAPY: BigDecimal!
"Bean APY for 3 seeds per BDV"
threeSeedBeanAPY: BigDecimal!
"Stalk APY for 3 seeds per BDV"
threeSeedStalkAPY: BigDecimal!
"Bean APY for 3.25 seeds per BDV"
threePointTwoFiveSeedBeanAPY: BigDecimal!
"Stalk APY for 3.25 seeds per BDV"
threePointTwoFiveSeedStalkAPY: BigDecimal!
"Bean APY for 4 seeds per BDV"
fourSeedBeanAPY: BigDecimal!
"Stalk APY for 4 seeds per BDV"
fourSeedStalkAPY: BigDecimal!
"Bean APY for 4.5 seeds per BDV"
fourPointFiveSeedBeanAPY: BigDecimal!
"Stalk APY for 4.5 seeds per BDV"
fourPointFiveSeedStalkAPY: BigDecimal!
"Current whitelisted silo tokens"
whitelistedTokens: [String!]!
"Current Bean (0) and Stalk (1) APY for each token."
tokenAPYS: [TokenYield!]!
tokenAPYS: [TokenYield!]! @derivedFrom(field: "siloYield")
"Unix timestamp of update"
createdAt: BigInt!
}
Expand All @@ -341,6 +319,8 @@ type TokenYield @entity {
token: Bytes!
"Season for APY calculation"
season: Int!
"Related silo yield entity"
siloYield: SiloYield!
"Bean APY for season"
beanAPY: BigDecimal!
"Stalk APY for season"
Expand Down
15 changes: 15 additions & 0 deletions projects/subgraph-beanstalk/src/DiamondHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { Address, BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { ZERO_BI } from "../../subgraph-core/utils/Decimals";
import { DiamondCut } from "../generated/Diamond/Beanstalk";
import { loadBeanstalk } from "./utils/Beanstalk";
import { TOKEN_YIELD_14_000 } from "./utils/HistoricYield";
import { loadTokenYield } from "./utils/SiloEntities";

export function handleDiamondCut(event: DiamondCut): void {
let beanstalk = loadBeanstalk(event.address);

// Load the historical vAPY figures in bulk at start
if (beanstalk.lastUpgrade == ZERO_BI) {
for (let i = 0; i < TOKEN_YIELD_14_000.length; i++) {
let tokenYield = loadTokenYield(Address.fromString(TOKEN_YIELD_14_000[i][0]), <i32>parseInt(TOKEN_YIELD_14_000[i][1]));
tokenYield.beanAPY = BigDecimal.fromString(TOKEN_YIELD_14_000[i][2]);
tokenYield.stalkAPY = BigDecimal.fromString(TOKEN_YIELD_14_000[i][3]);
tokenYield.createdAt = BigInt.fromString(TOKEN_YIELD_14_000[i][4]);
tokenYield.save();
}
}

beanstalk.lastUpgrade = event.block.timestamp;
beanstalk.save();
}
12 changes: 7 additions & 5 deletions projects/subgraph-beanstalk/src/SiloHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,15 +978,17 @@ export function handleUpdatedStalkPerBdvPerSeason(event: UpdatedStalkPerBdvPerSe
export function handleWhitelistToken_V3(event: WhitelistToken_V3): void {
let silo = loadSilo(event.address);
let currentList = silo.whitelistedTokens;
if (currentList.length == 0) {
// Push unripe bean and unripe bean:3crv upon the initial whitelisting.
currentList.push(UNRIPE_BEAN.toHexString());
currentList.push(UNRIPE_BEAN_3CRV.toHexString());
}

currentList.push(event.params.token.toHexString());
silo.whitelistedTokens = currentList;
silo.save();

let setting = loadWhitelistTokenSetting(event.params.token);
setting.selector = event.params.selector;
setting.stalkIssuedPerBdv = event.params.stalk.times(BigInt.fromI32(1_000_000));
setting.stalkEarnedPerSeason = event.params.stalkEarnedPerSeason;
setting.save();

let id = "whitelistToken-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
let rawEvent = new WhitelistTokenEntity(id);
rawEvent.hash = event.transaction.hash.toHexString();
Expand Down
129 changes: 16 additions & 113 deletions projects/subgraph-beanstalk/src/YieldHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import { toDecimal, ZERO_BD } from "../../subgraph-core/utils/Decimals";
import { loadFertilizer } from "./utils/Fertilizer";
import { loadFertilizerYield } from "./utils/FertilizerYield";
import { loadSilo, loadSiloHourlySnapshot, loadSiloYield, loadTokenYield, loadWhitelistTokenSetting } from "./utils/SiloEntities";
// import {
// HISTORIC_VAPY_8_000,
// HISTORIC_VAPY_10_000,
// HISTORIC_VAPY_12_000,
// HISTORIC_VAPY_14_000,
// HISTORIC_VAPY_16_000
// } from "./utils/HistoricYield";
// import { HISTORIC_VAPY } from "./utils/HistoricYield";
import { SILO_YIELD_14_000 } from "./utils/HistoricYield";

const MAX_WINDOW = 720;

Expand All @@ -21,6 +14,21 @@ export function updateBeanEMA(t: i32, timestamp: BigInt): void {
let silo = loadSilo(BEANSTALK);
let siloYield = loadSiloYield(t);

// Check for cached info
if (t <= 14_000) {
let cacheIndex = t - 6075;
siloYield.beta = BigDecimal.fromString(SILO_YIELD_14_000[cacheIndex][1]);
siloYield.u = <i32>parseInt(SILO_YIELD_14_000[cacheIndex][2]);
siloYield.beansPerSeasonEMA = BigDecimal.fromString(SILO_YIELD_14_000[cacheIndex][3]);
siloYield.whitelistedTokens = silo.whitelistedTokens;
siloYield.createdAt = BigInt.fromString(SILO_YIELD_14_000[cacheIndex][4]);
siloYield.save();

updateFertAPY(t, timestamp);

return;
}

// When less then MAX_WINDOW data points are available,
// smooth over whatever is available. Otherwise use MAX_WINDOW.
siloYield.u = t - 6074 < MAX_WINDOW ? t - 6074 : MAX_WINDOW;
Expand Down Expand Up @@ -56,7 +64,6 @@ export function updateBeanEMA(t: i32, timestamp: BigInt): void {
// Step through the whitelisted tokens and calculate the silo APY

let beanGrownStalk = loadWhitelistTokenSetting(BEAN_ERC20).stalkEarnedPerSeason;
let tokenIds = siloYield.tokenAPYS;

for (let i = 0; i < siloYield.whitelistedTokens.length; i++) {
let token = Address.fromString(siloYield.whitelistedTokens[i]);
Expand All @@ -74,112 +81,8 @@ export function updateBeanEMA(t: i32, timestamp: BigInt): void {
tokenYield.stalkAPY = tokenAPY[1];
tokenYield.createdAt = timestamp;
tokenYield.save();

tokenIds.push(tokenYield.id);
}

siloYield.tokenAPYS = tokenIds;
siloYield.save();

// This iterates through 8760 times to calculate the silo APY
// let silo = loadSilo(BEANSTALK);

// Pull from historically calculated values prior to season 15457 rather than iterating

// let cacheIndex = -1;
// if (t <= 8000) {
// cacheIndex = t - 6075;

// siloYield.twoSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][1]);
// siloYield.twoSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][2]);
// siloYield.threeSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][3]);
// siloYield.threeSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][4]);
// siloYield.threePointTwoFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][5]);
// siloYield.threePointTwoFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][6]);
// siloYield.fourSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][7]);
// siloYield.fourSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][8]);
// siloYield.fourPointFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][9]);
// siloYield.fourPointFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][10]);
// siloYield.zeroSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_8_000[cacheIndex][11]);
// }
// } else if (t <= 10000) {
// cacheIndex = t - 8001;

// siloYield.twoSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][1]);
// siloYield.twoSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][2]);
// siloYield.threeSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][3]);
// siloYield.threeSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][4]);
// siloYield.threePointTwoFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][5]);
// siloYield.threePointTwoFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][6]);
// siloYield.fourSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][7]);
// siloYield.fourSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][8]);
// siloYield.fourPointFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][9]);
// siloYield.fourPointFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][10]);
// siloYield.zeroSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_10_000[cacheIndex][11]);
// } else if (t <= 12000) {
// cacheIndex = t - 10001;

// siloYield.twoSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][1]);
// siloYield.twoSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][2]);
// siloYield.threeSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][3]);
// siloYield.threeSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][4]);
// siloYield.threePointTwoFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][5]);
// siloYield.threePointTwoFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][6]);
// siloYield.fourSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][7]);
// siloYield.fourSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][8]);
// siloYield.fourPointFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][9]);
// siloYield.fourPointFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][10]);
// siloYield.zeroSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_12_000[cacheIndex][11]);
// } else if (t <= 14000) {
// cacheIndex = t - 12001;

// siloYield.twoSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][1]);
// siloYield.twoSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][2]);
// siloYield.threeSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][3]);
// siloYield.threeSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][4]);
// siloYield.threePointTwoFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][5]);
// siloYield.threePointTwoFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][6]);
// siloYield.fourSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][7]);
// siloYield.fourSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][8]);
// siloYield.fourPointFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][9]);
// siloYield.fourPointFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][10]);
// siloYield.zeroSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_14_000[cacheIndex][11]);
// } else if (t <= 15457) {
// cacheIndex = t - 14001;

// siloYield.twoSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][1]);
// siloYield.twoSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][2]);
// siloYield.threeSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][3]);
// siloYield.threeSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][4]);
// siloYield.threePointTwoFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][5]);
// siloYield.threePointTwoFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][6]);
// siloYield.fourSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][7]);
// siloYield.fourSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][8]);
// siloYield.fourPointFiveSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][9]);
// siloYield.fourPointFiveSeedStalkAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][10]);
// siloYield.zeroSeedBeanAPY = BigDecimal.fromString(HISTORIC_VAPY_16_000[cacheIndex][11]);
// } else {
// let twoSeedAPY = calculateAPY(currentEMA, BigDecimal.fromString("2"), silo.stalk, silo.seeds);
// siloYield.twoSeedBeanAPY = twoSeedAPY[0];
// siloYield.twoSeedStalkAPY = twoSeedAPY[1];
// let fourSeedAPY = calculateAPY(currentEMA, BigDecimal.fromString("4"), silo.stalk, silo.seeds);
// siloYield.fourSeedBeanAPY = fourSeedAPY[0];
// siloYield.fourSeedStalkAPY = fourSeedAPY[1];
// siloYield.zeroSeedBeanAPY = calculateAPY(currentEMA, ZERO_BD, silo.stalk, silo.seeds)[0];

// BIP-37 Seed changes
// let threeSeedAPY = calculateAPY(currentEMA, BigDecimal.fromString("3"), silo.stalk, silo.seeds);
// siloYield.threeSeedBeanAPY = threeSeedAPY[0];
// siloYield.threeSeedStalkAPY = threeSeedAPY[1];
// let threePointTwoFiveSeedAPY = calculateAPY(currentEMA, BigDecimal.fromString("3.25"), silo.stalk, silo.seeds);
// siloYield.threePointTwoFiveSeedBeanAPY = threePointTwoFiveSeedAPY[0];
// siloYield.threePointTwoFiveSeedStalkAPY = threePointTwoFiveSeedAPY[1];
// let fourPointFiveSeedAPY = calculateAPY(currentEMA, BigDecimal.fromString("4.5"), silo.stalk, silo.seeds);
// siloYield.fourPointFiveSeedBeanAPY = fourPointFiveSeedAPY[0];
// siloYield.fourPointFiveSeedStalkAPY = fourPointFiveSeedAPY[1];
// }
// siloYield.save();

updateFertAPY(t, timestamp);
}

Expand Down
Loading

0 comments on commit fd43ed0

Please sign in to comment.