Skip to content

Commit

Permalink
Add vault current price in usd
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Mar 23, 2024
1 parent 8850955 commit 039a6b5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ yarn test:lint # run prettier linter
- feat: add APR
- feat: add P&L
- feat: handle boosts
- feat: handle zap?
- feat: fetch vault fees %?
-
9 changes: 9 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ type BeefyCLVault @entity {
For example, if the vault is a BTC/ETH vault, this is the price of 1 BTC in ETH
"""
currentPriceOfToken0InToken1: BigDecimal!
"""
Current price of the token zero in USD
"""
currentPriceOfToken0InUSD: BigDecimal!

"Price range start this vault is currently configured to operate in, in token 1"
priceRangeMin1: BigDecimal!
Expand Down Expand Up @@ -352,6 +356,11 @@ type BeefyCLVaultSnapshot implements Snapshot @entity {
"""
currentPriceOfToken0InToken1: BigDecimal!

"""
Current price of the token zero in USD
"""
currentPriceOfToken0InUSD: BigDecimal!

"Price range start this vault is currently configured to operate in, in token 1"
priceRangeMin1: BigDecimal!
"Price range end this vault is currently configured to operate in, in token 1"
Expand Down
3 changes: 3 additions & 0 deletions src/entity/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function getBeefyCLVault(vaultAddress: Bytes): BeefyCLVault {
vault.underlyingToken0 = ADDRESS_ZERO
vault.underlyingToken1 = ADDRESS_ZERO
vault.currentPriceOfToken0InToken1 = ZERO_BD
vault.currentPriceOfToken0InUSD = ZERO_BD
vault.priceRangeMin1 = ZERO_BD
vault.priceRangeMax1 = ZERO_BD
vault.priceRangeMinUSD = ZERO_BD
Expand Down Expand Up @@ -78,6 +79,7 @@ export function getBeefyCLVaultSnapshot(vault: BeefyCLVault, timestamp: BigInt,
snapshot.roundedTimestamp = interval
snapshot.period = period
snapshot.currentPriceOfToken0InToken1 = ZERO_BD
snapshot.currentPriceOfToken0InUSD = ZERO_BD
snapshot.priceRangeMin1 = ZERO_BD
snapshot.priceRangeMax1 = ZERO_BD
snapshot.priceRangeMinUSD = ZERO_BD
Expand Down Expand Up @@ -109,6 +111,7 @@ export function getBeefyCLVaultSnapshot(vault: BeefyCLVault, timestamp: BigInt,
const previousSnapshot = BeefyCLVaultSnapshot.load(previousSnapshotId)
if (previousSnapshot) {
snapshot.currentPriceOfToken0InToken1 = previousSnapshot.currentPriceOfToken0InToken1
snapshot.currentPriceOfToken0InUSD = previousSnapshot.currentPriceOfToken0InUSD
snapshot.priceRangeMin1 = previousSnapshot.priceRangeMin1
snapshot.priceRangeMax1 = previousSnapshot.priceRangeMax1
snapshot.priceRangeMinUSD = previousSnapshot.priceRangeMinUSD
Expand Down
2 changes: 2 additions & 0 deletions src/harvest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ 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)
Expand All @@ -130,6 +131,7 @@ export function handleStrategyHarvest(event: HarvestEvent): void {
])
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
Expand Down
3 changes: 3 additions & 0 deletions src/mapping/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export function handleNew15Minutes(tick: ClockTick): void {
// update latest vault usd values
log.debug("handleNew15Minutes: updating vault usd values for vault {}", [vault.id.toHexString()])
vault.currentPriceOfToken0InToken1 = currentPriceInToken1
vault.currentPriceOfToken0InUSD = token0PriceInUSD
vault.priceRangeMinUSD = vault.priceRangeMin1.times(token1PriceInUSD)
vault.priceRangeMaxUSD = vault.priceRangeMax1.times(token1PriceInUSD)
vault.underlyingAmount0 = vaultBalanceUnderlying0
Expand Down Expand Up @@ -245,6 +246,7 @@ export function handleNewDay(tick: ClockTick): void {
// update vault usd values
log.debug("handleNewDay: updating vault usd values for vault {}", [vault.id.toHexString()])
vault.currentPriceOfToken0InToken1 = currentPriceInToken1
vault.currentPriceOfToken0InUSD = token0PriceInUSD
vault.priceRangeMinUSD = vault.priceRangeMin1.times(token1PriceInUSD)
vault.priceRangeMaxUSD = vault.priceRangeMax1.times(token1PriceInUSD)
vault.underlyingAmount0 = vaultBalanceUnderlying0
Expand All @@ -261,6 +263,7 @@ export function handleNewDay(tick: ClockTick): void {
])
const vaultSnapshot = getBeefyCLVaultSnapshot(vault, tick.timestamp, period)
vaultSnapshot.currentPriceOfToken0InToken1 = vault.currentPriceOfToken0InToken1
vaultSnapshot.currentPriceOfToken0InUSD = vault.currentPriceOfToken0InUSD
vaultSnapshot.priceRangeMinUSD = vault.priceRangeMinUSD
vaultSnapshot.priceRangeMaxUSD = vault.priceRangeMaxUSD
vaultSnapshot.underlyingAmount0 = vault.underlyingAmount0
Expand Down
2 changes: 2 additions & 0 deletions src/vault-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ function updateUserPosition(
// update vault entities
log.debug("updateUserPosition: updating vault entities for vault {}", [vault.id.toHexString()])
vault.currentPriceOfToken0InToken1 = currentPriceInToken1
vault.currentPriceOfToken0InUSD = currentPriceInToken1.times(token1PriceInUSD)
vault.priceRangeMin1 = rangeToken1Price.min
vault.priceRangeMax1 = rangeToken1Price.max
vault.priceRangeMinUSD = vault.priceRangeMin1.times(token1PriceInUSD)
Expand All @@ -216,6 +217,7 @@ function updateUserPosition(
])
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
Expand Down

0 comments on commit 039a6b5

Please sign in to comment.