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

Beanstalk 3 Subgraphs (BIP-50) #1084

Draft
wants to merge 90 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
2edf9f3
Add cache builder js scripts for peg cross
soilking Sep 5, 2024
3c9361a
add package script for build-cache
soilking Sep 5, 2024
03381ca
updated readme with cache instructions
soilking Sep 5, 2024
138b266
bean entity init handler with sample input
soilking Sep 5, 2024
5bcfb35
bean subgraph l2 generator script
soilking Sep 5, 2024
8bb7f02
major refactor and file restructure
soilking Sep 6, 2024
8143641
chop converts and l2sr
soilking Sep 6, 2024
e8df1f9
entity bytes refactor and add protocol to version entity
soilking Sep 6, 2024
e9420ad
update bean manifest
soilking Sep 6, 2024
5893bb6
aggregate 3crv logic
soilking Sep 6, 2024
1df89b2
refactor constants usage
soilking Sep 6, 2024
83cc7ad
more constants refactor
soilking Sep 7, 2024
d4c791d
metapooloracle as legacy handler
soilking Sep 7, 2024
2464293
refactor constants
soilking Sep 7, 2024
7468335
add missing abis
soilking Sep 9, 2024
07fd6f7
refactor bean constants to core
soilking Sep 9, 2024
1ed4c38
add v function to all subgraphs
soilking Sep 9, 2024
c98b4b2
add shared constants to beanstalk subgraph
soilking Sep 9, 2024
40e21d7
remove unnecessary fields
soilking Sep 9, 2024
a47fa0b
string -> bytes for address refactor
soilking Sep 9, 2024
02ed6e3
misc refactor fixes
soilking Sep 9, 2024
46e1f40
finish constants refactor
soilking Sep 9, 2024
f69bf89
increment version to 3.0.0
soilking Sep 10, 2024
c9028c3
change string to bytes
soilking Sep 11, 2024
5380b78
extract stalk decimal constant
soilking Sep 11, 2024
3fd0222
carry over export for beanstalk subgraph
soilking Sep 11, 2024
60abb48
separate init and pre-unpause b3 init
soilking Sep 11, 2024
c62460a
clear deltas on pre-unpause
soilking Sep 11, 2024
24533c6
marketplace, silo legacy events
soilking Sep 12, 2024
32b75c0
reward event replacement
soilking Sep 12, 2024
4762926
legacy updategaugesettings
soilking Sep 12, 2024
325d262
fieldid to field events
soilking Sep 12, 2024
cb49737
Update ethereum manifests
soilking Sep 12, 2024
0e61694
update arbitrum manifest
soilking Sep 12, 2024
d8dabdd
update stalk decimal constants
soilking Sep 12, 2024
6b8f1d2
Update types in tests
soilking Sep 12, 2024
bf99584
latest events to use reseed abi
soilking Sep 12, 2024
2573dbd
set manifest abi to reseed
soilking Sep 12, 2024
2010d2e
move legacy gauge event handlers
soilking Sep 12, 2024
03db4f2
update farm event
soilking Sep 12, 2024
25a55fc
legacy season
soilking Sep 12, 2024
3b4707e
update type
soilking Sep 12, 2024
44bb42a
Add l2 constants
soilking Sep 12, 2024
ea2a306
arbitrum runtime constants
soilking Sep 12, 2024
9a6af5f
init arb version
soilking Sep 12, 2024
0a343c8
set fictitious deployment block
soilking Sep 12, 2024
d0efc38
fix replant milestone
soilking Sep 12, 2024
bb26fdb
basin arbitrum yaml
soilking Sep 13, 2024
01274b3
refactor basin subgraph for arb
soilking Sep 13, 2024
09e678b
refactor bean subgraph event abis
soilking Sep 13, 2024
fde954e
configure new pools
soilking Sep 13, 2024
a0d9788
clean up manifests
soilking Sep 13, 2024
e628689
Merge branch 'master' into b3-subgraph
soilking Sep 13, 2024
beea273
fix comparisons
soilking Sep 13, 2024
63dada4
add calculations curve abi
soilking Sep 13, 2024
7739532
fix variable shadowing
soilking Sep 13, 2024
4fb8736
migration event handlers
soilking Sep 14, 2024
773c351
fix plot index issue
soilking Sep 14, 2024
a7208b7
add abi
soilking Sep 14, 2024
bfee5e0
use correct handler name
soilking Sep 15, 2024
8249090
update endBlock for pre exploit
soilking Sep 15, 2024
3c9859e
update manifest
soilking Sep 16, 2024
99736a7
migration tests
soilking Sep 16, 2024
8e2855a
fix market tests
soilking Sep 16, 2024
b795509
refactor isgauge check on whitelisted tokens
soilking Sep 16, 2024
c58ea1c
handleUpdateWhitelistStatus
soilking Sep 16, 2024
80b8c2f
manifest for arb migration
soilking Sep 16, 2024
f4faf0f
fert token info cached
soilking Sep 16, 2024
2914bc0
Merge branch 'master' into b3-subgraph
soilking Sep 16, 2024
d33a6e3
fix bean manifest
soilking Sep 17, 2024
fbb86a7
Set basin deployment block
soilking Sep 18, 2024
0e2cd73
add init datasource to manifests
soilking Sep 18, 2024
dced90e
fix season beans init
soilking Sep 18, 2024
0e3115d
add RESEED_MIGRATED to plot source enum
soilking Sep 19, 2024
fe1b794
volatile price peg check every 10 seconds
soilking Sep 19, 2024
a3fc8a1
fix number of sows
soilking Sep 19, 2024
4b30336
fix harvestable pods count
soilking Sep 19, 2024
5a870e7
Update arb fert address
soilking Sep 19, 2024
8c3e498
set initial temperature
soilking Sep 19, 2024
10f590f
MigratedAccountStatus
soilking Sep 19, 2024
66ec3ae
fix ethereum manifest for silo
soilking Sep 19, 2024
a95aa2c
fix overcounting deposited beans
soilking Sep 19, 2024
834bfed
Update test
soilking Sep 19, 2024
dfa4ecd
set reseed pause block constants
soilking Sep 20, 2024
664d054
update b3 cached values
soilking Sep 23, 2024
29a3d57
update fert/bp address
soilking Sep 23, 2024
ed093f7
remove silo seeds
soilking Sep 24, 2024
fd03aa6
fix overall silo withdraw
soilking Sep 24, 2024
c023cb0
Fix deposited amount mismatch
soilking Sep 24, 2024
0477238
separate datasource
soilking Sep 24, 2024
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
109 changes: 109 additions & 0 deletions projects/subgraph-basin/manifests/arbitrum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
specVersion: 0.0.9
schema:
file: ../schema.graphql
dataSources:
###
# INITIALIZATION
###
- kind: ethereum/contract
name: InitVersion
network: arbitrum-one
source:
address: "0xBA51AAAa8C2f911AE672e783707Ceb2dA6E97521"
abi: Aquifer
startBlock: 235608025
endBlock: 235608025
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Version
abis:
- name: Aquifer
file: ../../subgraph-core/abis/Aquifer.json
blockHandlers:
- handler: handleInitVersion
filter:
kind: once
file: ../src/utils/constants/Version.ts
- kind: ethereum/contract
name: Aquifer
network: arbitrum-one
source:
address: "0xBA51AAAa8C2f911AE672e783707Ceb2dA6E97521"
abi: Aquifer
startBlock: 235608025
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Aquifer
abis:
- name: Aquifer
file: ../../subgraph-core/abis/Aquifer.json
- name: ERC20
file: ../../subgraph-core/abis/ERC20.json
eventHandlers:
- event: BoreWell(address,address,address[],(address,bytes),(address,bytes)[],bytes)
handler: handleBoreWell
file: ../src/templates/AquiferHandler.ts
- kind: ethereum/contract
name: Beanstalk
network: arbitrum-one
source:
address: "0xD1A0060ba708BC4BCD3DA6C37EFa8deDF015FB70"
abi: Beanstalk
startBlock: 235608025
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Beanstalk
abis:
- name: Beanstalk
file: ../../subgraph-core/abis/Beanstalk/Beanstalk-BIP50.json
eventHandlers:
- event: Sunrise(indexed uint256)
handler: handleSunrise
file: ../src/BeanstalkHandler.ts
templates:
- kind: ethereum/contract
name: Well
network: arbitrum-one
source:
abi: Well
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Well
abis:
- name: Well
file: ../../subgraph-core/abis/Well.json
- name: BeanstalkPrice
file: ../../subgraph-core/abis/BeanstalkPrice.json
- name: ERC20
file: ../../subgraph-core/abis/ERC20.json
eventHandlers:
- event: AddLiquidity(uint256[],uint256,address)
handler: handleAddLiquidity
- event: RemoveLiquidity(uint256,uint256[],address)
handler: handleRemoveLiquidity
- event: RemoveLiquidityOneToken(uint256,address,uint256,address)
handler: handleRemoveLiquidityOneToken
- event: Swap(address,address,uint256,uint256,address)
handler: handleSwap
- event: Shift(uint256[],address,uint256,address)
handler: handleShift
- event: Sync(uint256[],uint256,address)
handler: handleSync
file: ../src/WellHandler.ts
# features:
# - grafting
# graft:
# base: QmWHi6hu2wXnyxBHHmQHwCQLoq5KkoTX2qLm8MdyVXTyTu
# block: 20216425
6 changes: 3 additions & 3 deletions projects/subgraph-basin/manifests/codegen-abis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dataSources:
- name: ERC20
file: ../../subgraph-core/abis/ERC20.json
- name: Beanstalk
file: ../../subgraph-core/abis/Beanstalk/Beanstalk-Replanted.json
file: ../../subgraph-core/abis/Beanstalk/Beanstalk-BIP50.json
- name: Well
file: ../../subgraph-core/abis/Well.json
- name: CurvePrice
Expand All @@ -37,7 +37,7 @@ dataSources:
- handler: handleInitVersion
filter:
kind: once
file: ../src/utils/Init.ts
file: ../src/utils/constants/Version.ts
templates:
- kind: ethereum/contract
name: Well
Expand All @@ -57,4 +57,4 @@ templates:
- handler: handleInitVersion
filter:
kind: once
file: ../src/utils/Init.ts
file: ../src/utils/constants/Version.ts
6 changes: 3 additions & 3 deletions projects/subgraph-basin/manifests/ethereum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dataSources:
# INITIALIZATION
###
- kind: ethereum/contract
name: Version
name: InitVersion
network: mainnet
source:
address: "0xBA51AAAA95aeEFc1292515b36D86C51dC7877773"
Expand All @@ -26,7 +26,7 @@ dataSources:
- handler: handleInitVersion
filter:
kind: once
file: ../src/utils/Init.ts
file: ../src/utils/constants/Version.ts
- kind: ethereum/contract
name: Aquifer
network: mainnet
Expand Down Expand Up @@ -64,7 +64,7 @@ dataSources:
- Beanstalk
abis:
- name: Beanstalk
file: ../../subgraph-core/abis/Beanstalk/Beanstalk-Replanted.json
file: ../../subgraph-core/abis/Beanstalk/Beanstalk-BIP50.json
eventHandlers:
- event: Sunrise(indexed uint256)
handler: handleSunrise
Expand Down
29 changes: 13 additions & 16 deletions projects/subgraph-basin/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Custommized base schema from Messari
# Subgraph Schema: DEX AMM (Extended)
# Version: 3.0.3
# See https://github.com/messari/subgraphs/blob/master/docs/Schema.md for details
# This same entity schema is intended for use across the subgraphs
type Version @entity {
"= 'subgraph'"
id: ID!
"= 'beanstalk'"
subgraphName: String!
"Verison number of the subgraph"
versionNumber: String!
"Address of Beanstalk protocol"
protocolAddress: Bytes!
"Which blockchain is being indexed, i.e. 'ethereum', 'arbitrum', etc."
chain: String!
}

