Skip to content

Commit

Permalink
Add investor position level harvests amount
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Mar 23, 2024
1 parent 9d28295 commit c1314e7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
26 changes: 22 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,8 @@ type Investor @entity {

"The transactions count that the investor was the sender of"
cumulativeInteractionsCount: Int!

"All deposits of the investor"
cumulativeDepositCount: Int!

"All withdraws of the investor"
cumulativeWithdrawCount: Int!

Expand Down Expand Up @@ -621,10 +619,8 @@ type InvestorSnapshot implements Snapshot @entity {

"Interactions count for the investor at the time of the snapshot"
interactionsCount: Int!

"Deposits count for the investor at the time of the snapshot"
depositCount: Int!

"Withdraws count for the investor at the time of the snapshot"
withdrawCount: Int!

Expand Down Expand Up @@ -674,6 +670,17 @@ type InvestorPosition @entity {
"The last daily position values in USD for the last 30 days. Most recent last."
last30DailyPositionValuesUSD: [BigDecimal!]!

"The harvested amount of the first token"
cumulativeHarvestedAmount0: BigDecimal!
"The harvested amount of the second token"
cumulativeHarvestedAmount1: BigDecimal!
"The harvested amount of the first token in USD"
cumulativeHarvestedAmount0USD: BigDecimal!
"The harvested amount of the second token in USD"
cumulativeHarvestedAmount1USD: BigDecimal!
"The harvested amount in USD"
cumulativeHarvestValueUSD: BigDecimal!

"All investor position snapshots"
snapshots: [InvestorPositionSnapshot!]! @derivedFrom(field: "investorPosition")

Expand Down Expand Up @@ -722,6 +729,17 @@ type InvestorPositionSnapshot implements Snapshot @entity {

"Current position value in USD"
positionValueUSD: BigDecimal!

"The harvested amount of the first token at the time of the snapshot"
harvestedAmount0: BigDecimal!
"The harvested amount of the second token at the time of the snapshot"
harvestedAmount1: BigDecimal!
"The harvested amount of the first token in USD at the time of the snapshot"
harvestedAmount0USD: BigDecimal!
"The harvested amount of the second token in USD at the time of the snapshot"
harvestedAmount1USD: BigDecimal!
"The harvested amount in USD at the time of the snapshot"
harvestValueUSD: BigDecimal!
}

enum InvestorPositionInteractionType {
Expand Down
10 changes: 10 additions & 0 deletions src/entity/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export function getInvestorPosition(vault: BeefyCLVault, investor: Investor): In
position.positionValueUSD = ZERO_BD
position.averageDailyPositionValueUSD30D = ZERO_BD
position.last30DailyPositionValuesUSD = new Array<BigDecimal>()
position.cumulativeHarvestedAmount0 = ZERO_BD
position.cumulativeHarvestedAmount1 = ZERO_BD
position.cumulativeHarvestedAmount0USD = ZERO_BD
position.cumulativeHarvestedAmount1USD = ZERO_BD
position.cumulativeHarvestValueUSD = ZERO_BD
}
return position
}
Expand All @@ -59,6 +64,11 @@ export function getInvestorPositionSnapshot(
snapshot.underlyingBalance0USD = ZERO_BD
snapshot.underlyingBalance1USD = ZERO_BD
snapshot.positionValueUSD = ZERO_BD
snapshot.harvestedAmount0 = ZERO_BD
snapshot.harvestedAmount1 = ZERO_BD
snapshot.harvestedAmount0USD = ZERO_BD
snapshot.harvestedAmount1USD = ZERO_BD
snapshot.harvestValueUSD = ZERO_BD
}

// copy non-reseting values from the previous snapshot to the new snapshot
Expand Down
36 changes: 36 additions & 0 deletions src/harvest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export function handleStrategyHarvest(event: HarvestEvent): void {
const vaultBalanceUnderlying0 = tokenAmountToDecimal(vaultBalancesRes.value.value0, token0.decimals)
const vaultBalanceUnderlying1 = tokenAmountToDecimal(vaultBalancesRes.value.value1, token1.decimals)

// vault shares total supply
const sharesTotalSupplyRes = vaultContract.try_totalSupply()
if (sharesTotalSupplyRes.reverted) {
log.error("handleStrategyHarvest: totalSupply() reverted for vault {}", [vault.id.toHexString()])
throw Error("handleStrategyHarvest: totalSupply() reverted")
}
const sharesTotalSupply = tokenAmountToDecimal(sharesTotalSupplyRes.value, sharesToken.decimals)

// preview withdraw of 1 share token
let previewWithdraw0Raw = BigInt.fromI32(0)
let previewWithdraw1Raw = BigInt.fromI32(0)
Expand Down Expand Up @@ -162,6 +170,8 @@ export function handleStrategyHarvest(event: HarvestEvent): void {
}
positivePositionCount += 1

const positionPercentOfTotalSupply = position.sharesBalance.div(sharesTotalSupply)

log.debug("handleStrategyHarvest: updating investor position for investor {}", [position.investor.toHexString()])
let investor = getInvestor(position.investor)
position.underlyingBalance0 = position.sharesBalance.times(shareTokenToUnderlying0Rate)
Expand All @@ -171,6 +181,19 @@ export function handleStrategyHarvest(event: HarvestEvent): void {
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),
)
position.cumulativeHarvestedAmount1 = position.cumulativeHarvestedAmount1.plus(
harvest.harvestedAmount1.times(positionPercentOfTotalSupply),
)
position.cumulativeHarvestedAmount0USD = position.cumulativeHarvestedAmount0USD.plus(
harvest.harvestedAmount0USD.times(positionPercentOfTotalSupply),
)
position.cumulativeHarvestedAmount1USD = position.cumulativeHarvestedAmount1USD.plus(
harvest.harvestedAmount1USD.times(positionPercentOfTotalSupply),
)
position.cumulativeHarvestValueUSD = position.cumulativeHarvestValueUSD.plus(positionChangeUSD)
position.save()
for (let i = 0; i < periods.length; i++) {
log.debug("handleStrategyHarvest: updating investor position snapshot for investor {} and period {}", [
Expand All @@ -184,6 +207,19 @@ export function handleStrategyHarvest(event: HarvestEvent): void {
positionSnapshot.underlyingBalance0USD = position.underlyingBalance0USD
positionSnapshot.underlyingBalance1USD = position.underlyingBalance1USD
positionSnapshot.positionValueUSD = position.positionValueUSD
positionSnapshot.harvestedAmount0 = positionSnapshot.harvestedAmount0.plus(
harvest.harvestedAmount0.times(positionPercentOfTotalSupply),
)
positionSnapshot.harvestedAmount1 = positionSnapshot.harvestedAmount1.plus(
harvest.harvestedAmount1.times(positionPercentOfTotalSupply),
)
positionSnapshot.harvestedAmount0USD = positionSnapshot.harvestedAmount0USD.plus(
harvest.harvestedAmount0USD.times(positionPercentOfTotalSupply),
)
positionSnapshot.harvestedAmount1USD = positionSnapshot.harvestedAmount1USD.plus(
harvest.harvestedAmount1USD.times(positionPercentOfTotalSupply),
)
positionSnapshot.harvestValueUSD = positionSnapshot.harvestValueUSD.plus(positionChangeUSD)
positionSnapshot.save()
}

Expand Down

0 comments on commit c1314e7

Please sign in to comment.