Skip to content

Commit

Permalink
Add reward pool data to classic vaults
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Jul 15, 2024
1 parent 608a7d9 commit feb30c5
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 121 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
},
"lint-staged": {
"*.*": "prettier --write"
}
},
"packageManager": "[email protected]"
}
57 changes: 57 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,20 @@ type Classic @entity {
strategy: ClassicStrategy!
"The vault boosts"
boosts: [ClassicBoost!]! @derivedFrom(field: "classic")
"""
The reward pools of this vault.
"""
rewardPools: [ClassicRewardPool!]! @derivedFrom(field: "classic")

"The current lifecycle status of the vault"
lifecycle: ProductLifecycle!

"The vault's share token"
vaultSharesToken: Token!
"The reward pool tokens of the CLM. This is where the CLM's earnings are sent if the CLM does not automatically compound."
rewardPoolTokens: [Token!]!
"The reward pool token addresses of the CLM. This is the source of truth for other tables reward ordering."
rewardPoolTokensOrder: [Bytes!]!

"The vault's underlying LP token"
underlyingToken: Token!
Expand All @@ -133,16 +141,24 @@ type Classic @entity {
boostRewardTokens: [Token!]!
"The boost token addresses of the vault. This is the source of truth for other tables reward ordering."
boostRewardTokensOrder: [Bytes!]!
"The reward tokens of the CLM's rewardpool. These will be rewarded to positions when the CLM earns non-compounding yield."
rewardTokens: [Token!]!
"The reward token addresses of the CLM. This is the source of truth for other tables reward ordering."
rewardTokensOrder: [Bytes!]!

# ----- PRICES & STATS -----

"The total supply of the vault shares token in circulation. Express with `sharesToken.decimals` decimals."
vaultSharesTotalSupply: BigInt!
"Total supply of the reward pool token. Express with `clm.rewardTokensOrder[idx].decimals` decimals."
rewardPoolsTotalSupply: [BigInt!]!

"Latest LP token price in native we have seen. Expressed with 18 decimals."
underlyingToNativePrice: BigInt!
"Latest boost token prices in native we have seen. Ordered by classic.boostRewardTokensOrder. Expressed with 18 decimals."
boostRewardToNativePrices: [BigInt!]!
"Latest reward token prices in native we have seen. Ordered by classic.rewardTokensOrder. Expressed with 18 decimals."
rewardToNativePrices: [BigInt!]!
"Latest native token price we have seen. Expressed with 18 decimals."
nativeToUSDPrice: BigInt!

Expand Down Expand Up @@ -190,11 +206,15 @@ type ClassicSnapshot @entity {

"The total supply of the vault shares token in circulation. Express with `sharesToken.decimals` decimals."
vaultSharesTotalSupply: BigInt!
"Total supply of the reward pool token. Express with `clm.rewardTokensOrder[idx].decimals` decimals."
rewardPoolsTotalSupply: [BigInt!]!

"Latest LP token price in native of this snapshot. Expressed with 18 decimals."
underlyingToNativePrice: BigInt!
"Latest boost token prices in native of this snapshot. Ordered by classic.boostRewardTokensOrder. Expressed with 18 decimals."
boostRewardToNativePrices: [BigInt!]!
"Latest reward token prices in native of this snapshot. Ordered by classic.rewardTokensOrder. Expressed with 18 decimals."
rewardToNativePrices: [BigInt!]!
"Latest native token price of this snapshot. Expressed with 18 decimals."
nativeToUSDPrice: BigInt!

Expand Down Expand Up @@ -253,6 +273,23 @@ type ClassicBoost @entity {
isInitialized: Boolean!
}

"""
Some Classic do not automatically compound their earnings. Those earnings are sent to the reward pool instead.
This entity is mostly used to start tracking the events and link them to the Classic on new event
"""
type ClassicRewardPool @entity {
"The strategy address"
id: Bytes!
"The classic product the reward pool is for"
classic: Classic!
"The vault the reward pool is linked to"
vault: ClassicVault!
"The transaction that created the reward pool"
createdWith: Transaction!
"Technical field to remember if the strategy was already initialized"
isInitialized: Boolean!
}

"""
Classic products are harvested by the strategy. This event is emitted when the strategy harvests the vault.
"""
Expand Down Expand Up @@ -281,11 +318,15 @@ type ClassicHarvestEvent @entity(immutable: true) {

"Total amount of liquidity in the vault at time of harvest"
vaultSharesTotalSupply: BigInt!
"Total amount of reward pool shares at time of harvest. Ordered by classic.rewardPoolTokensOrder."
rewardPoolsTotalSupply: [BigInt!]!

"LP token price in native at the time of harvest. Expressed with 18 decimals."
underlyingToNativePrice: BigInt!
"Boost token prices in native at the time of harvest. Ordered by classic.boostRewardTokensOrder. Expressed with 18 decimals."
boostRewardToNativePrices: [BigInt!]!
"Reward token prices in native at the time of harvest. Ordered by classic.rewardTokensOrder. Expressed with 18 decimals."
rewardToNativePrices: [BigInt!]!
"Native token price at the time of harvest. Expressed with 18 decimals."
nativeToUSDPrice: BigInt!
}
Expand All @@ -308,6 +349,8 @@ type ClassicPosition @entity {
vaultBalance: BigInt!
"The amount of vault shares the investor holds in a boost"
boostBalance: BigInt!
"Amount of reward pool shares the investor holds. Ordered by clm.rewardPoolTokensOrder."
rewardPoolBalances: [BigInt!]!
"Total amount of vault shares the investor holds. Should always equal vaultBalance + boostBalance. This is mostly used for filtering."
totalBalance: BigInt!

Expand All @@ -326,6 +369,12 @@ enum ClassicPositionInteractionType {
BOOST_UNSTAKE
"The investor claimed their rewards from the vault"
BOOST_REWARD_CLAIM
"The investor staked in the reward pool of the CLM and received reward pool shares"
REWARD_POOL_STAKE
"The investor unstaked from the reward pool of the CLM and received underlying tokens"
REWARD_POOL_UNSTAKE
"The investor claimed their rewards from the reward pool of the CLM"
REWARD_POOL_CLAIM
}

type ClassicPositionInteraction @entity(immutable: true) {
Expand Down Expand Up @@ -354,6 +403,8 @@ type ClassicPositionInteraction @entity(immutable: true) {
vaultBalance: BigInt!
"The amount of vault shares the investor holds in a boost at the time of the interaction"
boostBalance: BigInt!
"The amount of reward pool shares the investor holds at the time of the interaction. Ordered by clm.rewardPoolTokensOrder."
rewardPoolBalances: [BigInt!]!
"Total amount of vault shares the investor holds. Should always equal vaultBalance + boostBalance. This is mostly used for filtering."
totalBalance: BigInt!

Expand All @@ -363,11 +414,17 @@ type ClassicPositionInteraction @entity(immutable: true) {
boostBalanceDelta: BigInt!
"Amount of boost tokens change in the interaction. Ordered by classic.boostRewardTokensOrder."
boostRewardBalancesDelta: [BigInt!]!
"Amount of reward pool shares change in the interaction. Ordered by clm.rewardPoolTokensOrder."
rewardPoolBalancesDelta: [BigInt!]!
"Amount of reward tokens change in the interaction. Ordered by clm.rewardTokensOrder."
rewardBalancesDelta: [BigInt!]!

"LP token price in native at the time of the interaction. Expressed with 18 decimals."
underlyingToNativePrice: BigInt!
"Boost token prices in native at the time of the interaction. Ordered by classic.boostRewardTokensOrder. Expressed with 18 decimals."
boostRewardToNativePrices: [BigInt!]!
"Reward token prices in native at the time of the interaction. Expressed with 18 decimals. Ordered by clm.rewardTokensOrder."
rewardToNativePrices: [BigInt!]!
"Native token price at the time of the interaction. Expressed with 18 decimals."
nativeToUSDPrice: BigInt!
}
Expand Down
38 changes: 37 additions & 1 deletion src/classic/entity/classic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { BigInt, Bytes } from "@graphprotocol/graph-ts"
import { Classic, ClassicVault, ClassicStrategy, ClassicBoost, ClassicSnapshot } from "../../../generated/schema"
import {
Classic,
ClassicVault,
ClassicStrategy,
ClassicBoost,
ClassicSnapshot,
ClassicRewardPool,
} from "../../../generated/schema"
import { ADDRESS_ZERO } from "../../common/utils/address"
import { ZERO_BI } from "../../common/utils/decimal"
import { getIntervalFromTimestamp } from "../../common/utils/time"
Expand All @@ -11,6 +18,10 @@ export function isClassicInitialized(classic: Classic): boolean {
return classic.lifecycle != PRODUCT_LIFECYCLE_INITIALIZING
}

export function isClassicVaultAddress(vaultAddress: Bytes): boolean {
return ClassicVault.load(vaultAddress) != null
}

export function getClassic(vaultAddress: Bytes): Classic {
let classic = Classic.load(vaultAddress)
if (!classic) {
Expand All @@ -26,11 +37,17 @@ export function getClassic(vaultAddress: Bytes): Classic {
classic.underlyingToken = ADDRESS_ZERO
classic.boostRewardTokens = []
classic.boostRewardTokensOrder = []
classic.rewardPoolTokens = []
classic.rewardPoolTokensOrder = []
classic.rewardTokens = []
classic.rewardTokensOrder = []

classic.vaultSharesTotalSupply = ZERO_BI
classic.rewardPoolsTotalSupply = []

classic.underlyingToNativePrice = ZERO_BI
classic.boostRewardToNativePrices = []
classic.rewardToNativePrices = []
classic.nativeToUSDPrice = ZERO_BI

classic.underlyingAmount = ZERO_BI
Expand Down Expand Up @@ -74,6 +91,22 @@ export function getClassicBoost(boostAddress: Bytes): ClassicBoost {
return boost
}

export function isClassicRewardPool(rewardPoolAddress: Bytes): boolean {
return ClassicRewardPool.load(rewardPoolAddress) != null
}

export function getClassicRewardPool(rewardPoolAddress: Bytes): ClassicRewardPool {
let rewardPool = ClassicRewardPool.load(rewardPoolAddress)
if (!rewardPool) {
rewardPool = new ClassicRewardPool(rewardPoolAddress)
rewardPool.classic = ADDRESS_ZERO
rewardPool.vault = ADDRESS_ZERO
rewardPool.createdWith = ADDRESS_ZERO
rewardPool.isInitialized = false
}
return rewardPool
}

export function getClassicSnapshot(classic: Classic, timestamp: BigInt, period: BigInt): ClassicSnapshot {
const interval = getIntervalFromTimestamp(timestamp, period)
const snapshotId = classic.id.concat(getSnapshotIdSuffix(period, interval))
Expand All @@ -87,9 +120,11 @@ export function getClassicSnapshot(classic: Classic, timestamp: BigInt, period:
snapshot.roundedTimestamp = interval

snapshot.vaultSharesTotalSupply = ZERO_BI
snapshot.rewardPoolsTotalSupply = []

snapshot.underlyingToNativePrice = ZERO_BI
snapshot.boostRewardToNativePrices = []
snapshot.rewardToNativePrices = []
snapshot.nativeToUSDPrice = ZERO_BI

snapshot.underlyingAmount = ZERO_BI
Expand All @@ -101,6 +136,7 @@ export function getClassicSnapshot(classic: Classic, timestamp: BigInt, period:
snapshot.vaultSharesTotalSupply = previousSnapshot.vaultSharesTotalSupply
snapshot.underlyingToNativePrice = previousSnapshot.underlyingToNativePrice
snapshot.boostRewardToNativePrices = previousSnapshot.boostRewardToNativePrices
snapshot.rewardToNativePrices = previousSnapshot.rewardToNativePrices
snapshot.nativeToUSDPrice = previousSnapshot.nativeToUSDPrice
snapshot.underlyingAmount = previousSnapshot.underlyingAmount
}
Expand Down
1 change: 1 addition & 0 deletions src/classic/entity/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function getClassicPosition(classic: Classic, investor: Investor): Classi
position.createdWith = ADDRESS_ZERO
position.vaultBalance = ZERO_BI
position.boostBalance = ZERO_BI
position.rewardPoolBalances = []
position.totalBalance = ZERO_BI
}
return position
Expand Down
Loading

0 comments on commit feb30c5

Please sign in to comment.