enum LiquidityEvent {
# Additions
Expand All @@ -17,18 +26,6 @@ enum SwapEvent {
SHIFT
}

# This same entity schema is intended for use across the subgraphs
type Version @entity {
"= 'subgraph'"
id: ID!
"= 'beanstalk'"
subgraphName: String!
"Verison number of the subgraph"
versionNumber: String!
"Which blockchain is being indexed, i.e. 'ethereum', 'arbitrum', etc."
chain: String!
}

type Token @entity {
" Smart contract address of the token "
id: Bytes!
Expand Down
5 changes: 3 additions & 2 deletions projects/subgraph-basin/src/BeanstalkHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Address } from "@graphprotocol/graph-ts";
import { AQUIFER } from "../../subgraph-core/utils/Constants";
import { Sunrise } from "../generated/Basin-ABIs/Beanstalk";
import { loadOrCreateAquifer } from "./utils/Aquifer";
import { checkForSnapshot } from "./utils/Well";
import { getAquifer } from "../../subgraph-core/constants/RuntimeConstants";
import { v } from "./utils/constants/Version";

export function handleSunrise(event: Sunrise): void {
// Right now this is a manual list of aquifers that are checked for deployments and wells updated
// Keeping this manual is reasonable as each aquifer has to be defined as a datasource in subgraph.yaml

let aquifer = loadOrCreateAquifer(AQUIFER);
let aquifer = loadOrCreateAquifer(getAquifer(v()));

for (let i = 0; i < aquifer.wells.length; i++) {
checkForSnapshot(Address.fromBytes(aquifer.wells[i]), event.block.timestamp, event.block.number);
Expand Down
31 changes: 22 additions & 9 deletions projects/subgraph-basin/src/utils/BeanstalkPrice.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
// Unfortunately this file must be copied across the various subgraph projects. This is due to the codegen
import { Address, BigInt } from "@graphprotocol/graph-ts";
import { BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { BeanstalkPrice } from "../../generated/Basin-ABIs/BeanstalkPrice";
import { BEANSTALK_PRICE_1, BEANSTALK_PRICE_2, PRICE_2_BLOCK } from "../../../subgraph-core/utils/Constants";
import { getBeanstalkPriceAddress } from "../../../subgraph-core/constants/RuntimeConstants";
import { v } from "./constants/Version";
import { toDecimal } from "../../../subgraph-core/utils/Decimals";
import { CurvePrice } from "../../generated/Basin-ABIs/CurvePrice";
import { CURVE_PRICE } from "../../../subgraph-core/constants/raw/BeanstalkEthConstants";

// Gets the BeanstalkPrice contract, bound to the appropriate instance of the contract.
// Note: Will bind to PRICE_1 even if that contract has not been deployed yet
// Thus the caller still needs to check for reverts.
export function getBeanstalkPrice(blockNumber: BigInt): BeanstalkPrice {
let contractAddress: Address;
if (blockNumber < PRICE_2_BLOCK) {
contractAddress = BEANSTALK_PRICE_1;
return BeanstalkPrice.bind(getBeanstalkPriceAddress(v(), blockNumber));
}

export function getBeanPrice(blockNumber: BigInt): BigDecimal | null {
let beanstalkPrice = getBeanstalkPrice(blockNumber);
let price = beanstalkPrice.try_price();
if (!price.reverted) {
return toDecimal(price.value.price);
} else {
contractAddress = BEANSTALK_PRICE_2;
// Fetch price on Curve
let curvePrice = CurvePrice.bind(CURVE_PRICE);
let curve = curvePrice.try_getCurve();

if (curve.reverted) {
return null;
}
return toDecimal(curve.value.price);
}
return BeanstalkPrice.bind(contractAddress);
}
24 changes: 0 additions & 24 deletions projects/subgraph-basin/src/utils/Init.ts

This file was deleted.

31 changes: 10 additions & 21 deletions projects/subgraph-basin/src/utils/Token.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Address, BigDecimal, BigInt, log } from "@graphprotocol/graph-ts";
import { ERC20 } from "../../generated/Basin-ABIs/ERC20";
import { Token } from "../../generated/schema";
import { CurvePrice } from "../../generated/Basin-ABIs/CurvePrice";
import { BEAN_ERC20, CURVE_PRICE } from "../../../subgraph-core/utils/Constants";
import { toDecimal, ZERO_BD, ZERO_BI } from "../../../subgraph-core/utils/Decimals";
import { getBeanstalkPrice } from "./BeanstalkPrice";
import { BI_MAX, ZERO_BD, ZERO_BI } from "../../../subgraph-core/utils/Decimals";
import { getBeanPrice } from "./BeanstalkPrice";
import { getProtocolToken } from "../../../subgraph-core/constants/RuntimeConstants";
import { v } from "./constants/Version";

export function loadOrCreateToken(tokenAddress: Address): Token {
let token = Token.load(tokenAddress);
Expand Down Expand Up @@ -45,7 +45,7 @@ export function loadToken(tokenAddress: Address): Token {
}

export function getBeanPriceUDSC(): BigDecimal {
let token = loadToken(BEAN_ERC20);
let token = loadToken(getProtocolToken(v(), BI_MAX));
return token.lastPriceUSD;
}

Expand All @@ -56,23 +56,12 @@ export function getTokenDecimals(tokenAddress: Address): i32 {

export function updateTokenUSD(tokenAddress: Address, blockNumber: BigInt, beanPrice: BigDecimal = ZERO_BD): void {
let token = loadToken(tokenAddress);
if (tokenAddress == BEAN_ERC20) {
// Attempt to use Beanstalk price contract first

let beanstalkPrice = getBeanstalkPrice(blockNumber);
let price = beanstalkPrice.try_price();
if (!price.reverted) {
token.lastPriceUSD = toDecimal(price.value.price);
} else {
// Fetch price on Curve
let curvePrice = CurvePrice.bind(CURVE_PRICE);
let curve = curvePrice.try_getCurve();

if (curve.reverted) {
return;
}
token.lastPriceUSD = toDecimal(curve.value.price);
if (tokenAddress == getProtocolToken(v(), BI_MAX)) {
const beanPrice = getBeanPrice(blockNumber);
if (beanPrice === null) {
return;
}
token.lastPriceUSD = beanPrice;
} else {
token.lastPriceUSD = beanPrice.times(getBeanPriceUDSC());
}
Expand Down
8 changes: 5 additions & 3 deletions projects/subgraph-basin/src/utils/Well.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Address, BigDecimal, BigInt, Bytes, log } from "@graphprotocol/graph-ts
import { BoreWellWellFunctionStruct } from "../../generated/Basin-ABIs/Aquifer";
import { Well, WellDailySnapshot, WellFunction, WellHourlySnapshot } from "../../generated/schema";
import { ERC20 } from "../../generated/Basin-ABIs/ERC20";
import { BEAN_ERC20 } from "../../../subgraph-core/utils/Constants";
import { dayFromTimestamp, hourFromTimestamp } from "../../../subgraph-core/utils/Dates";
import {
deltaBigDecimalArray,
Expand All @@ -15,6 +14,8 @@ import {
ZERO_BI
} from "../../../subgraph-core/utils/Decimals";
import { getTokenDecimals, loadToken, updateTokenUSD } from "./Token";
import { getProtocolToken } from "../../../subgraph-core/constants/RuntimeConstants";
import { v } from "./constants/Version";

export function createWell(wellAddress: Address, implementation: Address, inputTokens: Address[]): Well {
let well = Well.load(wellAddress);
Expand Down Expand Up @@ -135,8 +136,9 @@ export function updateWellTokenUSDPrices(wellAddress: Address, blockNumber: BigI
let well = loadWell(wellAddress);

// Update the BEAN price first as it is the reference for other USD calculations
updateTokenUSD(BEAN_ERC20, blockNumber, BigDecimal.fromString("1"));
let beanIndex = well.tokens.indexOf(BEAN_ERC20);
const beanToken = getProtocolToken(v(), blockNumber);
updateTokenUSD(beanToken, blockNumber, BigDecimal.fromString("1"));
let beanIndex = well.tokens.indexOf(beanToken);
// Curretly only supporting USD values for Wells with BEAN as a token.
if (beanIndex == -1) {
return;
Expand Down
Loading
Loading