From a361cf77580adc63b3fb08a33abda7d0b86e02b8 Mon Sep 17 00:00:00 2001 From: Felix Leupold Date: Fri, 20 Sep 2024 17:20:12 +0200 Subject: [PATCH 1/5] Support CoW AMM stats on all chains --- cowamm/.sqlfluff | 2 +- cowamm/balancer_cow_amms_3959044.sql | 29 +++++++++++++++++++ .../profitability/10k_growth/cow_4047078.sql | 9 +++--- .../10k_growth/daily_rebalancing_4055484.sql | 9 +++--- .../invariant_growth/cow_amm_4059213.sql | 5 ++-- cowamm/tvl_by_tx_4059700.sql | 3 +- 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/cowamm/.sqlfluff b/cowamm/.sqlfluff index b4c6729..85ca914 100644 --- a/cowamm/.sqlfluff +++ b/cowamm/.sqlfluff @@ -7,4 +7,4 @@ blockchain=ethereum start_time='2024-08-20 00:00:00' end_time='2024-08-27 00:00:00' results=final_results_per_solver,cow_surplus_per_batch -budget=30000 \ No newline at end of file +budget=30000 diff --git a/cowamm/balancer_cow_amms_3959044.sql b/cowamm/balancer_cow_amms_3959044.sql index 6e37447..38b3e1c 100644 --- a/cowamm/balancer_cow_amms_3959044.sql +++ b/cowamm/balancer_cow_amms_3959044.sql @@ -62,11 +62,40 @@ cowamms_gnosis as ( group by 1, 2 ), +-- on arbitrum +cowamm_creations_arbitrum as ( + select varbinary_substring(topic1, 1 + 12, 20) as address + from arbitrum.logs + where ( + contract_address in (0xe0e2ba143ee5268da87d529949a2521115987302) + and topic0 = 0x0d03834d0d86c7f57e877af40e26f176dc31bd637535d4ba153d1ac9de88a7ea + ) + and block_time >= cast('2024-09-10 00:00:00' as timestamp) +), + +cowamms_arbitrum as ( + select + 'arbitrum' 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 + arbitrum.logs + where + contract_address in (select address from cowamm_creations_arbitrum) + and topic0 = 0xe4e1e53800000000000000000000000000000000000000000000000000000000 + and block_time >= cast('2024-09-10 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 ) select * from cowamms diff --git a/cowamm/profitability/10k_growth/cow_4047078.sql b/cowamm/profitability/10k_growth/cow_4047078.sql index 978abd5..3fd3e1a 100644 --- a/cowamm/profitability/10k_growth/cow_4047078.sql +++ b/cowamm/profitability/10k_growth/cow_4047078.sql @@ -3,6 +3,7 @@ -- {{token_a}} - either token of the desired uni pool -- {{token_b}} - other token of the desired uni pool -- {{start}} - date as of which the analysis should run +-- {{blockchain} - chain for which the query is -- Given that we might not have records every day in the source data (e.g. not every day the lp supply may change), -- but still want to visualize development on a per day basis, we create an auxiliary table with one record per @@ -32,7 +33,7 @@ lp_balance_delta as ( select date(evt_block_time) as "day", sum(case when "from" = 0x0000000000000000000000000000000000000000 then value else -value end) as lp_supply - from erc20_ethereum.evt_transfer + from erc20_{{blockchain}}.evt_transfer where contract_address in (select address from cow_amm_pool) and ("from" = 0x0000000000000000000000000000000000000000 or "to" = 0x0000000000000000000000000000000000000000) @@ -79,7 +80,7 @@ tvl_by_tx as ( select *, rank() over (partition by date(block_time) order by block_time desc) as latest - from "query_4059700(token_a='{{token_a}}', token_b='{{token_b}}')" + from "query_4059700(token_a='{{token_a}}', token_b='{{token_b}}', blockchain='{{blockchain}}')" where pool = (select address from cow_amm_pool) -- performance optimisation: this assumes one week prior to start there was at least one tvl change event and block_time >= timestamp '{{start}}' - interval '7' day @@ -110,12 +111,12 @@ tvl as ( on tvl_complete.day = p1.day and p1.contract_address = token1 - and p1.blockchain = 'ethereum' + and p1.blockchain = '{{blockchain}}' inner join prices.usd_daily as p2 on tvl_complete.day = p2.day and p2.contract_address = token2 - and p2.blockchain = 'ethereum' + and p2.blockchain = '{{blockchain}}' where latest = 1 ), diff --git a/cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql b/cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql index 93efb43..2883701 100644 --- a/cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql +++ b/cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql @@ -3,6 +3,7 @@ -- {{token_a}} - either token of the desired uni pool -- {{token_b}} - other token of the desired uni pool -- {{start}} - date as of which the analysis should run +-- {{blockchain} - chain for which the query is running -- Note: not using a simpler recursive approach due to Dune's recursion depth limitation. -- Current value of initial investment can be computed as the product of cumulative price changes per day, since @@ -31,26 +32,26 @@ daily_price_change as ( on p1.day = ds.day and p1.contract_address = {{token_a}} - and p1.blockchain = 'ethereum' + and p1.blockchain = '{{blockchain}}' left join prices.usd_daily as previous_p1 on previous_p1.day = ds.day - interval '1' day -- avoid computing price change on first day and previous_p1.day >= date(timestamp '{{start}}') and previous_p1.contract_address = {{token_a}} - and previous_p1.blockchain = 'ethereum' + and previous_p1.blockchain = '{{blockchain}}' inner join prices.usd_daily as p2 on p2.day = ds.day and p2.contract_address = {{token_b}} - and p2.blockchain = 'ethereum' + and p2.blockchain = '{{blockchain}}' left join prices.usd_daily as previous_p2 on previous_p2.day = ds.day - interval '1' day -- avoid computing price change on first day and previous_p2.day >= date(timestamp '{{start}}') and previous_p2.contract_address = {{token_b}} - and previous_p2.blockchain = 'ethereum' + and previous_p2.blockchain = '{{blockchain}}' ) -- For each day multiply initial investment with cumulative product of average price change of the two assets diff --git a/cowamm/profitability/invariant_growth/cow_amm_4059213.sql b/cowamm/profitability/invariant_growth/cow_amm_4059213.sql index 9bfbb38..1f7f52a 100644 --- a/cowamm/profitability/invariant_growth/cow_amm_4059213.sql +++ b/cowamm/profitability/invariant_growth/cow_amm_4059213.sql @@ -3,6 +3,7 @@ -- {{token_a}} - either token of the desired uni pool -- {{token_b}} - other token of the desired uni pool -- {{start}} - date as of which the analysis should run +-- {{blockchain} - chain for which the query is running -- Finds the CoW AMM pool address given tokens specified in query parameters (regardless of order) with cow_amm_pool as ( @@ -22,8 +23,8 @@ select SUM(surplus_usd) as absolute_invariant_growth, AVG(tvl) as tvl, SUM(surplus_usd / tvl) as pct_invariant_growth -from cow_protocol_ethereum.trades as t -inner join "query_4059700(token_a='{{token_a}}', token_b='{{token_b}}')" as tvl +from cow_protocol_{{blockchain}}.trades as t +inner join "query_4059700(token_a='{{token_a}}', token_b='{{token_b}}', blockchain='{{blockchain}}')" as tvl on t.tx_hash = tvl.tx_hash and tvl.pool = trader diff --git a/cowamm/tvl_by_tx_4059700.sql b/cowamm/tvl_by_tx_4059700.sql index 99722e2..a753dbd 100644 --- a/cowamm/tvl_by_tx_4059700.sql +++ b/cowamm/tvl_by_tx_4059700.sql @@ -2,6 +2,7 @@ -- Parameters -- {{token_a}} - either token of the pool -- {{token_b}} - other token of the pool +-- {{blockchain} - chain for which the query is running with cow_amm_pool as ( select @@ -22,7 +23,7 @@ reserves_delta as ( MAX(evt_block_number) as evt_block_number, MAX(evt_index) as evt_index, SUM(case when "from" = p.address then -value else value end) as amount - from erc20_ethereum.evt_transfer as t + from erc20_{{blockchain}}.evt_transfer as t inner join cow_amm_pool as p on t."from" = p.address From b7980e661f533528ab83ffa256a8dc763e733ddf Mon Sep 17 00:00:00 2001 From: Felix Leupold Date: Fri, 20 Sep 2024 17:42:44 +0200 Subject: [PATCH 2/5] remove chain parameter from daily rebalancing since it is not needed --- .../profitability/10k_growth/daily_rebalancing_4055484.sql | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql b/cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql index 2883701..eeb92c2 100644 --- a/cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql +++ b/cowamm/profitability/10k_growth/daily_rebalancing_4055484.sql @@ -3,7 +3,6 @@ -- {{token_a}} - either token of the desired uni pool -- {{token_b}} - other token of the desired uni pool -- {{start}} - date as of which the analysis should run --- {{blockchain} - chain for which the query is running -- Note: not using a simpler recursive approach due to Dune's recursion depth limitation. -- Current value of initial investment can be computed as the product of cumulative price changes per day, since @@ -32,26 +31,22 @@ daily_price_change as ( on p1.day = ds.day and p1.contract_address = {{token_a}} - and p1.blockchain = '{{blockchain}}' left join prices.usd_daily as previous_p1 on previous_p1.day = ds.day - interval '1' day -- avoid computing price change on first day and previous_p1.day >= date(timestamp '{{start}}') and previous_p1.contract_address = {{token_a}} - and previous_p1.blockchain = '{{blockchain}}' inner join prices.usd_daily as p2 on p2.day = ds.day and p2.contract_address = {{token_b}} - and p2.blockchain = '{{blockchain}}' left join prices.usd_daily as previous_p2 on previous_p2.day = ds.day - interval '1' day -- avoid computing price change on first day and previous_p2.day >= date(timestamp '{{start}}') and previous_p2.contract_address = {{token_b}} - and previous_p2.blockchain = '{{blockchain}}' ) -- For each day multiply initial investment with cumulative product of average price change of the two assets From 9a44b8f1ea73bbd1910045251c92d7a3a9417132 Mon Sep 17 00:00:00 2001 From: Felix Leupold Date: Fri, 20 Sep 2024 17:48:28 +0200 Subject: [PATCH 3/5] fix lint --- cowamm/profitability/10k_growth/cow_4047078.sql | 2 +- cowamm/profitability/invariant_growth/cow_amm_4059213.sql | 2 +- cowamm/tvl_by_tx_4059700.sql | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cowamm/profitability/10k_growth/cow_4047078.sql b/cowamm/profitability/10k_growth/cow_4047078.sql index 3fd3e1a..c51f1aa 100644 --- a/cowamm/profitability/10k_growth/cow_4047078.sql +++ b/cowamm/profitability/10k_growth/cow_4047078.sql @@ -3,7 +3,7 @@ -- {{token_a}} - either token of the desired uni pool -- {{token_b}} - other token of the desired uni pool -- {{start}} - date as of which the analysis should run --- {{blockchain} - chain for which the query is +-- {{blockchain}} - chain for which the query is -- Given that we might not have records every day in the source data (e.g. not every day the lp supply may change), -- but still want to visualize development on a per day basis, we create an auxiliary table with one record per diff --git a/cowamm/profitability/invariant_growth/cow_amm_4059213.sql b/cowamm/profitability/invariant_growth/cow_amm_4059213.sql index 1f7f52a..bad4589 100644 --- a/cowamm/profitability/invariant_growth/cow_amm_4059213.sql +++ b/cowamm/profitability/invariant_growth/cow_amm_4059213.sql @@ -3,7 +3,7 @@ -- {{token_a}} - either token of the desired uni pool -- {{token_b}} - other token of the desired uni pool -- {{start}} - date as of which the analysis should run --- {{blockchain} - chain for which the query is running +-- {{blockchain}} - chain for which the query is running -- Finds the CoW AMM pool address given tokens specified in query parameters (regardless of order) with cow_amm_pool as ( diff --git a/cowamm/tvl_by_tx_4059700.sql b/cowamm/tvl_by_tx_4059700.sql index a753dbd..39c3c3f 100644 --- a/cowamm/tvl_by_tx_4059700.sql +++ b/cowamm/tvl_by_tx_4059700.sql @@ -2,7 +2,7 @@ -- Parameters -- {{token_a}} - either token of the pool -- {{token_b}} - other token of the pool --- {{blockchain} - chain for which the query is running +-- {{blockchain}} - chain for which the query is running with cow_amm_pool as ( select From e3d1b85e80e97cbc8707089acda660ec323870cb Mon Sep 17 00:00:00 2001 From: Felix Leupold Date: Mon, 23 Sep 2024 13:08:31 +0200 Subject: [PATCH 4/5] [CoW AMM] Make Uni reference pool queries chain agnostic --- .../10k_growth/uni_v2_4047194.sql | 56 +++++++++--- .../invariant_growth/uni_v2_4060136.sql | 88 ++++++++++++++----- 2 files changed, 109 insertions(+), 35 deletions(-) diff --git a/cowamm/profitability/10k_growth/uni_v2_4047194.sql b/cowamm/profitability/10k_growth/uni_v2_4047194.sql index ba007b3..537b5c4 100644 --- a/cowamm/profitability/10k_growth/uni_v2_4047194.sql +++ b/cowamm/profitability/10k_growth/uni_v2_4047194.sql @@ -17,15 +17,43 @@ with date_range as ( ), -- Finds the uniswap v2 pool address given tokens specified in query parameters (regardless of order) -pool as ( +pools as ( select - pool as contract_address, - token0, - token1 - from uniswap_ethereum.pools + substr(data, 13, 20) as contract_address, + substr(topic1, 13, 20) as token0, + substr(topic2, 13, 20) as token1 + from {{blockchain}}.logs + where + topic0 = 0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9 -- PairCreated + -- topic1: 0x0...0, topic2: 0x0...0 + and ((substr(topic1, 13, 20) = {{token_a}} and substr(topic2, 13, 20) = {{token_b}}) or (substr(topic2, 13, 20) = {{token_a}} and substr(topic1, 13, 20) = {{token_b}})) +), + +syncs as ( + select + tx_hash as evt_tx_hash, + index as evt_index, + block_time as evt_block_time, + block_number as evt_block_number, + contract_address, + varbinary_to_uint256(substr(data, 1, 32)) as reserve0, + varbinary_to_uint256(substr(data, 33, 32)) as reserve1, + rank() over (partition by (contract_address) order by block_time desc) as latest + from {{blockchain}}.logs where - ((token0 = {{token_a}} and token1 = {{token_b}}) or (token1 = {{token_a}} and token0 = {{token_b}})) - and version = 'v2' + topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync + and contract_address in (select contract_address from pools) +), + +-- select the pool with the largest latest k +pool as ( + select pools.* + from pools + inner join syncs + on + pools.contract_address = syncs.contract_address + and latest = 1 + order by (reserve0 * reserve1) desc limit 1 ), @@ -34,11 +62,11 @@ lp_supply_delta as ( select date(evt_block_time) as "day", sum(case when "from" = 0x0000000000000000000000000000000000000000 then value else -value end) as delta - from erc20_ethereum.evt_transfer + from erc20_{{blockchain}}.evt_transfer where contract_address = (select contract_address from pool) and ("from" = 0x0000000000000000000000000000000000000000 or "to" = 0x0000000000000000000000000000000000000000) - group by 1 + group by date(evt_block_time) ), -- per day lp token total supply by summing up all deltas (may have missing records for some days) @@ -89,11 +117,11 @@ latest_syncs_per_day as ( token1, reserve1, rank() over (partition by (date_range.day) order by (evt_block_number, evt_index) desc) as latest - from uniswap_v2_ethereum.Pair_evt_Sync as sync + from syncs inner join date_range on day >= date(evt_block_time) inner join pool - on sync.contract_address = pool.contract_address + on syncs.contract_address = pool.contract_address ), -- Get reserve balances of token_a and token_b per day, by looking at the last `Sync` emitted event for each day @@ -136,10 +164,10 @@ tvl as ( ) as balances left join prices.usd_daily as prices on - blockchain = 'ethereum' + blockchain = '{{blockchain}}' and balances.token = prices.contract_address and balances.day = prices.day - group by 1, 2 + group by balances.day, balances.contract_address ), -- With this we can plot the lp token price (tvl/lp total supply) over time @@ -167,4 +195,4 @@ select where day = timestamp '{{start}}' ) * lp_token_price as current_value_of_investment from lp_token_price -order by 1 desc +order by day desc diff --git a/cowamm/profitability/invariant_growth/uni_v2_4060136.sql b/cowamm/profitability/invariant_growth/uni_v2_4060136.sql index 862e125..0e54637 100644 --- a/cowamm/profitability/invariant_growth/uni_v2_4060136.sql +++ b/cowamm/profitability/invariant_growth/uni_v2_4060136.sql @@ -15,43 +15,88 @@ with date_range as ( ), -- Finds the uniswap v2 pool address given tokens specified in query parameters (regardless of order) -pool as ( +pools as ( + select + substr(data, 13, 20) as contract_address, + substr(topic1, 13, 20) as token0, + substr(topic2, 13, 20) as token1 + from {{blockchain}}.logs + where + topic0 = 0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9 -- PairCreated + -- topic1: 0x0...0, topic2: 0x0...0 + and ((substr(topic1, 13, 20) = {{token_a}} and substr(topic2, 13, 20) = {{token_b}}) or (substr(topic2, 13, 20) = {{token_a}} and substr(topic1, 13, 20) = {{token_b}})) +), + +syncs as ( select - pool as contract_address, - token0, - token1 - from uniswap_ethereum.pools + tx_hash as evt_tx_hash, + index as evt_index, + block_time as evt_block_time, + block_number as evt_block_number, + contract_address, + varbinary_to_uint256(substr(data, 1, 32)) as reserve0, + varbinary_to_uint256(substr(data, 33, 32)) as reserve1, + rank() over (partition by (contract_address) order by block_time desc) as latest + from {{blockchain}}.logs where - ((token0 = {{token_a}} and token1 = {{token_b}}) or (token1 = {{token_a}} and token0 = {{token_b}})) - and version = 'v2' + topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync + and contract_address in (select contract_address from pools) +), + +-- select the pool with the largest latest k +pool as ( + select pools.* + from pools + inner join syncs + on + pools.contract_address = syncs.contract_address + and latest = 1 + order by (reserve0 * reserve1) desc limit 1 ), --- gets the swapped volume and tvl at the time of the swap for each swap swaps as ( select - sync.evt_block_time, - sync.evt_tx_hash, + tx_hash as evt_tx_hash, + index as evt_index, + block_time as evt_block_time, + block_number as evt_block_number, + contract_address, + varbinary_to_uint256(substr(data, 1, 32)) as amount0In, + varbinary_to_uint256(substr(data, 33, 32)) as amount1In, + varbinary_to_uint256(substr(data, 65, 32)) as amount0Out, + varbinary_to_uint256(substr(data, 97, 32)) as amount1Out + from {{blockchain}}.logs + where + topic0 = 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822 -- Swap + and contract_address = (select contract_address from pool) +), + +-- gets the swapped volume and tvl at the time of the swap for each swap +tvl_volume_per_swap as ( + select + syncs.evt_block_time, + syncs.evt_tx_hash, (amount0In * p0.price / pow(10, p0.decimals)) + (amount1In * p1.price / pow(10, p1.decimals)) as volume_in, (amount0Out * p0.price / pow(10, p0.decimals)) + (amount1Out * p1.price / pow(10, p1.decimals)) as volume_out, (reserve0 * p0.price / pow(10, p0.decimals)) + (reserve1 * p1.price / pow(10, p1.decimals)) as tvl - from uniswap_v2_ethereum.Pair_evt_Sync as sync - inner join uniswap_v2_ethereum.Pair_evt_Swap as swap + from syncs + inner join swaps on - sync.evt_tx_hash = swap.evt_tx_hash - and sync.contract_address = swap.contract_address - and sync.evt_index + 1 = swap.evt_index + syncs.evt_tx_hash = swaps.evt_tx_hash + and syncs.contract_address = swaps.contract_address + and syncs.evt_index + 1 = swaps.evt_index inner join pool - on sync.contract_address = pool.contract_address + on syncs.contract_address = pool.contract_address inner join prices.usd as p0 on - date_trunc('minute', sync.evt_block_time) = p0.minute + date_trunc('minute', syncs.evt_block_time) = p0.minute and p0.contract_address = token0 inner join prices.usd as p1 on - date_trunc('minute', sync.evt_block_time) = p1.minute + date_trunc('minute', syncs.evt_block_time) = p1.minute and p1.contract_address = token1 - where sync.evt_block_time >= date(timestamp '{{start}}') + where syncs.evt_block_time >= date(timestamp '{{start}}') ) select @@ -61,6 +106,7 @@ select avg(tvl) as tvl, sum((volume_in + volume_out) / 2 / tvl) * 0.003 as pct_invariant_growth from date_range as dr -left join swaps +left join tvl_volume_per_swap on dr.day = date(evt_block_time) -group by 1 +group by day +order by day desc From 7495c1ceaf49f7a33bcb9c40dfe354b88768a131 Mon Sep 17 00:00:00 2001 From: Felix Leupold Date: Tue, 1 Oct 2024 14:44:41 +0200 Subject: [PATCH 5/5] extract common subquery --- .../10k_growth/uni_v2_4047194.sql | 44 +++---------------- .../invariant_growth/uni_v2_4060136.sql | 44 +++---------------- cowamm/uni_v2_syncs_4117043.sql | 27 ++++++++++++ 3 files changed, 39 insertions(+), 76 deletions(-) create mode 100644 cowamm/uni_v2_syncs_4117043.sql diff --git a/cowamm/profitability/10k_growth/uni_v2_4047194.sql b/cowamm/profitability/10k_growth/uni_v2_4047194.sql index 537b5c4..41e9af4 100644 --- a/cowamm/profitability/10k_growth/uni_v2_4047194.sql +++ b/cowamm/profitability/10k_growth/uni_v2_4047194.sql @@ -16,43 +16,11 @@ with date_range as ( )) t (day) --noqa: AL01 ), --- Finds the uniswap v2 pool address given tokens specified in query parameters (regardless of order) -pools as ( - select - substr(data, 13, 20) as contract_address, - substr(topic1, 13, 20) as token0, - substr(topic2, 13, 20) as token1 - from {{blockchain}}.logs - where - topic0 = 0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9 -- PairCreated - -- topic1: 0x0...0, topic2: 0x0...0 - and ((substr(topic1, 13, 20) = {{token_a}} and substr(topic2, 13, 20) = {{token_b}}) or (substr(topic2, 13, 20) = {{token_a}} and substr(topic1, 13, 20) = {{token_b}})) -), - -syncs as ( - select - tx_hash as evt_tx_hash, - index as evt_index, - block_time as evt_block_time, - block_number as evt_block_number, - contract_address, - varbinary_to_uint256(substr(data, 1, 32)) as reserve0, - varbinary_to_uint256(substr(data, 33, 32)) as reserve1, - rank() over (partition by (contract_address) order by block_time desc) as latest - from {{blockchain}}.logs - where - topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync - and contract_address in (select contract_address from pools) -), - -- select the pool with the largest latest k pool as ( - select pools.* - from pools - inner join syncs - on - pools.contract_address = syncs.contract_address - and latest = 1 + select contract_address, token0, token1 + from "query_4117043(blockchain='{{blockchain}}',token_a='{{token_a}}',token_b='{{token_b}}')" + where latest = 1 order by (reserve0 * reserve1) desc limit 1 ), @@ -112,12 +80,12 @@ latest_syncs_per_day as ( select day, pool.contract_address, - token0, + pool.token0, reserve0, - token1, + pool.token1, reserve1, rank() over (partition by (date_range.day) order by (evt_block_number, evt_index) desc) as latest - from syncs + from "query_4117043(blockchain='{{blockchain}}',token_a='{{token_a}}',token_b='{{token_b}}')" syncs inner join date_range on day >= date(evt_block_time) inner join pool diff --git a/cowamm/profitability/invariant_growth/uni_v2_4060136.sql b/cowamm/profitability/invariant_growth/uni_v2_4060136.sql index 0e54637..0fc6f46 100644 --- a/cowamm/profitability/invariant_growth/uni_v2_4060136.sql +++ b/cowamm/profitability/invariant_growth/uni_v2_4060136.sql @@ -14,43 +14,11 @@ with date_range as ( )) t (day) --noqa: AL01 ), --- Finds the uniswap v2 pool address given tokens specified in query parameters (regardless of order) -pools as ( - select - substr(data, 13, 20) as contract_address, - substr(topic1, 13, 20) as token0, - substr(topic2, 13, 20) as token1 - from {{blockchain}}.logs - where - topic0 = 0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9 -- PairCreated - -- topic1: 0x0...0, topic2: 0x0...0 - and ((substr(topic1, 13, 20) = {{token_a}} and substr(topic2, 13, 20) = {{token_b}}) or (substr(topic2, 13, 20) = {{token_a}} and substr(topic1, 13, 20) = {{token_b}})) -), - -syncs as ( - select - tx_hash as evt_tx_hash, - index as evt_index, - block_time as evt_block_time, - block_number as evt_block_number, - contract_address, - varbinary_to_uint256(substr(data, 1, 32)) as reserve0, - varbinary_to_uint256(substr(data, 33, 32)) as reserve1, - rank() over (partition by (contract_address) order by block_time desc) as latest - from {{blockchain}}.logs - where - topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync - and contract_address in (select contract_address from pools) -), - -- select the pool with the largest latest k pool as ( - select pools.* - from pools - inner join syncs - on - pools.contract_address = syncs.contract_address - and latest = 1 + select contract_address, token0, token1 + from "query_4117043(blockchain='{{blockchain}}',token_a='{{token_a}}',token_b='{{token_b}}')" + where latest = 1 order by (reserve0 * reserve1) desc limit 1 ), @@ -80,7 +48,7 @@ tvl_volume_per_swap as ( (amount0In * p0.price / pow(10, p0.decimals)) + (amount1In * p1.price / pow(10, p1.decimals)) as volume_in, (amount0Out * p0.price / pow(10, p0.decimals)) + (amount1Out * p1.price / pow(10, p1.decimals)) as volume_out, (reserve0 * p0.price / pow(10, p0.decimals)) + (reserve1 * p1.price / pow(10, p1.decimals)) as tvl - from syncs + from "query_4117043(blockchain='{{blockchain}}',token_a='{{token_a}}',token_b='{{token_b}}')" syncs inner join swaps on syncs.evt_tx_hash = swaps.evt_tx_hash @@ -91,11 +59,11 @@ tvl_volume_per_swap as ( inner join prices.usd as p0 on date_trunc('minute', syncs.evt_block_time) = p0.minute - and p0.contract_address = token0 + and p0.contract_address = syncs.token0 inner join prices.usd as p1 on date_trunc('minute', syncs.evt_block_time) = p1.minute - and p1.contract_address = token1 + and p1.contract_address = syncs.token1 where syncs.evt_block_time >= date(timestamp '{{start}}') ) diff --git a/cowamm/uni_v2_syncs_4117043.sql b/cowamm/uni_v2_syncs_4117043.sql new file mode 100644 index 0000000..bc35371 --- /dev/null +++ b/cowamm/uni_v2_syncs_4117043.sql @@ -0,0 +1,27 @@ +-- Finds the uniswap v2 pool address given tokens specified in query parameters (regardless of order) +with pools as ( + select + substr(data, 13, 20) as contract_address, + substr(topic1, 13, 20) as token0, + substr(topic2, 13, 20) as token1 + from {{blockchain}}.logs + where + topic0 = 0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9 -- PairCreated + -- topic1: 0x0...0, topic2: 0x0...0 + and ((substr(topic1, 13, 20) = {{token_a}} and substr(topic2, 13, 20) = {{token_b}}) or (substr(topic2, 13, 20) = {{token_a}} and substr(topic1, 13, 20) = {{token_b}})) +) + +select + tx_hash as evt_tx_hash, + index as evt_index, + block_time as evt_block_time, + block_number as evt_block_number, + varbinary_to_uint256(substr(data, 1, 32)) as reserve0, + varbinary_to_uint256(substr(data, 33, 32)) as reserve1, + pools.*, + rank() over (partition by (logs.contract_address) order by block_time desc) as latest +from {{blockchain}}.logs +join pools + on logs.contract_address = pools.contract_address +where + topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync