Skip to content

Commit

Permalink
Add APR calc
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Mar 24, 2024
1 parent 3e40d8a commit 644d0ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
18 changes: 18 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ type BeefyCLVault @entity {
"Amount of native token charged as strategist fee incentive in USD"
cumulativeStrategistFeeCollectedUSD: BigDecimal!

"The last time the fees were collected. Used to calculate the vault APR."
lastCollectedFeeTimestamp: BigInt!
"""
The current APR of the vault.
This is calculated as the amound of fees collected since the last fee collection event
divided by the TVL in the vault at the time of the fee collection event, and
annualized by the time since the last fee collection event.
"""
annualPercentageRateFromLastCollection: BigDecimal!

"All investors positions in the vault"
positions: [InvestorPosition!]! @derivedFrom(field: "vault")

Expand Down Expand Up @@ -419,6 +429,14 @@ type BeefyCLVaultSnapshot implements Snapshot @entity {
protocolFeeCollectedUSD: BigDecimal!
"Amount of native token charged as strategist fee incentive in USD"
strategistFeeCollectedUSD: BigDecimal!

"""
The current APR of the vault.
This is calculated as the amound of fees collected since the last fee collection event
divided by the TVL in the vault at the time of the fee collection event, and
annualized by the time since the last fee collection event.
"""
annualPercentageRateFromLastCollection: BigDecimal!
}

"""
Expand Down
5 changes: 4 additions & 1 deletion src/entity/vault.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, Bytes } from "@graphprotocol/graph-ts"
import { BigDecimal, BigInt, Bytes } from "@graphprotocol/graph-ts"
import { BeefyCLStrategy, BeefyCLVault, BeefyCLVaultSnapshot } from "../../generated/schema"
import { ADDRESS_ZERO } from "../utils/address"
import { ZERO_BD } from "../utils/decimal"
Expand Down Expand Up @@ -53,6 +53,8 @@ export function getBeefyCLVault(vaultAddress: Bytes): BeefyCLVault {
vault.cumulativeHarvesterFeeCollectedUSD = ZERO_BD
vault.cumulativeProtocolFeeCollectedUSD = ZERO_BD
vault.cumulativeStrategistFeeCollectedUSD = ZERO_BD
vault.lastCollectedFeeTimestamp = BigInt.fromI32(0)
vault.annualPercentageRateFromLastCollection = ZERO_BD
}
return vault
}
Expand Down Expand Up @@ -106,6 +108,7 @@ export function getBeefyCLVaultSnapshot(vault: BeefyCLVault, timestamp: BigInt,
snapshot.harvesterFeeCollectedUSD = ZERO_BD
snapshot.protocolFeeCollectedUSD = ZERO_BD
snapshot.strategistFeeCollectedUSD = ZERO_BD
snapshot.annualPercentageRateFromLastCollection = ZERO_BD
}

// copy non-reseting values from the previous snapshot to the new snapshot
Expand Down
9 changes: 8 additions & 1 deletion src/vault-compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { BeefyCLVaultHarvestEvent, BeefyCLVaultUnderlyingFeesCollectedEvent } fr
import { getEventIdentifier } from "./utils/event"
import { getToken } from "./entity/token"
import { ONE_BD, ZERO_BD, tokenAmountToDecimal, decimalToTokenAmount } from "./utils/decimal"
import { SNAPSHOT_PERIODS } from "./utils/time"
import { SNAPSHOT_PERIODS, YEAR } from "./utils/time"
import { getBeefyCLProtocol, getBeefyCLProtocolSnapshot } from "./entity/protocol"
import { getInvestorPositionSnapshot } from "./entity/position"
import { getInvestor, getInvestorSnapshot } from "./entity/investor"
Expand Down Expand Up @@ -339,6 +339,12 @@ export function handleStrategyClaimedFees(event: ClaimedFeesEvent): void {
vault.underlyingAmount0USD = vault.underlyingAmount0.times(token0PriceInUSD)
vault.underlyingAmount1USD = vault.underlyingAmount1.times(token1PriceInUSD)
vault.totalValueLockedUSD = vault.underlyingAmount0USD.plus(vault.underlyingAmount1USD)
const feesEarnedSinceLastCollection = collect.collectedValueUSD
const feesEarnedOverSeconds = event.block.timestamp.minus(vault.lastCollectedFeeTimestamp)
const feesPerSecond = feesEarnedSinceLastCollection.div(feesEarnedOverSeconds.toBigDecimal())
const feesPerYear = feesPerSecond.times(YEAR.toBigDecimal())
vault.annualPercentageRateFromLastCollection = feesPerYear.div(vault.totalValueLockedUSD)
vault.lastCollectedFeeTimestamp = event.block.timestamp
vault.save()
for (let i = 0; i < periods.length; i++) {
log.debug("handleStrategyChargedFees: updating vault snapshot for vault {} and period {}", [
Expand All @@ -357,6 +363,7 @@ export function handleStrategyClaimedFees(event: ClaimedFeesEvent): void {
vaultSnapshot.underlyingAmount0USD = vault.underlyingAmount0USD
vaultSnapshot.underlyingAmount1USD = vault.underlyingAmount1USD
vaultSnapshot.totalValueLockedUSD = vault.totalValueLockedUSD
vaultSnapshot.annualPercentageRateFromLastCollection = vault.annualPercentageRateFromLastCollection
vaultSnapshot.save()
}

Expand Down

0 comments on commit 644d0ba

Please sign in to comment.