Skip to content

Commit

Permalink
Base CoW AMMs (#118)
Browse files Browse the repository at this point in the history
* AMMs on Base

* Query which references/displays pools

* Query to track volume evolution, with base

* TVL evolution with base

* Total volume, adapted for base

* Comments

* Answering remarks

* Changing cow_protocol.trades

* Adding aggregate_by value
  • Loading branch information
PoloX2021 authored Jan 10, 2025
1 parent f7fb504 commit 6ee39f1
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions cowamm/.sqlfluff
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ results=final_results_per_solver,cow_surplus_per_batch
cow_budget=30000
number_of_pools=1000
competitor_end_time='2024-08-27 00:00:00'
aggregate_by='week'
31 changes: 30 additions & 1 deletion cowamm/balancer_cow_amms_3959044.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- This is part of a base query for monitoring Balancer CoW AMMs
-- It indexes all Balancer CoW AMMs on ethereum and gnosis and arbitrum
-- It indexes all Balancer CoW AMMs on ethereum and gnosis and arbitrum and base
--
-- the final table has columns
-- - created_at: the creation timestamp
Expand Down Expand Up @@ -89,13 +89,42 @@ cowamms_arbitrum as (
group by 1, 2
),

-- on base
cowamm_creations_base as (
select varbinary_substring(topic1, 1 + 12, 20) as address
from base.logs
where (
contract_address in (0x03362f847B4fAbC12e1Ce98b6b59F94401E4588e)
and topic0 = 0x0d03834d0d86c7f57e877af40e26f176dc31bd637535d4ba153d1ac9de88a7ea
)
and block_time >= cast('2024-12-01 00:00:00' as timestamp)
),

cowamms_base as (
select
'base' as blockchain,
contract_address as address,
min(block_time) as created_at,
min(varbinary_substring(data, 5 + 2 * 32 + 12, 20)) as token_1_address,
max(varbinary_substring(data, 5 + 2 * 32 + 12, 20)) as token_2_address
from
base.logs
where
contract_address in (select address from cowamm_creations_base)
and topic0 = 0xe4e1e53800000000000000000000000000000000000000000000000000000000
and block_time >= cast('2024-12-01 00:00:00' as timestamp)
group by 1, 2
),

-- combine data for different chains
cowamms as (
select * from cowamms_ethereum
union all
select * from cowamms_gnosis
union all
select * from cowamms_arbitrum
union all
select * from cowamms_base
)

select * from cowamms
27 changes: 27 additions & 0 deletions cowamm/dashboard/cow_amms_total_volume_3968105.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--Query computes the trading volume over all Cow AMMs (matching users and rebalancing) and 7 day growth thereof.
with cow_trades as (
select
block_date,
sum(amount_usd) as volume
from cow_protocol.trades as t
inner join query_3959044 as pool
on t.taker = pool.address
group by 1
),

cumulative_volume as (
select
block_date as day, --noqa: RF04
sum(volume) over (order by block_date) as tvl
from cow_trades
)

select
prev.tvl as prev,
curr.tvl as curr,
100 * (curr.tvl - prev.tvl) / prev.tvl as growth
from cumulative_volume as curr
inner join cumulative_volume as prev
on curr.day = prev.day + interval '7' day
-- we don't have data for today
and curr.day = date(now()) - interval '1' day
21 changes: 21 additions & 0 deletions cowamm/dashboard/cow_amms_tvl_evolution_3968374.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Query computes the TVL over all CoW AMMs

-- Parameters
-- {{aggregate_by}}: the frequence of the data, e.g. 'day', 'week', 'month'

with prep as (
select * from "query_4096107(blockchain='ethereum')"
union all
select * from "query_4096107(blockchain='gnosis')"
union all
select * from "query_4096107(blockchain='arbitrum')"
union all
select * from "query_4096107(blockchain='base')"
)

select
date_trunc('{{aggregate_by}}', day) as period,
sum(tvl) as tvl
from prep
group by 1
order by 1 desc
37 changes: 37 additions & 0 deletions cowamm/dashboard/pools_w_links_4546258.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- This query displays all cow amms (all chains) and references a link to the micro dashboard

select --noqa:ST06
concat(
'<a href="https://dune.com/cowprotocol/cow-amm-micro-v2?token_a=', cast(token_1_address as varchar), '&token_b=', cast(token_2_address as varchar), '&blockchain=', t.blockchain, '&ref_token_a=', cast(token_1_address as varchar), '&ref_token_b=', cast(token_2_address as varchar), '&ref_blockchain=', t.blockchain,
'" target="_blank">', cast(address as varchar), '</a>'
) as cow_amm_address,
t.blockchain,
total_tvl,
t1.symbol as token_1_symbol,
t2.symbol as token_2_symbol,
365 * surplus_1d / total_tvl as "1d APY",
365 * surplus_7d / total_tvl / 7 as "7d APY",
365 * surplus_30d / total_tvl / 30 as "30d APY",
token_1_address,
token_2_address
from (
select
-- created_at,
address,
blockchain,
token_1_address,
token_2_address,
rank() over (partition by token_1_address, token_2_address order by created_at desc) as ranking
from query_3959044
) as t
inner join tokens.erc20 as t1
on token_1_address = t1.contract_address
left join tokens.erc20 as t2
on token_2_address = t2.contract_address
left join query_4062965 as tvl
on tvl.pool = address
where
t.ranking = 1
and total_tvl > 0
group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
order by total_tvl desc --noqa: AM06
15 changes: 15 additions & 0 deletions cowamm/dashboard/volume_evolution_4346160.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--Query computes the trading volume over all Cow AMMs (matching users and rebalancing)

-- Parameters:
-- {{aggregate_by}}: the frequence of the data, e.g. 'day', 'week', 'month'
with cow_trades as (
select
date_trunc('{{aggregate_by}}', block_date) as period,
sum(amount_usd) as volume
from cow_protocol.trades as t
inner join query_3959044 as pool
on t.taker = pool.address
group by 1
)

select * from cow_trades

0 comments on commit 6ee39f1

Please sign in to comment.