Skip to content

Commit

Permalink
Add dbt models
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh9200 committed Jan 24, 2024
1 parent d7bc94e commit 8f39307
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

with final as (
select
evt_tx_hash as transaction_hash
, evt_index as log_index
, evt_block_time as block_time
, evt_block_number as block_number
, amount0
, amount1
from mint
'0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852' as pool_address
, '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' as token0__id
, 'Wrapped ETH' as token0__name
, 'WETH' as token0__symbol
, '18' as token0__decimals
, '0xdAC17F958D2ee523a2206206994597C13D831ec7' as token1__id
, 'Tether USD' as token1__name
, 'USDT' as token1__symbol
, '6' as token1__decimals
)

select * from final
16 changes: 14 additions & 2 deletions sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__swaps.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
{% set pool = ref("uniswap_v2_ethereum__pool") %}

with final as (
select
evt_tx_hash as transaction_hash
, evt_index as log_index
, evt_block_time as block_time
, evt_block_number as block_number
, amount0_in - amount0_out as amount0
, amount1_in - amount1_out as amount1
, case
when amount0_in > 0 then p.token0__id else p.token1__id
end as token_in__id
, case
when amount0_in > 0 then amount0_in else amount1_in
end as amount_in
, case
when amount0_in > 0 then p.token1__id else p.token0__id
end as token_out__id
, case
when amount0_in > 0 then amount1_out else amount0_out
end as amount_out
from swap
cross join {{ pool }} p
)

select * from final
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% set swaps = ref("uniswap_v2_ethereum__swaps") %}

with final as (
select
transaction_hash
, log_index
, amount_in
, amount_out
, token_in__id
, case
when token_in__id = '0xdAC17F958D2ee523a2206206994597C13D831ec7' then 6 else 18
end as token_in__decimals
, case
when token_in__id = '0xdAC17F958D2ee523a2206206994597C13D831ec7' then 1 else (amount_out / pow(10, 6)) / (amount_in / pow(10, 18))
end as token_in__price
, token_out__id
, case
when token_out__id = '0xdAC17F958D2ee523a2206206994597C13D831ec7' then 6 else 18
end as token_out__decimals
, case
when token_out__id = '0xdAC17F958D2ee523a2206206994597C13D831ec7' then 1 else (amount_in / pow(10, 6)) / (amount_out / pow(10, 18))
end as token_out__price
from {{ swaps }}
)

select * from final
33 changes: 33 additions & 0 deletions sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__tvl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% set sync = ref("uniswap_v2_ethereum__sync") %}
{% set prices = ref("uniswap_v2_ethereum__token_prices") %}

with tvl as (
select
s.transaction_hash
, s.log_index
, s.block_number
, s.block_time
, case
when sw.token_in__id = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' then (s.reserve0 / pow(10, 18)) * sw.token_in__price
when sw.token_out__id = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' then (s.reserve0 / pow(10, 18)) * sw.token_out__price
end as token0_balance_usd
, s.reserve1 / pow(10, 6) as token1_balance_usd
from {{ sync }} s
left join {{ prices }} sw
on s.transaction_hash = sw.transaction_hash
and s.log_index = sw.log_index - 1
)

, final as (
select
transaction_hash
, log_index
, block_number
, block_time
, token0_balance_usd
, token1_balance_usd
, (token0_balance_usd + token1_balance_usd) as tvl
from tvl
)

select * from final
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% set swaps = ref("uniswap_v2_ethereum__swaps") %}
{% set prices = ref("uniswap_v2_ethereum__token_prices") %}

with swap_volume as (
select
s.transaction_hash
, s.log_index
, s.block_number
, s.block_time
, s.token_in__id
, (s.amount_in * p.token_in__price) / pow(10, p.token_in__decimals) as amount_in_usd
, s.token_out__id
, (s.amount_out * p.token_out__price) / pow(10, p.token_out__decimals) as amount_out_usd
from {{ swaps }} s
left join {{ prices }} p
on s.transaction_hash = p.transaction_hash
and s.log_index = p.log_index
)

, final as (
select
transaction_hash
, log_index
, block_number
, block_time
, amount_in_usd
, amount_out_usd
, (amount_in_usd + amount_out_usd) / 2 as volume
from swap_volume
)

select * from final

0 comments on commit 8f39307

Please sign in to comment.