Skip to content

Commit

Permalink
Access fewer times the logs table
Browse files Browse the repository at this point in the history
  • Loading branch information
PoloX2021 committed Jan 16, 2025
1 parent 7cb75c2 commit 0f683f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 132 deletions.
59 changes: 31 additions & 28 deletions cowamm/profitability/lp_prices/cow_amm_lp_4420687.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-- Issues with the other version (4420687)
-- Trying by accessing only once the logs table to get assets reserve
-- However for periods longer than 1 month, the query exceeds cluster capacity
-- The issue is with the join between both reserves_delta tables
-- Although this table is only 4000 rows at most (~10kb)

--Parameters
-- {{blockchain}}: The blockchain to query
-- {{start}}: The start date of the analysis
Expand All @@ -13,13 +19,14 @@ with date_range as (
)) t (day) --noqa: AL01
),

--use the materialized view of query_3959044
cow_amm_pool as (
select
created_at,
token_1_address as token0,
token_2_address as token1,
address as contract_address
from dune.cowprotocol.result_balancer_co_w_am_ms
from query_3959044
where blockchain = '{{blockchain}}'
),

Expand All @@ -40,30 +47,21 @@ lp_balance_delta as (
reserves_delta as (
select
p.contract_address,
date(coalesce(t0.evt_block_time, t1.evt_block_time)) as "day",
sum(case when t0."from" = p.contract_address then -t0.value else t0.value end) as transfer0,
sum(case when t1."from" = p.contract_address then -t1.value else t1.value end) as transfer1
t.contract_address as token,
date(t.evt_block_time) as "day",
sum(case when t."from" = p.contract_address then -t.value else t.value end) as transfer
from cow_amm_pool as p
left join erc20_{{blockchain}}.evt_transfer as t0
on
(
t0."from" = p.contract_address
or t0.to = p.contract_address
)
and t0.contract_address = p.token0
left join erc20_{{blockchain}}.evt_transfer as t1
left join erc20_{{blockchain}}.evt_transfer as t
on
(
t1."from" = p.contract_address
or t1.to = p.contract_address
t."from" = p.contract_address
or t.to = p.contract_address
)
and t1.contract_address = p.token1
where
t0.evt_block_time >= date(timestamp '{{start}}')
and date(t0.evt_block_time) <= date(timestamp '{{end}}')
and t1.evt_block_time >= date(timestamp '{{start}}')
and date(t1.evt_block_time) <= date(timestamp '{{end}}')
group by 1, 2
t.evt_block_time >= date(timestamp '{{start}}')
and date(t.evt_block_time) <= date(timestamp '{{end}}')
and t.contract_address in (p.token0, p.token1)
group by 1, 2, 3
)

select
Expand All @@ -76,18 +74,24 @@ select
price0.decimals as decimals0,
price1.decimals as decimals1,
coalesce(l.lp_transfer, 0) as lp_transfer,
coalesce(r.transfer0, 0) as transfer0,
coalesce(r.transfer1, 0) as transfer1
coalesce(r0.transfer, 0) as transfer0,
coalesce(r1.transfer, 0) as transfer1
from date_range as d
cross join cow_amm_pool as p
left join lp_balance_delta as l
on
d.day = l.day
and p.contract_address = l.contract_address
left join reserves_delta as r
left join reserves_delta as r0
on
d.day = r0.day
and p.contract_address = r0.contract_address
and p.token0 = r0.token
left join reserves_delta as r1
on
d.day = r.day
and p.contract_address = r.contract_address
d.day = r1.day
and p.contract_address = r1.contract_address
and p.token1 = r1.token
left join prices.day as price0
on
d.day = price0.timestamp
Expand All @@ -97,6 +101,5 @@ left join prices.day as price1
d.day = price1.timestamp
and p.token1 = price1.contract_address
where
(price0.blockchain = '{{blockchain}}' or price0.blockchain is null)
and (price1.blockchain = '{{blockchain}}' or price0.blockchain is null)
and d.day >= date_trunc('day', p.created_at)
coalesce(price0.blockchain, '{{blockchain}}') = '{{blockchain}}'
and coalesce(price1.blockchain, '{{blockchain}}') = '{{blockchain}}'
104 changes: 0 additions & 104 deletions cowamm/profitability/lp_prices/cow_amm_lp_v2.sql

This file was deleted.

0 comments on commit 0f683f3

Please sign in to comment.