Skip to content

Commit

Permalink
Add position snapshot table
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Nov 25, 2024
1 parent 109a764 commit 020033d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 69 deletions.
1 change: 0 additions & 1 deletion bin/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ function publish_sentio {
yarn prepare:$CHAIN
yarn codegen

SUBGRAPH=$1
echo "publishing $SUBGRAPH to sentio"
npx @sentio/cli graph deploy --owner $SENTIO_OWNER --name $SUBGRAPH
}
Expand Down
125 changes: 57 additions & 68 deletions sentio/alm_general.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,81 +219,70 @@ User level token balance snapshots.
| token_amount_usd | The amount of the token in USD. | number |

```SQL
WITH token_ids AS (
SELECT DISTINCT
arrayJoin(JSONExtract(clm.rewardTokensOrder, 'Array(String)')) as token_id
FROM
CLM clm
),
daily_timestamps AS (
WITH position_snapshots AS (
SELECT
toStartOfDay(toDateTime(timestamp)) as day
snapshot.timestamp,
fromUnixTimestamp(toInt64(snapshot.roundedTimestamp)) as block_date,
hex(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
ClmPositionInteraction
GROUP BY day
),
position_days AS (
SELECT
position.id as position_id,
position.investor as user_address,
position.clm as clm_id,
days.day as timestamp
FROM
ClmPosition position
`ClmPositionSnapshot` snapshot
JOIN
Transaction tx ON tx.id = position.createdWith
CROSS JOIN
daily_timestamps days
WHERE
days.day >= toStartOfDay(toDateTime(tx.blockTimestamp))
AND days.day <= toStartOfDay(now())
),
latest_position_state AS (
SELECT
interaction.investorPosition as position_id,
interaction.timestamp as timestamp,
interaction.rewardPoolBalances as balances
FROM ClmPositionInteraction interaction
WHERE type__ = 'CLM_REWARD_POOL_STAKE'
OR type__ = 'CLM_REWARD_POOL_UNSTAKE'
OR type__ = 'CLM_REWARD_POOL_CLAIM'
CLM clm ON snapshot.clm = clm.id
JOIN
Token t0 ON clm.underlyingToken0 = t0.id
JOIN
Token t1 ON clm.underlyingToken1 = t1.id
)

SELECT
pd.timestamp,
timestamp,
block_date,
42161 as chain_id,
hex(pd.user_address) as user_address,
hex(pd.clm_id) as pool_address,
JSONExtract(clm.rewardTokensOrder, 'Array(String)') as token_address,
JSONExtract(lps.balances, 'Array(String)') as balance,
arrayMap(
(balance, price) -> (
toDecimal256(balance, 18) / pow(10, t.decimals) *
(toDecimal256(price, 18) / pow(10, 18)) *
(toDecimal256(snapshot.nativeToUSDPrice, 18) / pow(10, 18))
),
JSONExtract(lps.balances, 'Array(String)'),
JSONExtract(snapshot.rewardToNativePrices, 'Array(String)')
) as balance_usd
user_address,
token_id as token_address,
token_symbol,
token_amount,
token_amount_usd
FROM
position_days pd
JOIN
CLM clm ON clm.id = pd.clm_id
LEFT JOIN
ClmSnapshot snapshot ON (
snapshot.clm = pd.clm_id
AND toStartOfDay(toDateTime(snapshot.timestamp)) = pd.timestamp
AND snapshot.period = 86400 -- daily snapshots
)
LEFT JOIN
latest_position_state lps ON (
lps.position_id = pd.position_id
AND toStartOfDay(toDateTime(lps.timestamp)) <= pd.timestamp
)
JOIN
Token t ON t.id = arrayJoin(JSONExtract(clm.rewardTokensOrder, 'Array(String)'))
ORDER BY
pd.timestamp DESC, pd.user_address, pd.clm_id
(
-- 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
```

### General Transactions
Expand Down

0 comments on commit 020033d

Please sign in to comment.