Skip to content

Commit

Permalink
Add vault active investor count
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Apr 20, 2024
1 parent 1381b1f commit 8572997
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
6 changes: 6 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ type BeefyCLVault @entity {
"The token the vault is earning."
earnedToken: Token!

"The current number of invested investors in the protocol"
activeInvestorCount: Int!

"""
The current price of the token zero expressed as a token1 value.
For example, if the vault is a BTC/ETH vault, this is the price of 1 BTC in ETH
Expand Down Expand Up @@ -367,6 +370,9 @@ type BeefyCLVaultSnapshot implements Snapshot @entity {
"Actual timestamp snapshot was initiated at"
timestamp: BigInt!

"Active investor count at the time of the snapshot"
activeInvestorCount: Int!

"""
The current price of the token zero expressed as a token1 value.
For example, if the vault is a BTC/ETH vault, this is the price of 1 BTC in ETH
Expand Down
3 changes: 3 additions & 0 deletions src/entity/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function getBeefyCLVault(vaultAddress: Bytes): BeefyCLVault {
vault.underlyingToken0 = ADDRESS_ZERO
vault.underlyingToken1 = ADDRESS_ZERO
vault.earnedToken = ADDRESS_ZERO
vault.activeInvestorCount = 0
vault.currentPriceOfToken0InToken1 = ZERO_BD
vault.currentPriceOfToken0InUSD = ZERO_BD
vault.priceRangeMin1 = ZERO_BD
Expand Down Expand Up @@ -87,6 +88,7 @@ export function getBeefyCLVaultSnapshot(vault: BeefyCLVault, timestamp: BigInt,
snapshot.timestamp = timestamp
snapshot.roundedTimestamp = interval
snapshot.period = period
snapshot.activeInvestorCount = 0
snapshot.currentPriceOfToken0InToken1 = ZERO_BD
snapshot.currentPriceOfToken0InUSD = ZERO_BD
snapshot.priceRangeMin1 = ZERO_BD
Expand Down Expand Up @@ -121,6 +123,7 @@ export function getBeefyCLVaultSnapshot(vault: BeefyCLVault, timestamp: BigInt,
const previousSnapshotId = vault.id.concat(getPreviousSnapshotIdSuffix(period, interval))
const previousSnapshot = BeefyCLVaultSnapshot.load(previousSnapshotId)
if (previousSnapshot) {
snapshot.activeInvestorCount = previousSnapshot.activeInvestorCount
snapshot.currentPriceOfToken0InToken1 = previousSnapshot.currentPriceOfToken0InToken1
snapshot.currentPriceOfToken0InUSD = previousSnapshot.currentPriceOfToken0InUSD
snapshot.priceRangeMin1 = previousSnapshot.priceRangeMin1
Expand Down
78 changes: 41 additions & 37 deletions src/vault-interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,45 @@ function updateUserPosition(
positionInteraction.positionValueUSDDelta = positionValueUSDDelta
positionInteraction.save()

///////
// update investor entities
if (isNewPosition) investor.activePositionCount += 1
if (isClosingPosition) investor.activePositionCount -= 1
const isEnteringTheProtocol = newInvestor || (isNewPosition && investor.activePositionCount === 1)
if (isEnteringTheProtocol) investor.currentInvestmentOpenAtTimestamp = event.block.timestamp
const isExitingTheProtocol = investor.activePositionCount > 0
if (!isExitingTheProtocol) {
investor.closedInvestmentDuration = investor.closedInvestmentDuration.plus(
event.block.timestamp.minus(investor.currentInvestmentOpenAtTimestamp),
)
investor.currentInvestmentOpenAtTimestamp = ZERO_BI
}
investor.lastInteractionAt = event.block.timestamp
investor.totalPositionValueUSD = investor.totalPositionValueUSD.plus(positionValueUSDDelta)
investor.cumulativeInteractionsCount += 1
if (!isTransfer && isDeposit) investor.cumulativeDepositCount += 1
if (!isTransfer && !isDeposit) investor.cumulativeWithdrawCount += 1
dailyAvgState = DailyAvgState.deserialize(investor.averageDailyTotalPositionValueUSDState)
dailyAvgState.setPendingValue(investor.totalPositionValueUSD, event.block.timestamp)
investor.averageDailyTotalPositionValueUSD30D = DailyAvgCalc.avg(BigInt.fromI32(30), dailyAvgState)
investor.averageDailyTotalPositionValueUSDState = DailyAvgCalc.evictOldEntries(
BigInt.fromU32(30),
dailyAvgState,
).serialize()
investor.save()
for (let i = 0; i < periods.length; i++) {
const investorSnapshot = getInvestorSnapshot(investor, event.block.timestamp, periods[i])
investorSnapshot.totalPositionValueUSD = investor.totalPositionValueUSD
investorSnapshot.interactionsCount += 1
if (!isTransfer && isDeposit) investorSnapshot.depositCount += 1
if (!isTransfer && !isDeposit) investorSnapshot.withdrawCount += 1
investorSnapshot.save()
}

///////
// update vault entities
if (isNewPosition) vault.activeInvestorCount += 1
if (isClosingPosition) vault.activeInvestorCount -= 1
vault.currentPriceOfToken0InToken1 = currentPriceInToken1
vault.currentPriceOfToken0InUSD = currentPriceInToken1.times(token1PriceInUSD)
vault.priceRangeMin1 = rangeMinToken1Price
Expand All @@ -219,6 +256,7 @@ function updateUserPosition(
vault.save()
for (let i = 0; i < periods.length; i++) {
const vaultSnapshot = getBeefyCLVaultSnapshot(vault, event.block.timestamp, periods[i])
vaultSnapshot.activeInvestorCount = vault.activeInvestorCount
vaultSnapshot.currentPriceOfToken0InToken1 = vault.currentPriceOfToken0InToken1
vaultSnapshot.currentPriceOfToken0InUSD = vault.currentPriceOfToken0InUSD
vaultSnapshot.priceRangeMin1 = vault.priceRangeMin1
Expand All @@ -241,13 +279,14 @@ function updateUserPosition(
if (!isTransfer || isDeposit) protocol.cumulativeTransactionCount += 1
if (!isTransfer || isDeposit) protocol.cumulativeInvestorInteractionsCount += 1
protocol.totalValueLockedUSD = protocol.totalValueLockedUSD.plus(positionValueUSDDelta)
if (isNewPosition) protocol.activeInvestorCount += 1
if (isEnteringTheProtocol) protocol.activeInvestorCount += 1
else if (isExitingTheProtocol) protocol.activeInvestorCount -= 1
protocol.save()
for (let i = 0; i < periods.length; i++) {
const period = periods[i]
const protocolSnapshot = getBeefyCLProtocolSnapshot(event.block.timestamp, period)
protocolSnapshot.totalValueLockedUSD = protocol.totalValueLockedUSD
if (newInvestor) protocolSnapshot.newInvestorCount += 1
if (isEnteringTheProtocol) protocolSnapshot.newInvestorCount += 1
if (investor.lastInteractionAt.lt(protocolSnapshot.roundedTimestamp))
protocolSnapshot.uniqueActiveInvestorCount += 1
if (!isTransfer || isDeposit) protocolSnapshot.transactionCount += 1
Expand All @@ -258,39 +297,4 @@ function updateUserPosition(
protocolSnapshot.investorGasSpentUSD = protocolSnapshot.investorGasSpentUSD.plus(txGasFeeUSD)
protocolSnapshot.save()
}

///////
// update investor entities
if (isNewPosition) investor.activePositionCount += 1
if (isClosingPosition) investor.activePositionCount -= 1
const isEnteringTheProtocol = newInvestor || (isNewPosition && investor.activePositionCount === 1)
if (isEnteringTheProtocol) investor.currentInvestmentOpenAtTimestamp = event.block.timestamp
const isExitingTheProtocol = investor.activePositionCount > 0
if (!isExitingTheProtocol) {
investor.closedInvestmentDuration = investor.closedInvestmentDuration.plus(
event.block.timestamp.minus(investor.currentInvestmentOpenAtTimestamp),
)
investor.currentInvestmentOpenAtTimestamp = ZERO_BI
}
investor.lastInteractionAt = event.block.timestamp
investor.totalPositionValueUSD = investor.totalPositionValueUSD.plus(positionValueUSDDelta)
investor.cumulativeInteractionsCount += 1
if (!isTransfer && isDeposit) investor.cumulativeDepositCount += 1
if (!isTransfer && !isDeposit) investor.cumulativeWithdrawCount += 1
dailyAvgState = DailyAvgState.deserialize(investor.averageDailyTotalPositionValueUSDState)
dailyAvgState.setPendingValue(investor.totalPositionValueUSD, event.block.timestamp)
investor.averageDailyTotalPositionValueUSD30D = DailyAvgCalc.avg(BigInt.fromI32(30), dailyAvgState)
investor.averageDailyTotalPositionValueUSDState = DailyAvgCalc.evictOldEntries(
BigInt.fromU32(30),
dailyAvgState,
).serialize()
investor.save()
for (let i = 0; i < periods.length; i++) {
const investorSnapshot = getInvestorSnapshot(investor, event.block.timestamp, periods[i])
investorSnapshot.totalPositionValueUSD = investor.totalPositionValueUSD
investorSnapshot.interactionsCount += 1
if (!isTransfer && isDeposit) investorSnapshot.depositCount += 1
if (!isTransfer && !isDeposit) investorSnapshot.withdrawCount += 1
investorSnapshot.save()
}
}

0 comments on commit 8572997

Please sign in to comment.