KeyrockUniV2 -- usdc/weth TVL Overtime -- working version
    Updated 2024-02-07
    -- forked from UniV2 -- token1/token0 TVL Overtime (V3) @ https://flipsidecrypto.xyz/edit/queries/7f9d30a7-238c-4642-9398-866b2611e2d8

    /* Extract and categorize transfers involving token0 (token0)
    ('0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc') -- pool
    Token0 ('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'), -- token0
    Token1 ('0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48') -- token1
    )
    */
    WITH transfers AS (
    SELECT
    BLOCK_TIMESTAMP,
    CASE
    WHEN tr.contract_address = '{{token0_contract}}' -- token 0
    AND tr.to_address = '{{pool_contract}}' THEN TRY_CAST(raw_amount_precise AS DOUBLE) -- pool addy
    WHEN tr.contract_address = '{{token0_contract}}' -- token 0
    AND tr.from_address = '{{pool_contract}}' THEN - TRY_CAST(raw_amount_precise AS DOUBLE) -- pooly addy
    END AS token0
    FROM
    ethereum.core.fact_token_transfers AS tr
    WHERE
    (
    tr.to_address = '{{pool_contract}}' -- pool addy
    )
    OR (
    tr.from_address = '{{pool_contract}}' -- pool addy
    )
    AND tr.contract_address IN (
    '{{token0_contract}}' -- token 1
    )
    ),
    -- Group and sum token0 transfers by date
    grouped_transfers AS (
    SELECT
    DATE_TRUNC('day', BLOCK_TIMESTAMP) AS evt_date,
    Last run: over 1 year ago
    DAY
    TOKEN0_BAL
    TOKEN1_BAL
    PRICE_0
    PRICE_1
    1
    2024-02-07 00:00:00.0005032452.003210545102321.020733369.255639329e-72369.907623956
    2
    2024-02-06 00:00:00.0005007291.850827734923445.044478899.017626667e-72335.06375
    3
    2024-02-05 00:00:00.0005015418.572825334901652.520025339.136146346e-72299.170416667
    4
    2024-02-04 00:00:00.0004967222.106389824896507.307750219.051110417e-72297.095
    5
    2024-02-03 00:00:00.0005032379.048353455001681.988186619.32342875e-72308.594583333
    6
    2024-02-02 00:00:00.0005008196.399429934984268.068412929.274204167e-72302.232083333
    7
    2024-02-01 00:00:00.0004965673.72946194896240.007801439.153660833e-72272.497916667
    8
    2024-01-31 00:00:00.0005161325.580896144996385.803673779.539739583e-72313.784166667
    9
    2024-01-30 00:00:00.0005473967.791984755209203.674594660.000001048752328.230416667
    10
    2024-01-29 00:00:00.0005352306.275867245253480.466039250.0000010604166672272.340416667
    11
    2024-01-28 00:00:00.0005400309.191270155331699.47460840.000001086252272.091666667
    12
    2024-01-27 00:00:00.0005362054.330580995375923.869946860.0000010904166672266.472916667
    13
    2024-01-26 00:00:00.0005245125.049221265272515.713419790.0000010595833332237.15875
    14
    2024-01-25 00:00:00.0005134407.53859475141912.791312490.0000010241666672210.020416667
    15
    2024-01-24 00:00:00.0005196140.722533345186118.371733490.00000103752227.209583333
    16
    2024-01-23 00:00:00.0005173924.203284915249409.37951850.0000010318164582257.564583333
    17
    2024-01-22 00:00:00.0005534331.377740715503309.218471270.0000010929166672391.14125
    18
    2024-01-21 00:00:00.0005834346.182631745707671.07339650.0000011559395192472.16125
    19
    2024-01-20 00:00:00.0005596985.790342115801155.16424120.0000011272055412472.411838445
    20
    2024-01-19 00:00:00.0005689765.658674315697139.180989570.0000011260900542471.49541956
    ...
    300
    27KB
    41s