diff --git a/schema.graphql b/schema.graphql index d0ea255..c359bdc 100644 --- a/schema.graphql +++ b/schema.graphql @@ -466,12 +466,10 @@ type BeefyCLVaultHarvestEvent @entity(immutable: true) { underlyingAmount0: BigDecimal! "Underlying balance of the second token after the harvest" underlyingAmount1: BigDecimal! - "Underlying balance of the first token after the harvest in USD" underlyingAmount0USD: BigDecimal! "Underlying balance of the second token after the harvest in USD" underlyingAmount1USD: BigDecimal! - "Total value locked in the vault after the harvest in USD" totalValueLockedUSD: BigDecimal! @@ -479,12 +477,10 @@ type BeefyCLVaultHarvestEvent @entity(immutable: true) { harvestedAmount0: BigDecimal! "The amount of second underlying tokens harvested" harvestedAmount1: BigDecimal! - "The amount of first underlying tokens harvested in USD" harvestedAmount0USD: BigDecimal! "The amount of second underlying tokens harvested in USD" harvestedAmount1USD: BigDecimal! - "Total usd value of the harvest" harvestValueUSD: BigDecimal! @@ -494,6 +490,53 @@ type BeefyCLVaultHarvestEvent @entity(immutable: true) { priceOfToken0InUSD: BigDecimal! } +""" +This event is emitted when we collect earned trading fees from the underlying pool. +It has a different meaning as the harvest event as we may collect fees on multiple occasions, including on harvest. +""" +type BeefyCLVaultUnderlyingFeesCollectedEvent @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: BigDecimal! + "Underlying balance of the second token after the harvest" + underlyingAmount1: BigDecimal! + "Underlying balance of the first token after the harvest in USD" + underlyingAmount0USD: BigDecimal! + "Underlying balance of the second token after the harvest in USD" + underlyingAmount1USD: BigDecimal! + "Total value locked in the vault after the harvest in USD" + totalValueLockedUSD: BigDecimal! + + "Amount of collected fees in the first token" + collectedAmount0: BigDecimal! + "Amount of collected fees in the second token" + collectedAmount1: BigDecimal! + "Amount of collected fees in the first token in USD" + collectedAmount0USD: BigDecimal! + "Amount of collected fees in the second token in USD" + collectedAmount1USD: BigDecimal! + "Total usd value of the collected fees" + collectedValueUSD: BigDecimal! + + "Price of the first token in expressed as the second token at the time of the harvest" + priceOfToken0InToken1: BigDecimal! + "Price of the first token in USD at the time of the harvest" + priceOfToken0InUSD: BigDecimal! +} + ####################### ##### CL Incentive #### ####################### diff --git a/src/mapping/strategy.ts b/src/mapping/strategy.ts index e366714..f09c4e0 100644 --- a/src/mapping/strategy.ts +++ b/src/mapping/strategy.ts @@ -4,8 +4,11 @@ export { handleStrategyUnpaused as handleUnpaused, } from "../vault-lifecycle" export { handleStrategyOwnershipTransferred as handleOwnershipTransferred } from "../ownership" -export { handleStrategyHarvest as handleHarvest } from "../harvest" -export { handleChargedFees } from "../vault-fees" +export { + handleStrategyHarvest as handleHarvest, + handleStrategyClaimedFees as handleClaimedFees, +} from "../vault-compound" +export { handleStrategyChargedFees as handleChargedFees } from "../vault-fees" export { handleStrategySetLpToken0ToNativePath as handleSetLpToken0ToNativePath, handleStrategySetLpToken1ToNativePath as handleSetLpToken1ToNativePath, diff --git a/src/harvest.ts b/src/vault-compound.ts similarity index 64% rename from src/harvest.ts rename to src/vault-compound.ts index ee88a95..f6d8568 100644 --- a/src/harvest.ts +++ b/src/vault-compound.ts @@ -1,9 +1,12 @@ import { Address, BigDecimal, BigInt, log } from "@graphprotocol/graph-ts" -import { Harvest as HarvestEvent } from "./../generated/templates/BeefyCLStrategy/StrategyPassiveManagerUniswap" -import { BeefyVaultConcLiq as BeefyCLVaultContract } from "./../generated/templates/BeefyCLVault/BeefyVaultConcLiq" +import { + ClaimedFees as ClaimedFeesEvent, + Harvest as HarvestEvent, +} from "../generated/templates/BeefyCLStrategy/StrategyPassiveManagerUniswap" +import { BeefyVaultConcLiq as BeefyCLVaultContract } from "../generated/templates/BeefyCLVault/BeefyVaultConcLiq" import { getBeefyCLStrategy, getBeefyCLVault, getBeefyCLVaultSnapshot, isVaultRunning } from "./entity/vault" import { getTransaction } from "./entity/transaction" -import { BeefyCLVaultHarvestEvent } from "./../generated/schema" +import { BeefyCLVaultHarvestEvent, BeefyCLVaultUnderlyingFeesCollectedEvent } from "../generated/schema" import { getEventIdentifier } from "./utils/event" import { getToken } from "./entity/token" import { ONE_BD, ZERO_BD, tokenAmountToDecimal, decimalToTokenAmount } from "./utils/decimal" @@ -100,11 +103,11 @@ export function handleStrategyHarvest(event: HarvestEvent): void { harvest.timestamp = event.block.timestamp harvest.underlyingAmount0 = vaultBalanceUnderlying0 harvest.underlyingAmount1 = vaultBalanceUnderlying1 - harvest.underlyingAmount0USD = vault.underlyingAmount0.times(token0PriceInUSD) - harvest.underlyingAmount1USD = vault.underlyingAmount1.times(token1PriceInUSD) - harvest.totalValueLockedUSD = vault.underlyingAmount0 + harvest.underlyingAmount0USD = vaultBalanceUnderlying0.times(token0PriceInUSD) + harvest.underlyingAmount1USD = vaultBalanceUnderlying1.times(token1PriceInUSD) + harvest.totalValueLockedUSD = vaultBalanceUnderlying0 .times(token0PriceInUSD) - .plus(vault.underlyingAmount1.times(token1PriceInUSD)) + .plus(vaultBalanceUnderlying1.times(token1PriceInUSD)) harvest.harvestedAmount0 = tokenAmountToDecimal(event.params.fee0, token0.decimals) harvest.harvestedAmount1 = tokenAmountToDecimal(event.params.fee1, token1.decimals) harvest.harvestedAmount0USD = harvest.harvestedAmount0.times(token0PriceInUSD) @@ -116,17 +119,6 @@ export function handleStrategyHarvest(event: HarvestEvent): void { /////// // update vault entities - vault.currentPriceOfToken0InToken1 = currentPriceInToken1 - vault.currentPriceOfToken0InUSD = currentPriceInToken1.times(token1PriceInUSD) - vault.priceRangeMin1 = rangeToken1Price.min - vault.priceRangeMax1 = rangeToken1Price.max - vault.priceRangeMinUSD = vault.priceRangeMin1.times(token1PriceInUSD) - vault.priceRangeMaxUSD = vault.priceRangeMax1.times(token1PriceInUSD) - vault.underlyingAmount0 = vaultBalanceUnderlying0 - vault.underlyingAmount1 = vaultBalanceUnderlying1 - vault.underlyingAmount0USD = vault.underlyingAmount0.times(token0PriceInUSD) - vault.underlyingAmount1USD = vault.underlyingAmount1.times(token1PriceInUSD) - vault.totalValueLockedUSD = vault.underlyingAmount0USD.plus(vault.underlyingAmount1USD) vault.cumulativeHarvestCount += 1 vault.cumulativeHarvestedAmount0 = vault.cumulativeHarvestedAmount0.plus(harvest.harvestedAmount0) vault.cumulativeHarvestedAmount1 = vault.cumulativeHarvestedAmount1.plus(harvest.harvestedAmount1) @@ -140,17 +132,6 @@ export function handleStrategyHarvest(event: HarvestEvent): void { periods[i].toString(), ]) const vaultSnapshot = getBeefyCLVaultSnapshot(vault, event.block.timestamp, periods[i]) - vaultSnapshot.currentPriceOfToken0InToken1 = vault.currentPriceOfToken0InToken1 - vaultSnapshot.currentPriceOfToken0InUSD = vault.currentPriceOfToken0InUSD - vaultSnapshot.priceRangeMin1 = vault.priceRangeMin1 - vaultSnapshot.priceRangeMax1 = vault.priceRangeMax1 - vaultSnapshot.priceRangeMinUSD = vault.priceRangeMinUSD - vaultSnapshot.priceRangeMaxUSD = vault.priceRangeMaxUSD - vaultSnapshot.underlyingAmount0 = vault.underlyingAmount0 - vaultSnapshot.underlyingAmount1 = vault.underlyingAmount1 - vaultSnapshot.underlyingAmount0USD = vault.underlyingAmount0USD - vaultSnapshot.underlyingAmount1USD = vault.underlyingAmount1USD - vaultSnapshot.totalValueLockedUSD = vault.totalValueLockedUSD vaultSnapshot.harvestCount += 1 vaultSnapshot.harvestedAmount0 = vaultSnapshot.harvestedAmount0.plus(harvest.harvestedAmount0) vaultSnapshot.harvestedAmount1 = vaultSnapshot.harvestedAmount1.plus(harvest.harvestedAmount1) @@ -173,16 +154,10 @@ export function handleStrategyHarvest(event: HarvestEvent): void { positivePositionCount += 1 const positionPercentOfTotalSupply = position.sharesBalance.div(sharesTotalSupply) + const positionChangeUSD = positionPercentOfTotalSupply.times(harvest.harvestValueUSD) log.debug("handleStrategyHarvest: updating investor position for investor {}", [position.investor.toHexString()]) let investor = getInvestor(position.investor) - position.underlyingBalance0 = position.sharesBalance.times(shareTokenToUnderlying0Rate) - position.underlyingBalance1 = position.sharesBalance.times(shareTokenToUnderlying1Rate) - position.underlyingBalance0USD = position.underlyingBalance0.times(token0PriceInUSD) - position.underlyingBalance1USD = position.underlyingBalance1.times(token1PriceInUSD) - const previousPositionValueUSD = position.positionValueUSD - position.positionValueUSD = position.underlyingBalance0USD.plus(position.underlyingBalance1USD) - const positionChangeUSD = position.positionValueUSD.minus(previousPositionValueUSD) position.cumulativeHarvestedAmount0 = position.cumulativeHarvestedAmount0.plus( harvest.harvestedAmount0.times(positionPercentOfTotalSupply), ) @@ -203,12 +178,6 @@ export function handleStrategyHarvest(event: HarvestEvent): void { periods[i].toString(), ]) const positionSnapshot = getInvestorPositionSnapshot(vault, investor, event.block.timestamp, periods[i]) - positionSnapshot.sharesBalance = position.sharesBalance - positionSnapshot.underlyingBalance0 = position.underlyingBalance0 - positionSnapshot.underlyingBalance1 = position.underlyingBalance1 - positionSnapshot.underlyingBalance0USD = position.underlyingBalance0USD - positionSnapshot.underlyingBalance1USD = position.underlyingBalance1USD - positionSnapshot.positionValueUSD = position.positionValueUSD positionSnapshot.harvestedAmount0 = positionSnapshot.harvestedAmount0.plus( harvest.harvestedAmount0.times(positionPercentOfTotalSupply), ) @@ -226,7 +195,6 @@ export function handleStrategyHarvest(event: HarvestEvent): void { } log.debug("handleStrategyHarvest: updating investor for investor {}", [position.investor.toHexString()]) - investor.totalPositionValueUSD = investor.totalPositionValueUSD.plus(positionChangeUSD) investor.cumulativeHarvestValueUSD = investor.cumulativeHarvestValueUSD.plus(positionChangeUSD) investor.save() for (let i = 0; i < periods.length; i++) { @@ -235,7 +203,6 @@ export function handleStrategyHarvest(event: HarvestEvent): void { periods[i].toString(), ]) const investorSnapshot = getInvestorSnapshot(investor, event.block.timestamp, periods[i]) - investorSnapshot.totalPositionValueUSD = investor.totalPositionValueUSD investorSnapshot.harvestValueUSD = investorSnapshot.harvestValueUSD.plus(positionChangeUSD) investorSnapshot.save() } @@ -245,15 +212,11 @@ export function handleStrategyHarvest(event: HarvestEvent): void { // update protocol entities log.debug("handleStrategyHarvest: updating protocol entities for vault {}", [vault.id.toHexString()]) const protocol = getBeefyCLProtocol() - protocol.totalValueLockedUSD = protocol.totalValueLockedUSD.plus(harvest.harvestValueUSD) - protocol.cumulativeTransactionCount += 1 protocol.cumulativeHarvestCount += 1 protocol.save() for (let i = 0; i < periods.length; i++) { log.debug("handleStrategyHarvest: updating protocol snapshot for period {}", [periods[i].toString()]) const protocolSnapshot = getBeefyCLProtocolSnapshot(event.block.timestamp, periods[i]) - protocolSnapshot.totalValueLockedUSD = protocol.totalValueLockedUSD - protocolSnapshot.transactionCount += 1 protocolSnapshot.harvesterTransactionsCount += 1 protocolSnapshot.totalGasSpent = protocolSnapshot.totalGasSpent.plus(tx.gasFee) protocolSnapshot.totalGasSpentUSD = protocolSnapshot.totalGasSpentUSD.plus(txGasFeeUSD) @@ -268,3 +231,201 @@ export function handleStrategyHarvest(event: HarvestEvent): void { protocolSnapshot.save() } } + +export function handleStrategyClaimedFees(event: ClaimedFeesEvent): void { + let strategy = getBeefyCLStrategy(event.address) + let vault = getBeefyCLVault(strategy.vault) + if (!isVaultRunning(vault)) { + log.error("handleStrategyClaimedFees: vault {} not active at block {}: {}", [ + vault.id.toHexString(), + event.block.number.toString(), + vault.lifecycle, + ]) + return + } + + log.debug("handleStrategyChargedFees: processing chaimed fees for vault {}", [vault.id.toHexString()]) + + const periods = SNAPSHOT_PERIODS + const sharesToken = getToken(vault.sharesToken) + const token0 = getToken(vault.underlyingToken0) + const token1 = getToken(vault.underlyingToken1) + + let tx = getTransaction(event.block, event.transaction, event.receipt) + tx.save() + + /////// + // fetch data on chain + // TODO: use multicall3 to fetch all data in one call + log.debug("handleStrategyChargedFees: fetching data for vault {}", [vault.id.toHexString()]) + const vaultContract = BeefyCLVaultContract.bind(Address.fromBytes(vault.id)) + const strategyAddress = Address.fromBytes(vault.strategy) + + // current prices + const currentPriceInToken1 = fetchCurrentPriceInToken1(strategyAddress, true) + const rangeToken1Price = fetchVaultPriceRangeInToken1(strategyAddress, true) + + // balances of the vault + const vaultBalancesRes = vaultContract.try_balances() + if (vaultBalancesRes.reverted) { + log.error("handleStrategyChargedFees: balances() reverted for strategy {}", [vault.strategy.toHexString()]) + throw Error("handleStrategyChargedFees: balances() reverted") + } + const vaultBalanceUnderlying0 = tokenAmountToDecimal(vaultBalancesRes.value.value0, token0.decimals) + const vaultBalanceUnderlying1 = tokenAmountToDecimal(vaultBalancesRes.value.value1, token1.decimals) + + // preview withdraw of 1 share token + let previewWithdraw0Raw = BigInt.fromI32(0) + let previewWithdraw1Raw = BigInt.fromI32(0) + if (vaultBalanceUnderlying0.gt(ZERO_BD) || vaultBalanceUnderlying1.gt(ZERO_BD)) { + const previewWithdrawRes = vaultContract.try_previewWithdraw(decimalToTokenAmount(ONE_BD, sharesToken.decimals)) + if (previewWithdrawRes.reverted) { + log.error("handleStrategyChargedFees: previewWithdraw() reverted for vault {}", [vault.id.toHexString()]) + throw Error("handleStrategyChargedFees: previewWithdraw() reverted") + } + previewWithdraw0Raw = previewWithdrawRes.value.value0 + previewWithdraw1Raw = previewWithdrawRes.value.value1 + } + let shareTokenToUnderlying0Rate = tokenAmountToDecimal(previewWithdraw0Raw, token0.decimals) + let shareTokenToUnderlying1Rate = tokenAmountToDecimal(previewWithdraw1Raw, token1.decimals) + + const prices = fetchVaultPrices(vault, strategy, token0, token1) + const token0PriceInNative = prices.token0ToNative + const token1PriceInNative = prices.token1ToNative + const nativePriceUSD = prices.nativeToUsd + + /////// + // compute derived values + log.debug("handleStrategyChargedFees: computing derived values for vault {}", [vault.id.toHexString()]) + const token0PriceInUSD = token0PriceInNative.times(nativePriceUSD) + const token1PriceInUSD = token1PriceInNative.times(nativePriceUSD) + const collectedAmount0 = tokenAmountToDecimal(event.params.feeAlt0.plus(event.params.feeMain0), token0.decimals) + const collectedAmount1 = tokenAmountToDecimal(event.params.feeAlt1.plus(event.params.feeMain1), token1.decimals) + + /////// + // store the raw collect event + log.debug("handleStrategyChargedFees: updating vault entities for vault {}", [vault.id.toHexString()]) + let collect = new BeefyCLVaultUnderlyingFeesCollectedEvent(getEventIdentifier(event)) + collect.vault = vault.id + collect.strategy = strategy.id + collect.createdWith = tx.id + collect.timestamp = event.block.timestamp + collect.underlyingAmount0 = vaultBalanceUnderlying0 + collect.underlyingAmount1 = vaultBalanceUnderlying1 + collect.underlyingAmount0USD = vaultBalanceUnderlying0.times(token0PriceInUSD) + collect.underlyingAmount1USD = vaultBalanceUnderlying1.times(token1PriceInUSD) + collect.totalValueLockedUSD = vaultBalanceUnderlying0 + .times(token0PriceInUSD) + .plus(vaultBalanceUnderlying1.times(token1PriceInUSD)) + collect.collectedAmount0 = collectedAmount0 + collect.collectedAmount1 = collectedAmount1 + collect.collectedAmount0USD = collectedAmount0.times(token0PriceInUSD) + collect.collectedAmount1USD = collectedAmount1.times(token1PriceInUSD) + collect.collectedValueUSD = collect.collectedAmount0USD.plus(collect.collectedAmount1USD) + collect.priceOfToken0InToken1 = currentPriceInToken1 + collect.priceOfToken0InUSD = currentPriceInToken1.times(token1PriceInUSD) + collect.save() + + /////// + // update vault entities + vault.currentPriceOfToken0InToken1 = currentPriceInToken1 + vault.currentPriceOfToken0InUSD = currentPriceInToken1.times(token1PriceInUSD) + vault.priceRangeMin1 = rangeToken1Price.min + vault.priceRangeMax1 = rangeToken1Price.max + vault.priceRangeMinUSD = vault.priceRangeMin1.times(token1PriceInUSD) + vault.priceRangeMaxUSD = vault.priceRangeMax1.times(token1PriceInUSD) + vault.underlyingAmount0 = vaultBalanceUnderlying0 + vault.underlyingAmount1 = vaultBalanceUnderlying1 + vault.underlyingAmount0USD = vault.underlyingAmount0.times(token0PriceInUSD) + vault.underlyingAmount1USD = vault.underlyingAmount1.times(token1PriceInUSD) + vault.totalValueLockedUSD = vault.underlyingAmount0USD.plus(vault.underlyingAmount1USD) + vault.save() + for (let i = 0; i < periods.length; i++) { + log.debug("handleStrategyChargedFees: updating vault snapshot for vault {} and period {}", [ + vault.id.toHexString(), + periods[i].toString(), + ]) + const vaultSnapshot = getBeefyCLVaultSnapshot(vault, event.block.timestamp, periods[i]) + vaultSnapshot.currentPriceOfToken0InToken1 = vault.currentPriceOfToken0InToken1 + vaultSnapshot.currentPriceOfToken0InUSD = vault.currentPriceOfToken0InUSD + vaultSnapshot.priceRangeMin1 = vault.priceRangeMin1 + vaultSnapshot.priceRangeMax1 = vault.priceRangeMax1 + vaultSnapshot.priceRangeMinUSD = vault.priceRangeMinUSD + vaultSnapshot.priceRangeMaxUSD = vault.priceRangeMaxUSD + vaultSnapshot.underlyingAmount0 = vault.underlyingAmount0 + vaultSnapshot.underlyingAmount1 = vault.underlyingAmount1 + vaultSnapshot.underlyingAmount0USD = vault.underlyingAmount0USD + vaultSnapshot.underlyingAmount1USD = vault.underlyingAmount1USD + vaultSnapshot.totalValueLockedUSD = vault.totalValueLockedUSD + vaultSnapshot.save() + } + + /////// + // update investor positions + log.debug("handleStrategyChargedFees: updating investor positions for vault {}", [vault.id.toHexString()]) + let positions = vault.positions.load() + let positivePositionCount = 0 + for (let i = 0; i < positions.length; i++) { + let position = positions[i] + if (!position.sharesBalance.gt(ZERO_BD)) { + continue + } + positivePositionCount += 1 + + log.debug("handleStrategyChargedFees: updating investor position for investor {}", [ + position.investor.toHexString(), + ]) + let investor = getInvestor(position.investor) + position.underlyingBalance0 = position.sharesBalance.times(shareTokenToUnderlying0Rate) + position.underlyingBalance1 = position.sharesBalance.times(shareTokenToUnderlying1Rate) + position.underlyingBalance0USD = position.underlyingBalance0.times(token0PriceInUSD) + position.underlyingBalance1USD = position.underlyingBalance1.times(token1PriceInUSD) + const previousPositionValueUSD = position.positionValueUSD + position.positionValueUSD = position.underlyingBalance0USD.plus(position.underlyingBalance1USD) + const positionChangeUSD = position.positionValueUSD.minus(previousPositionValueUSD) + position.save() + for (let i = 0; i < periods.length; i++) { + log.debug("handleStrategyChargedFees: updating investor position snapshot for investor {} and period {}", [ + position.investor.toHexString(), + periods[i].toString(), + ]) + const positionSnapshot = getInvestorPositionSnapshot(vault, investor, event.block.timestamp, periods[i]) + positionSnapshot.sharesBalance = position.sharesBalance + positionSnapshot.underlyingBalance0 = position.underlyingBalance0 + positionSnapshot.underlyingBalance1 = position.underlyingBalance1 + positionSnapshot.underlyingBalance0USD = position.underlyingBalance0USD + positionSnapshot.underlyingBalance1USD = position.underlyingBalance1USD + positionSnapshot.positionValueUSD = position.positionValueUSD + positionSnapshot.save() + } + + log.debug("handleStrategyChargedFees: updating investor for investor {}", [position.investor.toHexString()]) + investor.totalPositionValueUSD = investor.totalPositionValueUSD.plus(positionChangeUSD) + investor.save() + for (let i = 0; i < periods.length; i++) { + log.debug("handleStrategyChargedFees: updating investor snapshot for investor {} and period {}", [ + position.investor.toHexString(), + periods[i].toString(), + ]) + const investorSnapshot = getInvestorSnapshot(investor, event.block.timestamp, periods[i]) + investorSnapshot.totalPositionValueUSD = investor.totalPositionValueUSD + investorSnapshot.save() + } + } + + /////// + // update protocol entities + log.debug("handleStrategyChargedFees: updating protocol entities for vault {}", [vault.id.toHexString()]) + const protocol = getBeefyCLProtocol() + // this is not exact but will be corrected by the next clock tick + protocol.totalValueLockedUSD = protocol.totalValueLockedUSD.plus(collect.collectedValueUSD) + protocol.cumulativeTransactionCount += 1 + protocol.save() + for (let i = 0; i < periods.length; i++) { + log.debug("handleStrategyChargedFees: updating protocol snapshot for period {}", [periods[i].toString()]) + const protocolSnapshot = getBeefyCLProtocolSnapshot(event.block.timestamp, periods[i]) + protocolSnapshot.totalValueLockedUSD = protocol.totalValueLockedUSD + protocolSnapshot.transactionCount += 1 + protocolSnapshot.save() + } +} diff --git a/src/vault-fees.ts b/src/vault-fees.ts index 5f64f14..8959d38 100644 --- a/src/vault-fees.ts +++ b/src/vault-fees.ts @@ -10,11 +10,11 @@ import { getBeefyCLProtocolSnapshot } from "./entity/protocol" import { weiToBigDecimal } from "./utils/decimal" import { fetchNativePriceUSD } from "./utils/price" -export function handleChargedFees(event: ChargedFees): void { +export function handleStrategyChargedFees(event: ChargedFees): void { let strategy = getBeefyCLStrategy(event.address) let vault = getBeefyCLVault(strategy.vault) if (!isVaultRunning(vault)) { - log.error("updateUserPosition: vault {} not active at block {}: {}", [ + log.error("handleStrategyChargedFees: vault {} not active at block {}: {}", [ vault.id.toHexString(), event.block.number.toString(), vault.lifecycle, @@ -22,7 +22,7 @@ export function handleChargedFees(event: ChargedFees): void { return } - log.info("handleChargedFees: vault {}", [vault.id.toHexString()]) + log.info("handleStrategyChargedFees: vault {}", [vault.id.toHexString()]) const periods = SNAPSHOT_PERIODS const sharesToken = getToken(vault.sharesToken) @@ -50,9 +50,9 @@ export function handleChargedFees(event: ChargedFees): void { /////// // update protocol entities - log.debug("handleChargedFees: update protocol", []) + log.debug("handleStrategyChargedFees: update protocol", []) for (let i = 0; i < periods.length; i++) { - log.debug("updateUserPosition: updating protocol snapshot for period {}", [periods[i].toString()]) + log.debug("handleStrategyChargedFees: updating protocol snapshot for period {}", [periods[i].toString()]) const protocolSnapshot = getBeefyCLProtocolSnapshot(event.block.timestamp, periods[i]) protocolSnapshot.protocolFeesCollectedNative = protocolSnapshot.protocolFeesCollectedNative.plus(beefyFeeNative) protocolSnapshot.protocolFeesCollectedUSD = protocolSnapshot.protocolFeesCollectedUSD.plus( @@ -73,7 +73,7 @@ export function handleChargedFees(event: ChargedFees): void { /////// // update vault entities - log.debug("handleChargedFees: update vault", []) + log.debug("handleStrategyChargedFees: update vault", []) vault.cumulativeProtocolFeeCollectedNative = vault.cumulativeProtocolFeeCollectedNative.plus(beefyFeeNative) vault.cumulativeProtocolFeeCollectedUSD = vault.cumulativeProtocolFeeCollectedUSD.plus( beefyFeeNative.times(nativePriceUSD), @@ -89,7 +89,7 @@ export function handleChargedFees(event: ChargedFees): void { vault.save() for (let i = 0; i < periods.length; i++) { - log.debug("handleChargedFees: update vault snapshot for period {}", [periods[i].toString()]) + log.debug("handleStrategyChargedFees: update vault snapshot for period {}", [periods[i].toString()]) const vaultSnapshot = getBeefyCLVaultSnapshot(vault, event.block.timestamp, periods[i]) vaultSnapshot.protocolFeeCollectedNative = vaultSnapshot.protocolFeeCollectedNative.plus(beefyFeeNative) vaultSnapshot.protocolFeeCollectedUSD = vaultSnapshot.protocolFeeCollectedUSD.plus( diff --git a/subgraph.template.yaml b/subgraph.template.yaml index 1a6055a..26dc0be 100644 --- a/subgraph.template.yaml +++ b/subgraph.template.yaml @@ -158,6 +158,9 @@ templates: - event: ChargedFees(uint256,uint256,uint256) handler: handleChargedFees receipt: true + - event: ClaimedFees(uint256,uint256,uint256,uint256) + handler: handleClaimedFees + receipt: true - event: SetLpToken0ToNativePath(bytes) handler: handleSetLpToken0ToNativePath - event: SetLpToken1ToNativePath(bytes)