Skip to content

Commit

Permalink
Fix underlyingToNative price for classic
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Jul 18, 2024
1 parent c4e1446 commit 0499adf
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 16 deletions.
15 changes: 15 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ type ClassicPositionInteraction @entity(immutable: true) {
"Total amount of vault shares the investor holds. Should always equal vaultBalance + boostBalance. This is mostly used for filtering."
totalBalance: BigInt!

"The vault total supply of shares at the time of the interaction"
vaultSharesTotalSupply: BigInt!
"The amount of underlying the vault contains at the time of the interaction"
vaultUnderlyingAmount: BigInt!

"Amount of vault shares change in the interaction"
vaultBalanceDelta: BigInt!
"Amount of vault shares token change on the boost contract in the interaction"
Expand Down Expand Up @@ -503,6 +508,11 @@ type CLM @entity {
"Price range end this CLM is currently configured to operate in, in token 1"
priceRangeMax1: BigInt!

"Total amount of token0 in the CLM. Can be slightly higher than main + alt amounts due to vested tokens."
totalUnderlyingAmount0: BigInt!
"Total amount of token1 in the CLM. Can be slightly higher than main + alt amounts due to vested tokens."
totalUnderlyingAmount1: BigInt!

"Amount of underlying tokens in the CLM assigned to the main position in the first token."
underlyingMainAmount0: BigInt!
"Amount of underlying tokens in the CLM assigned to the main position in the second token."
Expand Down Expand Up @@ -581,6 +591,11 @@ type ClmSnapshot @entity {
"Price range end this CLM is currently configured to operate in, in token 1"
priceRangeMax1: BigInt!

"Total amount of token0 in the CLM. Can be slightly higher than main + alt amounts due to vested tokens."
totalUnderlyingAmount0: BigInt!
"Total amount of token1 in the CLM. Can be slightly higher than main + alt amounts due to vested tokens."
totalUnderlyingAmount1: BigInt!

"Amount of underlying tokens in the CLM assigned to the main position in the first token."
underlyingMainAmount0: BigInt!
"Amount of underlying tokens in the CLM assigned to the main position in the second token."
Expand Down
2 changes: 2 additions & 0 deletions src/classic/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ function updateUserPosition(
interaction.boostBalance = position.boostBalance
interaction.rewardPoolBalances = position.rewardPoolBalances
interaction.totalBalance = position.totalBalance
interaction.vaultSharesTotalSupply = classicData.vaultSharesTotalSupply
interaction.vaultUnderlyingAmount = classicData.underlyingAmount
interaction.vaultBalanceDelta = vaultBalanceDelta
interaction.boostBalanceDelta = boostBalanceDelta
interaction.boostRewardBalancesDelta = boostRewardBalancesDelta
Expand Down
15 changes: 13 additions & 2 deletions src/classic/utils/classic-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
PRICE_STORE_DECIMALS_USD,
PYTH_NATIVE_PRICE_ID,
PYTH_PRICE_FEED_ADDRESS,
WNATIVE_DECIMALS,
WNATIVE_TOKEN_ADDRESS,
} from "../../config"
import { Multicall3Params, MulticallResult, multicall } from "../../common/utils/multicall"
Expand Down Expand Up @@ -167,8 +168,18 @@ export function fetchClassicData(classic: Classic): ClassicData {
}

if (clm) {
const totalNativeAmount0 = clm.underlyingMainAmount0.plus(clm.underlyingAltAmount0).times(clm.token0ToNativePrice)
const totalNativeAmount1 = clm.underlyingMainAmount1.plus(clm.underlyingAltAmount1).times(clm.token1ToNativePrice)
const token0 = getToken(clm.underlyingToken0)
const token1 = getToken(clm.underlyingToken1)
const totalNativeAmount0 = changeValueEncoding(
clm.totalUnderlyingAmount0.times(clm.token0ToNativePrice),
token0.decimals.plus(WNATIVE_DECIMALS),
WNATIVE_DECIMALS,
)
const totalNativeAmount1 = changeValueEncoding(
clm.totalUnderlyingAmount1.times(clm.token1ToNativePrice),
token1.decimals.plus(WNATIVE_DECIMALS),
WNATIVE_DECIMALS,
)
const totalNativeAmountInClm = totalNativeAmount0.plus(totalNativeAmount1)
// assumption: 1 rewardPool token === 1 manager token
underlyingToNativePrice = clm.managerTotalSupply.equals(ZERO_BI)
Expand Down
4 changes: 2 additions & 2 deletions src/clm/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ function handleClmStrategyHarvest(
harvest.createdWith = tx.id
harvest.logIndex = event.logIndex
harvest.timestamp = event.block.timestamp
harvest.underlyingAmount0 = clmData.token0Balance
harvest.underlyingAmount1 = clmData.token1Balance
harvest.underlyingAmount0 = clmData.totalUnderlyingAmount0
harvest.underlyingAmount1 = clmData.totalUnderlyingAmount1
harvest.compoundedAmount0 = compoundedAmount0
harvest.compoundedAmount1 = compoundedAmount1
harvest.collectedOutputAmounts = collectedOutputAmounts
Expand Down
8 changes: 8 additions & 0 deletions src/clm/entity/clm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export function getCLM(managerAddress: Bytes): CLM {
clm.priceRangeMin1 = ZERO_BI
clm.priceRangeMax1 = ZERO_BI

clm.totalUnderlyingAmount0 = ZERO_BI
clm.totalUnderlyingAmount1 = ZERO_BI

clm.underlyingMainAmount0 = ZERO_BI
clm.underlyingMainAmount1 = ZERO_BI
clm.underlyingAltAmount0 = ZERO_BI
Expand Down Expand Up @@ -132,6 +135,9 @@ export function getClmSnapshot(clm: CLM, timestamp: BigInt, period: BigInt): Clm
snapshot.priceRangeMin1 = ZERO_BI
snapshot.priceRangeMax1 = ZERO_BI

snapshot.totalUnderlyingAmount0 = ZERO_BI
snapshot.totalUnderlyingAmount1 = ZERO_BI

snapshot.underlyingMainAmount0 = ZERO_BI
snapshot.underlyingMainAmount1 = ZERO_BI
snapshot.underlyingAltAmount0 = ZERO_BI
Expand All @@ -151,6 +157,8 @@ export function getClmSnapshot(clm: CLM, timestamp: BigInt, period: BigInt): Clm
snapshot.priceOfToken0InToken1 = previousSnapshot.priceOfToken0InToken1
snapshot.priceRangeMin1 = previousSnapshot.priceRangeMin1
snapshot.priceRangeMax1 = previousSnapshot.priceRangeMax1
snapshot.totalUnderlyingAmount0 = previousSnapshot.totalUnderlyingAmount0
snapshot.totalUnderlyingAmount1 = previousSnapshot.totalUnderlyingAmount1
snapshot.underlyingMainAmount0 = previousSnapshot.underlyingMainAmount0
snapshot.underlyingMainAmount1 = previousSnapshot.underlyingMainAmount1
snapshot.underlyingAltAmount0 = previousSnapshot.underlyingAltAmount0
Expand Down
8 changes: 4 additions & 4 deletions src/clm/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ function updateUserPosition(
// set the underlying balances at the time of the transaction
if (!clmData.managerTotalSupply.equals(ZERO_BI)) {
interaction.underlyingBalance0 = interaction.underlyingBalance0.plus(
clmData.token0Balance.times(position.totalBalance).div(clmData.managerTotalSupply),
clmData.totalUnderlyingAmount0.times(position.totalBalance).div(clmData.managerTotalSupply),
)
interaction.underlyingBalance1 = interaction.underlyingBalance1.plus(
clmData.token1Balance.times(position.totalBalance).div(clmData.managerTotalSupply),
clmData.totalUnderlyingAmount1.times(position.totalBalance).div(clmData.managerTotalSupply),
)

// assumption: 1 rewardPool token === 1 manager token
Expand All @@ -256,10 +256,10 @@ function updateUserPosition(
}
const positionEquivalentInManagerBalance = managerBalanceDelta.plus(totalRewardPoolBalanceDelta)
interaction.underlyingBalance0Delta = interaction.underlyingBalance0Delta.plus(
clmData.token0Balance.times(positionEquivalentInManagerBalance).div(clmData.managerTotalSupply),
clmData.totalUnderlyingAmount0.times(positionEquivalentInManagerBalance).div(clmData.managerTotalSupply),
)
interaction.underlyingBalance1Delta = interaction.underlyingBalance1Delta.plus(
clmData.token1Balance.times(positionEquivalentInManagerBalance).div(clmData.managerTotalSupply),
clmData.totalUnderlyingAmount1.times(positionEquivalentInManagerBalance).div(clmData.managerTotalSupply),
)
}
interaction.token0ToNativePrice = clmData.token0ToNativePrice
Expand Down
20 changes: 12 additions & 8 deletions src/clm/utils/clm-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ export function fetchCLMData(clm: CLM): CLMData {
log.error("Failed to fetch totalSupply for CLM {}", [clm.id.toHexString()])
}

let token0Balance = ZERO_BI
let token1Balance = ZERO_BI
let totalUnderlyingAmount0 = ZERO_BI
let totalUnderlyingAmount1 = ZERO_BI
if (!balanceRes.reverted) {
const balances = balanceRes.value.toTuple()
token0Balance = balances[0].toBigInt()
token1Balance = balances[1].toBigInt()
totalUnderlyingAmount0 = balances[0].toBigInt()
totalUnderlyingAmount1 = balances[1].toBigInt()
} else {
log.error("Failed to fetch balance for CLM {}", [clm.id.toHexString()])
}
Expand Down Expand Up @@ -289,8 +289,8 @@ export function fetchCLMData(clm: CLM): CLMData {
return new CLMData(
managerTotalSupply,
rewardPoolsTotalSupply,
token0Balance,
token1Balance,
totalUnderlyingAmount0,
totalUnderlyingAmount1,

token0PositionMainBalance,
token1PositionMainBalance,
Expand All @@ -313,8 +313,8 @@ class CLMData {
constructor(
public managerTotalSupply: BigInt,
public rewardPoolsTotalSupply: Array<BigInt>,
public token0Balance: BigInt,
public token1Balance: BigInt,
public totalUnderlyingAmount0: BigInt,
public totalUnderlyingAmount1: BigInt,

public token0PositionMainBalance: BigInt,
public token1PositionMainBalance: BigInt,
Expand Down Expand Up @@ -345,6 +345,8 @@ export function updateCLMDataAndSnapshots(clm: CLM, clmData: CLMData, nowTimesta
clm.priceOfToken0InToken1 = clmData.priceOfToken0InToken1
clm.priceRangeMin1 = clmData.priceRangeMin1
clm.priceRangeMax1 = clmData.priceRangeMax1
clm.totalUnderlyingAmount0 = clmData.totalUnderlyingAmount0
clm.totalUnderlyingAmount1 = clmData.totalUnderlyingAmount1
clm.underlyingMainAmount0 = clmData.token0PositionMainBalance
clm.underlyingMainAmount1 = clmData.token1PositionMainBalance
clm.underlyingAltAmount0 = clmData.token0PositionAltBalance
Expand All @@ -370,6 +372,8 @@ export function updateCLMDataAndSnapshots(clm: CLM, clmData: CLMData, nowTimesta
snapshot.priceOfToken0InToken1 = clm.priceOfToken0InToken1
snapshot.priceRangeMin1 = clm.priceRangeMin1
snapshot.priceRangeMax1 = clm.priceRangeMax1
snapshot.totalUnderlyingAmount0 = clm.totalUnderlyingAmount0
snapshot.totalUnderlyingAmount1 = clm.totalUnderlyingAmount1
snapshot.underlyingMainAmount0 = clm.underlyingMainAmount0
snapshot.underlyingMainAmount1 = clm.underlyingMainAmount1
snapshot.underlyingAltAmount0 = clm.underlyingAltAmount0
Expand Down

0 comments on commit 0499adf

Please sign in to comment.