From 45d3f2a08c3adbaaedc91ed2b60fa4878c7587e0 Mon Sep 17 00:00:00 2001 From: "pauljanicot@yahoo.com" Date: Wed, 6 Nov 2024 16:10:10 +0000 Subject: [PATCH] Formatting --- .../curve/curve_kpis_4232873.sql | 40 +++++----- .../curve_largest_2token_pools_4232976.sql | 1 - .../pancakeswap_largest_pools_4232597.sql | 79 ++++++++++-------- .../pancakeswap/pancakeswap_syncs_4232660.sql | 2 +- .../sushiswap/sushiswap_kpis_4232588.sql | 16 ++-- .../sushiswap_largest_pools_4223554.sql | 78 +++++++++++------- .../sushiswap/sushiswap_syncs_4227247.sql | 2 +- .../uniswap/uniswap_kpis_4229217.sql | 16 ++-- .../uniswap/uniswap_largest_pools_4223063.sql | 80 +++++++++++-------- 9 files changed, 182 insertions(+), 132 deletions(-) diff --git a/cowamm/profitability/competitor_kpis/curve/curve_kpis_4232873.sql b/cowamm/profitability/competitor_kpis/curve/curve_kpis_4232873.sql index d8a2b389..248bd876 100644 --- a/cowamm/profitability/competitor_kpis/curve/curve_kpis_4232873.sql +++ b/cowamm/profitability/competitor_kpis/curve/curve_kpis_4232873.sql @@ -1,27 +1,31 @@ -- Computes volume, tvl and APR for Curve pools -- APR is measured as the fees earned per $ invested, over the last 24 hours, projected over 1 year -- Input: blockchain -select r.contract_address, -sum(amount_usd) as volume, -365*sum(amount_usd*fee/tvl) as apr, -avg(fee) as fee, -avg(tvl) as tvl -from "query_4232976(blockchain='{{blockchain}}')" r -left join curve.trades t -on r.contract_address = t.project_contract_address -and r.tx_hash = t.tx_hash -where t.block_time>=date_add('day', -1, now()) --- This test avoids any possible issue with reconstructing the reserves of the pool -and tvl >0 +select + r.contract_address, + sum(amount_usd) as volume, + 365 * sum(amount_usd * fee / tvl) as apr, + avg(fee) as fee, + avg(tvl) as tvl +from "query_4232976(blockchain='{{blockchain}}')" as r +left join curve.trades as t + on + r.contract_address = t.project_contract_address + and r.tx_hash = t.tx_hash +where + t.block_time >= date_add('day', -1, now()) + -- This test avoids any possible issue with reconstructing the reserves of the pool + and tvl > 0 group by r.contract_address -union -select - contract_address, - 0 as volume, +union distinct +select + contract_address, + 0 as volume, 0 as apr, fee, tvl from "query_4232976(blockchain='{{blockchain}}')" -where time= date_trunc('day', cast('2024-10-01' as date))), + select + p.contract_address as pool_address, + p.call_block_time as time, + p.output__reserve0 as balance0, + p.output__reserve1 as balance1, + rank() over (partition by p.contract_address order by p.call_block_time desc) as latest + from pancakeswap_v2_{{blockchain}}.PancakePair_call_getReserves as p + where date_trunc('day', p.call_block_time) >= date_trunc('day', cast('2024-10-01' as date)) +), --Gets the token0 and token1 addresses for each pool -t0 as (select contract_address, max(output_0) as token0 +t0 as ( + select + contract_address, + max(output_0) as token0 from pancakeswap_v2_{{blockchain}}.PancakePair_call_token0 - group by contract_address), - -t1 as (select contract_address, max(output_0) as token1 + group by contract_address +), + +t1 as ( + select + contract_address, + max(output_0) as token1 from pancakeswap_v2_{{blockchain}}.PancakePair_call_token1 - group by contract_address), + group by contract_address +), --computes the tvl for each pool -- for each pool we could get multiple balance values if the function was called multiple times in a same block -- we arbitrarily choose the maximum value for each pool -recent_tvl as( -select pool_address, -token0, max(balance0) as balance0, -token1, max(balance1) as balance1, -max(least(balance0,balance1)* greatest(p0.price/pow(10, p0.decimals),p1.price/pow(10, p1.decimals)) + - greatest(balance0,balance1)* least(p0.price/pow(10, p0.decimals),p1.price/pow(10, p1.decimals))) as tvl -from data -join t0 - on pool_address = t0.contract_address -join t1 - on pool_address = t1.contract_address -join prices.usd_latest as p0 - on token0 = p0.contract_address -join prices.usd_latest as p1 - on token1 = p1.contract_address -where latest = 1 -group by 1,2,4) +recent_tvl as ( + select + pool_address, + token0, + max(balance0) as balance0, + token1, + max(balance1) as balance1, + max( + least(balance0, balance1) * greatest(p0.price / pow(10, p0.decimals), p1.price / pow(10, p1.decimals)) + + greatest(balance0, balance1) * least(p0.price / pow(10, p0.decimals), p1.price / pow(10, p1.decimals)) + ) as tvl + from data + inner join t0 + on pool_address = t0.contract_address + inner join t1 + on pool_address = t1.contract_address + inner join prices.usd_latest as p0 + on token0 = p0.contract_address + inner join prices.usd_latest as p1 + on token1 = p1.contract_address + where latest = 1 + group by 1, 2, 4 +) select * from recent_tvl order by tvl desc -limit {{number_of_pools}} \ No newline at end of file +limit {{number_of_pools}} diff --git a/cowamm/profitability/competitor_kpis/pancakeswap/pancakeswap_syncs_4232660.sql b/cowamm/profitability/competitor_kpis/pancakeswap/pancakeswap_syncs_4232660.sql index a558f4e1..89cf836b 100644 --- a/cowamm/profitability/competitor_kpis/pancakeswap/pancakeswap_syncs_4232660.sql +++ b/cowamm/profitability/competitor_kpis/pancakeswap/pancakeswap_syncs_4232660.sql @@ -24,4 +24,4 @@ from {{blockchain}}.logs inner join pools on logs.contract_address = pools.contract_address where - topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync \ No newline at end of file + topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync diff --git a/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_kpis_4232588.sql b/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_kpis_4232588.sql index e2ae9cf5..1dde9581 100644 --- a/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_kpis_4232588.sql +++ b/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_kpis_4232588.sql @@ -2,7 +2,7 @@ -- APR is measured as the fees earned per $ invested, over the last 24 hours, projected over 1 year -- Input: blockchain -with +with -- select the pool with the largest latest k pool as ( select @@ -33,7 +33,7 @@ swaps as ( -- gets the swapped volume and tvl at the time of the swap for each swap tvl_volume_per_swap as ( select - syncs.contract_address as contract_address, + syncs.contract_address, 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, @@ -62,16 +62,16 @@ select contract_address, sum((volume_in + volume_out) / 2) as volume, avg(tvl) as tvl, - 365*sum((volume_in + volume_out) / 2 / tvl) * 0.003 as apr, + 365 * sum((volume_in + volume_out) / 2 / tvl) * 0.003 as apr, 0.003 as fee from tvl_volume_per_swap - where evt_block_time >= date_add('day', -1, now()) +where evt_block_time >= date_add('day', -1, now()) group by contract_address -union -select - pool_address as contract_address, - 0 as volume, +union distinct +select + pool_address as contract_address, + 0 as volume, tvl, 0 as apr, 0.003 as fee diff --git a/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_largest_pools_4223554.sql b/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_largest_pools_4223554.sql index d086af0e..396c7231 100644 --- a/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_largest_pools_4223554.sql +++ b/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_largest_pools_4223554.sql @@ -2,47 +2,63 @@ -- Then returns the top {{number_of_pools}} pools by TVL -- Input: blockchain, number_of_pools to return -with +with -- finds the pools which have been active since 2024-10-01 data as ( -select - p.contract_address as pool_address, - call_block_time as time, - output__reserve0 as balance0, - output__reserve1 as balance1, - rank() over (partition by p.contract_address order by p.call_block_time desc) latest -from sushi_{{blockchain}}.Pair_call_getReserves p -where date_trunc('day',call_block_time) >= date_trunc('day', cast('2024-10-01' as date))), + select + p.contract_address as pool_address, + p.call_block_time as block_time, + p.output__reserve0 as balance0, + p.output__reserve1 as balance1, + rank() over (partition by p.contract_address order by p.call_block_time desc) as latest + from sushi_{{blockchain}}.Pair_call_getReserves as p + where date_trunc('day', p.call_block_time) >= date_trunc('day', cast('2024-10-01' as date)) +), --Gets the token0 and token1 addresses for each pool -t0 as (select contract_address, max(output_0) as token0 +t0 as ( + select + contract_address, + max(output_0) as token0 from sushi_{{blockchain}}.Pair_call_token0 - group by contract_address), -t1 as (select contract_address, max(output_0) as token1 + group by contract_address +), + +t1 as ( + select + contract_address, + max(output_0) as token1 from sushi_{{blockchain}}.Pair_call_token1 - group by contract_address), + group by contract_address +), --computes the tvl for each pool -- for each pool we could get multiple balance values if the function was called multiple times in a same block -- we arbitrarily choose the maximum value for each pool -recent_tvl as( -select pool_address, -token0, max(balance0) as balance0, -token1, max(balance1) as balance1, -max(least(balance0,balance1)* greatest(p0.price/pow(10, p0.decimals),p1.price/pow(10, p1.decimals)) + - greatest(balance0,balance1)* least(p0.price/pow(10, p0.decimals),p1.price/pow(10, p1.decimals))) as tvl -from data -join t0 - on pool_address = t0.contract_address -join t1 - on pool_address = t1.contract_address -join prices.usd_latest as p0 - on token0 = p0.contract_address -join prices.usd_latest as p1 - on token1 = p1.contract_address -where latest = 1 -group by 1,2,4) +recent_tvl as ( + select + pool_address, + token0, + max(balance0) as balance0, + token1, + max(balance1) as balance1, + max( + least(balance0, balance1) * greatest(p0.price / pow(10, p0.decimals), p1.price / pow(10, p1.decimals)) + + greatest(balance0, balance1) * least(p0.price / pow(10, p0.decimals), p1.price / pow(10, p1.decimals)) + ) as tvl + from data + inner join t0 + on pool_address = t0.contract_address + inner join t1 + on pool_address = t1.contract_address + inner join prices.usd_latest as p0 + on token0 = p0.contract_address + inner join prices.usd_latest as p1 + on token1 = p1.contract_address + where latest = 1 + group by 1, 2, 4 +) select * from recent_tvl order by tvl desc -limit {{number_of_pools}} \ No newline at end of file +limit {{number_of_pools}} diff --git a/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_syncs_4227247.sql b/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_syncs_4227247.sql index b885776b..5714fb08 100644 --- a/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_syncs_4227247.sql +++ b/cowamm/profitability/competitor_kpis/sushiswap/sushiswap_syncs_4227247.sql @@ -24,4 +24,4 @@ from {{blockchain}}.logs inner join pools on logs.contract_address = pools.contract_address where - topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync \ No newline at end of file + topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync diff --git a/cowamm/profitability/competitor_kpis/uniswap/uniswap_kpis_4229217.sql b/cowamm/profitability/competitor_kpis/uniswap/uniswap_kpis_4229217.sql index d2a1fb2e..766b90af 100644 --- a/cowamm/profitability/competitor_kpis/uniswap/uniswap_kpis_4229217.sql +++ b/cowamm/profitability/competitor_kpis/uniswap/uniswap_kpis_4229217.sql @@ -2,7 +2,7 @@ -- APR is measured as the fees earned per $ invested, over the last 24 hours, projected over 1 year -- Input: blockchain -with +with -- select the pool with the largest latest k pool as ( select @@ -33,7 +33,7 @@ swaps as ( -- gets the swapped volume and tvl at the time of the swap for each swap tvl_volume_per_swap as ( select - syncs.contract_address as contract_address, + syncs.contract_address, 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, @@ -62,16 +62,16 @@ select contract_address, sum((volume_in + volume_out) / 2) as volume, avg(tvl) as tvl, - 365*sum((volume_in + volume_out) / 2 / tvl) * 0.003 as apr, + 365 * sum((volume_in + volume_out) / 2 / tvl) * 0.003 as apr, 0.003 as fee from tvl_volume_per_swap - where evt_block_time >= date_add('day', -1, now()) +where evt_block_time >= date_add('day', -1, now()) group by contract_address -union -select - pool_address as contract_address, - 0 as volume, +union distinct +select + pool_address as contract_address, + 0 as volume, tvl, 0 as apr, 0.003 as fee diff --git a/cowamm/profitability/competitor_kpis/uniswap/uniswap_largest_pools_4223063.sql b/cowamm/profitability/competitor_kpis/uniswap/uniswap_largest_pools_4223063.sql index 93ab4f9f..566b60ae 100644 --- a/cowamm/profitability/competitor_kpis/uniswap/uniswap_largest_pools_4223063.sql +++ b/cowamm/profitability/competitor_kpis/uniswap/uniswap_largest_pools_4223063.sql @@ -2,47 +2,63 @@ -- Then returns the top {{number_of_pools}} pools by TVL -- Input: blockchain, number_of_pools to return -with +with -- finds the pools which have been active since 2024-10-01 data as ( -select - p.contract_address as pool_address, - call_block_time as time, - output__reserve0 as balance0, - output__reserve1 as balance1, - rank() over (partition by p.contract_address order by p.call_block_time desc) latest -from uniswap_v2_{{blockchain}}.Pair_call_getReserves p -where date_trunc('day',call_block_time) >= date_trunc('day', cast('2024-10-01' as date))), + select + p.contract_address as pool_address, + p.call_block_time as block_time, + p.output__reserve0 as balance0, + p.output__reserve1 as balance1, + rank() over (partition by p.contract_address order by p.call_block_time desc) as latest + from uniswap_v2_{{blockchain}}.Pair_call_getReserves as p + where date_trunc('day', p.call_block_time) >= date_trunc('day', cast('2024-10-01' as date)) +), --Gets the token0 and token1 addresses for each pool -t0 as (select contract_address, max(output_0) as token0 +t0 as ( + select + contract_address, + max(output_0) as token0 from uniswap_v2_{{blockchain}}.Pair_call_token0 - group by contract_address), -t1 as (select contract_address, max(output_0) as token1 - from uniswap_v2_{{blockchain}}.Pair_call_token1 - group by contract_address), + group by contract_address +), + +t1 as ( + select + contract_address, + max(output_0) as token1 + from uniswap_v2_{{blockchain}}.Pair_call_token1 + group by contract_address +), --computes the tvl for each pool -- for each pool we could get multiple balance values if the function was called multiple times in a same block -- we arbitrarily choose the maximum value for each pool -recent_tvl as( -select pool_address, -token0, max(balance0) as balance0, -token1, max(balance1) as balance1, -max(least(balance0,balance1)* greatest(p0.price/pow(10, p0.decimals),p1.price/pow(10, p1.decimals)) + - greatest(balance0,balance1)* least(p0.price/pow(10, p0.decimals),p1.price/pow(10, p1.decimals))) as tvl -from data -join t0 - on pool_address = t0.contract_address -join t1 - on pool_address = t1.contract_address -join prices.usd_latest as p0 - on token0 = p0.contract_address -join prices.usd_latest as p1 - on token1 = p1.contract_address -where latest = 1 -group by 1,2,4) +recent_tvl as ( + select + pool_address, + token0, + max(balance0) as balance0, + token1, + max(balance1) as balance1, + max( + least(balance0, balance1) * greatest(p0.price / pow(10, p0.decimals), p1.price / pow(10, p1.decimals)) + + greatest(balance0, balance1) * least(p0.price / pow(10, p0.decimals), p1.price / pow(10, p1.decimals)) + ) as tvl + from data + inner join t0 + on pool_address = t0.contract_address + inner join t1 + on pool_address = t1.contract_address + inner join prices.usd_latest as p0 + on token0 = p0.contract_address + inner join prices.usd_latest as p1 + on token1 = p1.contract_address + where latest = 1 + group by 1, 2, 4 +) select * from recent_tvl order by tvl desc -limit {{number_of_pools}} \ No newline at end of file +limit {{number_of_pools}}