diff --git a/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__pool.sql b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__pool.sql index 39638e6..d1c2738 100644 --- a/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__pool.sql +++ b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__pool.sql @@ -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 \ No newline at end of file diff --git a/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__swaps.sql b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__swaps.sql index 1342a80..26e49bc 100644 --- a/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__swaps.sql +++ b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__swaps.sql @@ -1,3 +1,4 @@ +{% set pool = ref("uniswap_v2_ethereum__pool") %} with final as ( select @@ -5,9 +6,20 @@ with final as ( , 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 diff --git a/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__token_prices.sql b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__token_prices.sql new file mode 100644 index 0000000..96b7a11 --- /dev/null +++ b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__token_prices.sql @@ -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 \ No newline at end of file diff --git a/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__tvl.sql b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__tvl.sql new file mode 100644 index 0000000..b5e1b2a --- /dev/null +++ b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__tvl.sql @@ -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 \ No newline at end of file diff --git a/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__volume.sql b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__volume.sql new file mode 100644 index 0000000..a3a1165 --- /dev/null +++ b/sql/dbt/uniswap_v2/models/analytics/uniswap_v2_ethereum__volume.sql @@ -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 \ No newline at end of file