-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uniswap V4 dex liquidity (ethereum) #7644
base: main
Are you sure you want to change the base?
Conversation
FROM | ||
{{ PoolManager_evt_ModifyLiquidity }} ml | ||
LEFT JOIN | ||
{{ PoolManager_evt_Swap }} s ON ml.evt_block_time > s.evt_block_time AND ml.id = s.id | ||
LEFT JOIN | ||
{{ PoolManager_evt_Initialize }} i ON ml.evt_block_time >= i.evt_block_time AND i.{{pair_column_name}} = ml.id | ||
{%- if is_incremental() %} | ||
WHERE | ||
{{ incremental_predicate('ml.evt_block_time') }} | ||
{%- endif %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need full PoolManager_evt_Swap
and PoolManager_evt_Initialize
table for left join?
If not we may filter out in another CTE first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ummm It's being joined on id (pool in this case).
How would you suggest to do it in another CTE? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we will do a experimental spell in parallel to test the output first.
Currently it seems like [incremental evt_ModifyLiquidity] x [full evt_Swap] x [full evt_Initialize].
I may try with if [incremental evt_ModifyLiquidity] x [incremental evt_Swap] x [full evt_Initialize] output the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can try this in a parallel spell:
WITH filtered_swaps AS (
SELECT
evt_block_time,
id,
sqrtPriceX96
FROM {{ PoolManager_evt_Swap }}
{%- if is_incremental() %}
WHERE {{ incremental_predicate('evt_block_time') }}
{%- endif %}
),
get_recent_sqrtPriceX96 AS
(
SELECT *
FROM (
SELECT
ml.*
,i.currency0 as token0
, i.currency1 as token1
, COALESCE(s.evt_block_time, i.evt_block_time) as most_recent_time
, COALESCE(s.sqrtPriceX96, i.sqrtPriceX96) AS sqrtPriceX96
, ROW_NUMBER() OVER (
PARTITION BY ml.id, ml.evt_block_time
ORDER BY
CASE WHEN s.sqrtPriceX96 IS NOT NULL THEN s.evt_block_time ELSE i.evt_block_time END DESC
) AS rn
FROM
{{ PoolManager_evt_ModifyLiquidity }} ml
LEFT JOIN
filtered_swaps s ON ml.evt_block_time > s.evt_block_time AND ml.id = s.id
LEFT JOIN
{{ PoolManager_evt_Initialize }} i ON ml.evt_block_time >= i.evt_block_time AND i.{{pair_column_name}} = ml.id
{%- if is_incremental() %}
WHERE
{{ incremental_predicate('ml.evt_block_time') }}
{%- endif %}
)tbl
WHERE rn = 1
)
If this implementation outputs the same data, we may optimize the query, but probably not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Original implementation preview with 3893 rows:
https://dune.com/queries/4719491
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified implementation with 3892 rows (match the original time duration):
https://dune.com/queries/4719556
If modification is acceptable, we may optimize this query a bit.
(I am having a branch namespace conflicts so I cannot git pull this PR to local currently, we should always try to submit PR from a newly named branch or updated branch maybe.) |
Thank you for contributing to Spellbook 🪄
Please open the PR in draft and mark as ready when you want to request a review.
Description:
[...]
quick links for more information: