Skip to content

Commit

Permalink
Added/propagated through queries number_of_pools parameter and variou…
Browse files Browse the repository at this point in the history
…s comments
  • Loading branch information
PoloX2021 committed Nov 18, 2024
1 parent db8459a commit cb41f52
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- 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
-- Parameters:
-- {{blockchain}}: The blockchain to query
select
r.contract_address,
sum(amount_usd) as volume,
Expand All @@ -18,7 +19,8 @@ where
and tvl > 0
group by r.contract_address

-- NOw include pools which did not trade during the specified timeframe
-- Now include pools which did not trade during the specified timeframe
-- This allows to include a performance metric for every pool even the less active ones
union distinct
select
contract_address,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-- Finds all the curve pools with 2 tokens and their TVLs
-- Input: blockchain
-- Parameters:
-- {{blockchain}}: The blockchain to query
-- {{number_of_pools}}: The number of largest pools to return

with
-- filters pools with 2 tokens
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-- Computes volume, tvl and APR for Pancakeswap pools
-- APR is measured as the fees earned per $ invested, over the last 24 hours, projected over 1 year
-- Input: blockchain
-- Parameters:
-- {{blockchain}}: The blockchain to query

with
-- select the pool with the largest latest k
pool as (
with pool as (
select
contract_address,
token0,
Expand Down Expand Up @@ -62,18 +62,20 @@ 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

-- Now include pools which did not trade during the specified timeframe
-- This allows to include a performance metric for every pool even the less active ones
union
select
pool_address as contract_address,
0 as volume,
select
pool_address as contract_address,
0 as volume,
tvl,
0 as apr,
0.003 as fee
from "query_4232597(blockchain='{{blockchain}}')"
from "query_4232597(blockchain='{{blockchain}}', number_of_pools = '500')"
where pool_address not in (select contract_address from tvl_volume_per_swap)
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
-- Computes the TVL for every Pancakeswap pool
-- Then returns the top {{number_of_pools}} pools by TVL
-- Input: blockchain, number_of_pools to return
-- Parameters:
-- {{blockchain}}: The blockchain to query
-- {{number_of_pools}}: The number of largest pools to return

with
-- finds the pools which have been active since 2024-10-01
data as (
select
p.contract_address as pool_address,
p.call_block_time as time,
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
Expand Down Expand Up @@ -35,6 +37,7 @@ t1 as (
--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
-- This decision does not have much impact on metrics, as it is just to rank the pools.
recent_tvl as (
select
pool_address,
Expand All @@ -56,7 +59,7 @@ recent_tvl as (
inner join prices.usd_latest as p1
on token1 = p1.contract_address
where latest = 1
group by 1, 2, 4
group by pool_address, token0, token1
)

select * from recent_tvl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
-- Finds the pancakeswap v2 pool address given tokens specified in query parameters (regardless of order)
-- This query finds all the sync event which happens after a swap and reference the new reserves of the pool
-- Parameters:
-- {{blockchain}}: The blockchain to query

-- Finds the pools with the largest tvl
with pools as (
select
substr(data, 13, 20) as contract_address,
Expand All @@ -8,7 +12,7 @@ with pools as (
where
topic0 = 0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9 -- PairCreated
-- topic1: 0x0...0<token0>, topic2: 0x0...0<token1>
and substr(data, 13, 20) in (select pool_address from "query_4232597(blockchain='{{blockchain}}')")
and substr(data, 13, 20) in (select pool_address from "query_4232597(blockchain='{{blockchain}}', number_of_pools = '500')")
)

select
Expand Down

0 comments on commit cb41f52

Please sign in to comment.