diff --git a/schema.graphql b/schema.graphql index de82a7c..444e31b 100644 --- a/schema.graphql +++ b/schema.graphql @@ -229,6 +229,38 @@ type BeefyCLStrategy @entity { isInitialized: Boolean! } +""" +Vault are harvested by the strategy. This event is emitted when the strategy harvests the vault. +""" +type BeefyCLVaultHarvestEvent @entity(immutable: true) { + "transaction hash + log index" + id: Bytes! + + "The vault the harvest event is for" + vault: BeefyCLVault! + "The strategy that harvested the vault" + strategy: BeefyCLStrategy! + + "The transaction that created the vault harvest event" + createdWith: Transaction! + + "The timestamp of the harvest event so you can sort by time" + timestamp: BigInt! + + "Underlying balance of the first token after the harvest" + underlyingAmount0: BigInt! + "Underlying balance of the second token after the harvest" + underlyingAmount1: BigInt! + + "The amount of first underlying tokens compounded" + compoundedAmount0: BigInt! + "The amount of second underlying tokens compounded" + compoundedAmount1: BigInt! + + "Total amount of liquidity at time of harvest" + totalSupply: BigInt! +} + """ This event is emitted when we collect earned trading fees from the underlying pool. """ diff --git a/src/mapping/strategy.ts b/src/mapping/strategy.ts index b23f247..436c9dd 100644 --- a/src/mapping/strategy.ts +++ b/src/mapping/strategy.ts @@ -4,6 +4,7 @@ export { handleStrategyUnpaused as handleUnpaused, } from "../vault-lifecycle" export { + handleStrategyHarvest as handleHarvest, handleStrategyClaimedFees as handleClaimedFees, handleStrategyClaimedOutput as handleClaimedOutput, } from "../vault-compound" diff --git a/src/vault-compound.ts b/src/vault-compound.ts index 187b298..5a4db41 100644 --- a/src/vault-compound.ts +++ b/src/vault-compound.ts @@ -1,17 +1,54 @@ import { BigInt, ethereum } from "@graphprotocol/graph-ts" import { ClaimedFees as ClaimedFeesEvent, + Harvest as HarvestEvent, ClaimedOutput as ClaimedOutputEvent, } from "../generated/templates/BeefyCLStrategy/BeefyStrategy" import { getBeefyCLStrategy, getBeefyCLVault, getBeefyCLVaultSnapshot, isVaultRunning } from "./entity/vault" import { getTransaction } from "./entity/transaction" -import { BeefyCLVaultUnderlyingFeesCollectedEvent } from "../generated/schema" +import { BeefyCLVaultHarvestEvent, BeefyCLVaultUnderlyingFeesCollectedEvent } from "../generated/schema" import { getEventIdentifier } from "./utils/event" import { getToken } from "./entity/token" import { ZERO_BI } from "./utils/decimal" import { VAULT_SNAPSHOT_PERIODS } from "./utils/time" import { fetchVaultLatestData } from "./utils/price" +export function handleStrategyHarvest(event: HarvestEvent): void { + let strategy = getBeefyCLStrategy(event.address) + let vault = getBeefyCLVault(strategy.vault) + if (!isVaultRunning(vault)) { + return + } + + const sharesToken = getToken(vault.sharesToken) + const token0 = getToken(vault.underlyingToken0) + const token1 = getToken(vault.underlyingToken1) + + let tx = getTransaction(event.block, event.transaction) + tx.save() + + /////// + // fetch data on chain + const vaultData = fetchVaultLatestData(vault, strategy, sharesToken, token0, token1) + const vaultBalanceUnderlying0 = vaultData.token0Balance + const vaultBalanceUnderlying1 = vaultData.token1Balance + const sharesTotalSupply = vaultData.sharesTotalSupply + + /////// + // store the raw harvest event + let harvest = new BeefyCLVaultHarvestEvent(getEventIdentifier(event)) + harvest.vault = vault.id + harvest.strategy = strategy.id + harvest.createdWith = tx.id + harvest.timestamp = event.block.timestamp + harvest.underlyingAmount0 = vaultBalanceUnderlying0 + harvest.underlyingAmount1 = vaultBalanceUnderlying1 + harvest.totalSupply = sharesTotalSupply + harvest.compoundedAmount0 = event.params.fee0 + harvest.compoundedAmount1 = event.params.fee1 + harvest.save() +} + export function handleStrategyClaimedFees(event: ClaimedFeesEvent): void { handleStrategyFees( event, diff --git a/subgraph.template.yaml b/subgraph.template.yaml index 0d28e9c..d2a6c19 100644 --- a/subgraph.template.yaml +++ b/subgraph.template.yaml @@ -133,6 +133,8 @@ templates: handler: handlePaused - event: Unpaused(address) handler: handleUnpaused + - event: Harvest(uint256,uint256) + handler: handleHarvest - event: ClaimedFees(uint256,uint256,uint256,uint256) handler: handleClaimedFees - event: ClaimedOutput(uint256)