-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimizing competitor kpis #89
Changes from 6 commits
3d12d6e
cc2ad7b
bdacff9
02db83b
2123552
ffe78ba
65c9076
dd3d1f9
20bd708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,46 +54,42 @@ reserves as ( | |
token0, | ||
token1, | ||
tx_hash, | ||
block_time, | ||
block_time as evt_block_time, | ||
fee, | ||
sum(transfer0) over (partition by contract_address order by block_time, evt_index) as reserve0, | ||
sum(transfer1) over (partition by contract_address order by block_time, evt_index) as reserve1, | ||
row_number() over (partition by tx_hash, contract_address order by evt_index desc) as latest_per_tx, | ||
row_number() over (partition by contract_address order by block_time desc) as latest_per_pool | ||
row_number() over (partition by contract_address order by block_time desc, evt_index desc) as latest_per_pool | ||
from transfers | ||
where block_time <= (case when '{{end_time}}' = '2100-01-01' then now() else timestamp '{{end_time}}' end) | ||
), | ||
|
||
-- finds the TVL of the pools | ||
recent_tvl as ( | ||
latest_tvl as ( | ||
select | ||
r.contract_address, | ||
token0, | ||
token1, | ||
block_time, | ||
tx_hash, | ||
reserve0, | ||
reserve1, | ||
fee, | ||
latest_per_pool, | ||
(reserve0 * p0.price / pow(10, p0.decimals)) + (reserve1 * p1.price / pow(10, p1.decimals)) as tvl | ||
from reserves as r | ||
inner join prices.minute as p0 | ||
--using the daily value to get a better representation of the TVL over the 24 hour period | ||
inner join prices.day as p0 | ||
on | ||
date_trunc('minute', block_time) = p0.timestamp | ||
p0.timestamp = date_trunc('day', case when ('{{end_time}}' = '2100-01-01' or date('{{end_time}}') = date_trunc('day', now())) then now() - interval '1' day else date('{{end_time}}') end) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this logic seems unnecessarily complex. Isn't this the same as
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also please document the new parameter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, indeed :) |
||
and token0 = p0.contract_address | ||
inner join prices.minute as p1 | ||
inner join prices.day as p1 | ||
on | ||
date_trunc('minute', block_time) = p1.timestamp | ||
p1.timestamp = date_trunc('day', case when ('{{end_time}}' = '2100-01-01' or date('{{end_time}}') = date_trunc('day', now())) then now() - interval '1' day else date('{{end_time}}') end) | ||
and token1 = p1.contract_address | ||
where latest_per_tx = 1 | ||
) | ||
|
||
|
||
select * from recent_tvl | ||
where contract_address in ( | ||
select contract_address | ||
from recent_tvl | ||
where latest_per_pool = 1 | ||
order by tvl desc | ||
limit {{number_of_pools}} | ||
) | ||
|
||
select | ||
reserves.*, | ||
tvl | ||
from reserves | ||
inner join latest_tvl | ||
on reserves.contract_address = latest_tvl.contract_address | ||
where latest_per_tx = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe using
is slightly easier to read? Also I'm not sure if reducing the range to
now()
is actually making the query more effective (since there are no datapoints beyond now the full table scan should complete in the same time)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
About the now(), the query is built to only search for 24 hours, so the upper bound is meant to backfill the data base