Skip to content
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

Cowamm/univ2 multichain #48

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions cowamm/profitability/10k_growth/uni_v2_4047194.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ with date_range as (
)) t (day) --noqa: AL01
),

-- Finds the uniswap v2 pool address given tokens specified in query parameters (regardless of order)
-- select the pool with the largest latest k
pool as (
select
pool as contract_address,
token0,
token1
from uniswap_ethereum.pools
where
((token0 = {{token_a}} and token1 = {{token_b}}) or (token1 = {{token_a}} and token0 = {{token_b}}))
and version = 'v2'
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
),

Expand All @@ -34,11 +30,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)
Expand Down Expand Up @@ -84,16 +80,16 @@ 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 uniswap_v2_ethereum.Pair_evt_Sync as sync
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
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
Expand Down Expand Up @@ -136,10 +132,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
Expand Down Expand Up @@ -167,4 +163,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
62 changes: 38 additions & 24 deletions cowamm/profitability/invariant_growth/uni_v2_4060136.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,57 @@ with date_range as (
)) t (day) --noqa: AL01
),

-- Finds the uniswap v2 pool address given tokens specified in query parameters (regardless of order)
-- select the pool with the largest latest k
pool as (
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
),

swaps 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 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
((token0 = {{token_a}} and token1 = {{token_b}}) or (token1 = {{token_a}} and token0 = {{token_b}}))
and version = 'v2'
limit 1
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
swaps as (
tvl_volume_per_swap as (
select
sync.evt_block_time,
sync.evt_tx_hash,
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 "query_4117043(blockchain='{{blockchain}}',token_a='{{token_a}}',token_b='{{token_b}}')" 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
and p0.contract_address = token0
date_trunc('minute', syncs.evt_block_time) = p0.minute
and p0.contract_address = syncs.token0
inner join prices.usd as p1
on
date_trunc('minute', sync.evt_block_time) = p1.minute
and p1.contract_address = token1
where sync.evt_block_time >= date(timestamp '{{start}}')
date_trunc('minute', syncs.evt_block_time) = p1.minute
and p1.contract_address = syncs.token1
where syncs.evt_block_time >= date(timestamp '{{start}}')
)

select
Expand All @@ -61,6 +74,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
27 changes: 27 additions & 0 deletions cowamm/uni_v2_syncs_4117043.sql
Original file line number Diff line number Diff line change
@@ -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<token0>, topic2: 0x0...0<token1>
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
Loading