Skip to content

Commit

Permalink
New default value for day, few comments and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
PoloX2021 committed Nov 26, 2024
1 parent 7f4dbb9 commit 5b26df3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cowamm/.sqlfluff
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ end_time='2024-08-27 00:00:00'
results=final_results_per_solver,cow_surplus_per_batch
cow_budget=30000
number_of_pools=500
end_time = 2024-01-01
competitor_end_time = 2100-01-01
8 changes: 5 additions & 3 deletions cowamm/kpi/competitors/curve/curve_kpis_4232873.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
-- APR is measured as the fees earned per $ invested, over the last 24 hours, projected over 1 year
-- Parameters:
-- {{blockchain}}: The blockchain to query
-- {{end_time}}: The end time of the time window (end_time - 1 day; end_time), defaults to now()
-- {{competitor_end_time}}: The end time of the time window (end_time - 1 day; end_time), defaults to now()
select
r1.contract_address,
r1.fee,
r1.tvl,
sum(amount_usd) as volume,
365 * sum(amount_usd * r.fee / r.tvl) as apr
-- The first call to 4232976 gets the tvl after each tx to compute volume/tvl
from "query_4232976(blockchain='{{blockchain}}')" as r
-- The second call to 4232976 gets only the last tvl and fee to display for the pool
inner join "query_4232976(blockchain='{{blockchain}}')" as r1
on
r.contract_address = r1.contract_address
Expand All @@ -18,8 +20,8 @@ left join curve.trades as t
r.contract_address = t.project_contract_address
and r.tx_hash = t.tx_hash
where
t.block_time >= date_add('day', -1, (case when '{{end_time}}' = '2024-01-01' then now() else timestamp '{{end_time}}' end))
and r1.latest = 1
t.block_time >= date_add('day', -1, (case when '{{competitor_end_time}}' = '2100-01-01' then now() else timestamp '{{competitor_end_time}}' end))
and r1.latest_per_pool = 1
-- This test avoids any possible issue with reconstructing the reserves of the pool
and r1.tvl > 0
group by 1, 2, 3
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ reserves as (
fee,
sum(transfer0) over (partition by contract_address order by block_time, evt_index) as reserve0,
sum(transfer1) over (partition by contract_address order by block_time, evt_index) as reserve1,
row_number() over (partition by tx_hash, contract_address order by evt_index desc) as row_num,
row_number() over (partition by contract_address order by block_time desc) as latest
row_number() over (partition by tx_hash, contract_address order by evt_index desc) as latest_per_tx,
row_number() over (partition by contract_address order by block_time desc) as latest_per_pool
from transfers
),

Expand All @@ -74,7 +74,7 @@ recent_tvl as (
reserve0,
reserve1,
fee,
latest,
latest_per_pool,
(reserve0 * p0.price / pow(10, p0.decimals)) + (reserve1 * p1.price / pow(10, p1.decimals)) as tvl
from reserves as r
inner join prices.usd as p0
Expand All @@ -85,15 +85,15 @@ recent_tvl as (
on
date_trunc('minute', block_time) = p1.minute
and token1 = p1.contract_address
where row_num = 1
where latest_per_tx = 1
)


select * from recent_tvl
where contract_address in (
select contract_address
from recent_tvl
where latest = 1
where latest_per_pool = 1
order by tvl desc
limit {{number_of_pools}}
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
-- Computes volume, tvl and APR for Uni Swap style pools (Uni, Pancake, Sushi)
-- APR is measured as the fees earned per $ invested, over the last 24 hours, projected over 1 year
-- The type of pool (Uni/Pancake/Sushi) is not specified
-- Parameters:
-- {{blockchain}}: The blockchain to query
-- {{number_of_pools}}: The number of largest pools to return
-- {{end_time}}: The end time of the time window (end_time - 1 day; end_time), defaults to now()
-- {{competitor_end_time}}: The end time of the time window (end_time - 1 day; end_time), defaults to now()

-- select the pool with the largest latest k
with pool as (
Expand All @@ -31,7 +30,7 @@ syncs as (
inner join pool
on logs.contract_address = pool.contract_address
where
block_time >= date_add('day', -1, (case when '{{end_time}}' = '2024-01-01' then now() else timestamp '{{end_time}}' end))
block_time >= date_add('day', -1, (case when '{{competitor_end_time}}' = '2100-01-01' then now() else timestamp '{{competitor_end_time}}' end))
and topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync
),

Expand Down Expand Up @@ -89,9 +88,9 @@ select
end as fee,
coalesce(sum((volume_in + volume_out) / 2), 0) as volume,
-- the average pool is conceptually unnecessary because the table pool has only one tvl per pool
-- but it is necessary for the group by statement
-- but it is necessary for the group by statement
avg(pool.tvl) as tvl,
case
case
when project = 'pancakeswap' then coalesce(365 * sum((volume_in + volume_out) / 2 / t.tvl) * 0.0025, 0)
else coalesce(365 * sum((volume_in + volume_out) / 2 / t.tvl) * 0.003, 0)
end as apr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ inner join prices.usd as p1
token1 = p1.contract_address
and p1.minute = evt_block_time
where latest = 1
order by 7 desc
order by tvl desc
limit {{number_of_pools}}

0 comments on commit 5b26df3

Please sign in to comment.