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

re-add dumb harvest event tracking for compounded fees calculations #7

Merged
merged 2 commits into from
May 7, 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
32 changes: 32 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
1 change: 1 addition & 0 deletions src/mapping/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
handleStrategyUnpaused as handleUnpaused,
} from "../vault-lifecycle"
export {
handleStrategyHarvest as handleHarvest,
handleStrategyClaimedFees as handleClaimedFees,
handleStrategyClaimedOutput as handleClaimedOutput,
} from "../vault-compound"
39 changes: 38 additions & 1 deletion src/vault-compound.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading