diff --git a/schema.graphql b/schema.graphql index f69b24a..2b69225 100644 --- a/schema.graphql +++ b/schema.graphql @@ -68,7 +68,7 @@ type ClockTick @entity(immutable: true) { type Investor @entity { "The investor address" id: Bytes! - + "All investor beefy positions" positions: [ClmPosition!]! @derivedFrom(field: "investor") "All investor interactions" @@ -998,6 +998,10 @@ type ClmPositionSnapshot @entity { rewardPoolBalances: [BigInt!]! "Total amount of CLM shares the investor holds. Should always equal to managerBalance + rewardPoolBalance." totalBalance: BigInt! + "The amount of first underlying tokens the investor is entitled to at the time of snapshot" + underlyingBalance0: BigInt! + "The amount of second underlying tokens the investor is entitled to at the time of snapshot" + underlyingBalance1: BigInt! "Token 0 price in native at the time of snapshot. Expressed with 18 decimals." token0ToNativePrice: BigInt! diff --git a/sentio/clm_alm.md b/sentio/clm_alm.md index 3ef2a37..00fae30 100644 --- a/sentio/clm_alm.md +++ b/sentio/clm_alm.md @@ -58,7 +58,70 @@ Snapshot of the pool users. | total_fees_usd | The total amount of revenue and fees paid in this pool in the given snapshot, in USD. | number | ```SQL - +WITH position_snapshots AS ( + SELECT + snapshot.timestamp, + fromUnixTimestamp(toInt64(snapshot.roundedTimestamp)) as block_date, + snapshot.investor as user_address, + clm.underlyingToken0 as token0_id, + clm.underlyingToken1 as token1_id, + t0.symbol as token0_symbol, + t1.symbol as token1_symbol, + -- Calculate token amounts + toDecimal256(snapshot.underlyingAmount0, 18) / pow(10, t0.decimals) as token0_amount, + toDecimal256(snapshot.underlyingAmount1, 18) / pow(10, t1.decimals) as token1_amount, + -- Calculate USD values using native price conversions + (toDecimal256(snapshot.underlyingAmount0, 18) / pow(10, t0.decimals)) * + (toDecimal256(snapshot.token0ToNativePrice, 18) / pow(10, 18)) * + (toDecimal256(snapshot.nativeToUSDPrice, 18) / pow(10, 18)) as token0_amount_usd, + (toDecimal256(snapshot.underlyingAmount1, 18) / pow(10, t1.decimals)) * + (toDecimal256(snapshot.token1ToNativePrice, 18) / pow(10, 18)) * + (toDecimal256(snapshot.nativeToUSDPrice, 18) / pow(10, 18)) as token1_amount_usd + FROM + `ClmPositionSnapshot` snapshot + JOIN + CLM clm ON snapshot.clm = clm.id + JOIN + Token t0 ON clm.underlyingToken0 = t0.id + JOIN + Token t1 ON clm.underlyingToken1 = t1.id +) +SELECT + timestamp, + block_date, + 42161 as chain_id, + user_address, + token_id as token_address, + token_symbol, + token_amount, + token_amount_usd +FROM +( + -- Token0 records + SELECT + timestamp, + block_date, + user_address, + token0_id as token_id, + token0_symbol as token_symbol, + token0_amount as token_amount, + token0_amount_usd as token_amount_usd + FROM position_snapshots + + UNION ALL + + -- Token1 records + SELECT + timestamp, + block_date, + user_address, + token1_id as token_id, + token1_symbol as token_symbol, + token1_amount as token_amount, + token1_amount_usd as token_amount_usd + FROM position_snapshots +) +ORDER BY timestamp DESC, user_address, token_symbol ``` ### Pool Snapshot diff --git a/src/clm/entity/position.ts b/src/clm/entity/position.ts index 44b8f55..6fa5e19 100644 --- a/src/clm/entity/position.ts +++ b/src/clm/entity/position.ts @@ -48,6 +48,8 @@ export function getClmPositionSnapshot(position: ClmPosition, timestamp: BigInt, snapshot.managerBalance = ZERO_BI snapshot.rewardPoolBalances = [] snapshot.totalBalance = ZERO_BI + snapshot.underlyingBalance0 = ZERO_BI + snapshot.underlyingBalance1 = ZERO_BI snapshot.token0ToNativePrice = ZERO_BI snapshot.token1ToNativePrice = ZERO_BI @@ -62,6 +64,8 @@ export function getClmPositionSnapshot(position: ClmPosition, timestamp: BigInt, snapshot.managerBalance = previousSnapshot.managerBalance snapshot.rewardPoolBalances = previousSnapshot.rewardPoolBalances snapshot.totalBalance = previousSnapshot.totalBalance + snapshot.underlyingBalance0 = previousSnapshot.underlyingBalance0 + snapshot.underlyingBalance1 = previousSnapshot.underlyingBalance1 snapshot.token0ToNativePrice = previousSnapshot.token0ToNativePrice snapshot.token1ToNativePrice = previousSnapshot.token1ToNativePrice snapshot.outputToNativePrices = previousSnapshot.outputToNativePrices diff --git a/src/clm/utils/position-snapshot.ts b/src/clm/utils/position-snapshot.ts index 70c3088..5992d5c 100644 --- a/src/clm/utils/position-snapshot.ts +++ b/src/clm/utils/position-snapshot.ts @@ -6,6 +6,7 @@ import { POSITION_SNAPSHOT_ENABLED } from "../../config" import { CLM_SNAPSHOT_PERIODS } from "./snapshot" import { ZERO_BI } from "../../common/utils/decimal" import { isClmInitialized } from "../entity/clm" +import { getToken } from "../../common/entity/token" export function updateClmPositionSnapshotsIfEnabled( clm: CLM, @@ -34,6 +35,8 @@ export function updateClmPositionSnapshotsIfEnabled( snapshot.managerBalance = position.managerBalance snapshot.rewardPoolBalances = position.rewardPoolBalances snapshot.totalBalance = position.totalBalance + snapshot.underlyingBalance0 = clmData.totalUnderlyingAmount0.times(position.managerBalance).div(clmData.managerTotalSupply) + snapshot.underlyingBalance1 = clmData.totalUnderlyingAmount1.times(position.managerBalance).div(clmData.managerTotalSupply) snapshot.token0ToNativePrice = clmData.token0ToNativePrice snapshot.token1ToNativePrice = clmData.token1ToNativePrice snapshot.outputToNativePrices = clmData.outputToNativePrices