Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
PoloX2021 committed Nov 6, 2024
1 parent dd0dccc commit 45d3f2a
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 132 deletions.
40 changes: 22 additions & 18 deletions cowamm/profitability/competitor_kpis/curve/curve_kpis_4232873.sql
Original file line number Diff line number Diff line change
@@ -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_add('day', -1, now())
and latest = 1
where
time < date_add('day', -1, now())
and latest = 1
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,3 @@ inner join prices.usd as p0
date_trunc('minute', time) = p1.minute
and token1 = p1.contract_address
where row_num = 1

Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +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 pancakeswap_v2_{{blockchain}}.PancakePair_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 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}}
limit {{number_of_pools}}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ from {{blockchain}}.logs
inner join pools
on logs.contract_address = pools.contract_address
where
topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync
topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
limit {{number_of_pools}}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ from {{blockchain}}.logs
inner join pools
on logs.contract_address = pools.contract_address
where
topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync
topic0 = 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 -- Sync
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 45d3f2a

Please sign in to comment.