Skip to content
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

Open
wants to merge 37 commits into
base: main
Choose a base branch
from

Conversation

IrishLatte19
Copy link
Contributor

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:

@github-actions github-actions bot marked this pull request as draft February 12, 2025 15:00
@github-actions github-actions bot added WIP work in progress dbt: dex covers the DEX dbt subproject labels Feb 12, 2025
@IrishLatte19 IrishLatte19 marked this pull request as ready for review February 12, 2025 20:32
@github-actions github-actions bot added ready-for-review this PR development is complete, please review and removed WIP work in progress labels Feb 12, 2025
@Hosuke Hosuke self-assigned this Feb 13, 2025
Comment on lines 26 to 35
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 %}
Copy link
Collaborator

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.

Copy link
Contributor Author

@IrishLatte19 IrishLatte19 Feb 13, 2025

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? 🤔

Copy link
Collaborator

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.

Copy link
Collaborator

@Hosuke Hosuke Feb 13, 2025

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.

Copy link
Collaborator

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

Copy link
Collaborator

@Hosuke Hosuke Feb 13, 2025

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.

@Hosuke
Copy link
Collaborator

Hosuke commented Feb 13, 2025

(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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dbt: dex covers the DEX dbt subproject ready-for-review this PR development is complete, please review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants