From 6ee39f14eb74fee770fe9b79ceef7b18fc5e5dba Mon Sep 17 00:00:00 2001 From: PoloX2021 <115744849+PoloX2021@users.noreply.github.com> Date: Fri, 10 Jan 2025 16:59:05 +0000 Subject: [PATCH] Base CoW AMMs (#118) * 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 --- cowamm/.sqlfluff | 1 + cowamm/balancer_cow_amms_3959044.sql | 31 +++++++++++++++- .../cow_amms_total_volume_3968105.sql | 27 ++++++++++++++ .../cow_amms_tvl_evolution_3968374.sql | 21 +++++++++++ cowamm/dashboard/pools_w_links_4546258.sql | 37 +++++++++++++++++++ cowamm/dashboard/volume_evolution_4346160.sql | 15 ++++++++ 6 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 cowamm/dashboard/cow_amms_total_volume_3968105.sql create mode 100644 cowamm/dashboard/cow_amms_tvl_evolution_3968374.sql create mode 100644 cowamm/dashboard/pools_w_links_4546258.sql create mode 100644 cowamm/dashboard/volume_evolution_4346160.sql diff --git a/cowamm/.sqlfluff b/cowamm/.sqlfluff index a628ff89..b681cfb1 100644 --- a/cowamm/.sqlfluff +++ b/cowamm/.sqlfluff @@ -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' diff --git a/cowamm/balancer_cow_amms_3959044.sql b/cowamm/balancer_cow_amms_3959044.sql index b29ab837..dd254b10 100644 --- a/cowamm/balancer_cow_amms_3959044.sql +++ b/cowamm/balancer_cow_amms_3959044.sql @@ -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 @@ -89,6 +89,33 @@ 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 @@ -96,6 +123,8 @@ cowamms as ( select * from cowamms_gnosis union all select * from cowamms_arbitrum + union all + select * from cowamms_base ) select * from cowamms diff --git a/cowamm/dashboard/cow_amms_total_volume_3968105.sql b/cowamm/dashboard/cow_amms_total_volume_3968105.sql new file mode 100644 index 00000000..18ef48e7 --- /dev/null +++ b/cowamm/dashboard/cow_amms_total_volume_3968105.sql @@ -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 diff --git a/cowamm/dashboard/cow_amms_tvl_evolution_3968374.sql b/cowamm/dashboard/cow_amms_tvl_evolution_3968374.sql new file mode 100644 index 00000000..910e1bce --- /dev/null +++ b/cowamm/dashboard/cow_amms_tvl_evolution_3968374.sql @@ -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 diff --git a/cowamm/dashboard/pools_w_links_4546258.sql b/cowamm/dashboard/pools_w_links_4546258.sql new file mode 100644 index 00000000..5a1ae7e6 --- /dev/null +++ b/cowamm/dashboard/pools_w_links_4546258.sql @@ -0,0 +1,37 @@ +-- This query displays all cow amms (all chains) and references a link to the micro dashboard + +select --noqa:ST06 + concat( + '', cast(address as varchar), '' + ) 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 diff --git a/cowamm/dashboard/volume_evolution_4346160.sql b/cowamm/dashboard/volume_evolution_4346160.sql new file mode 100644 index 00000000..2e990212 --- /dev/null +++ b/cowamm/dashboard/volume_evolution_4346160.sql @@ -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