From 5b0f10ef6f19368208560336f90517d81cd47e1c Mon Sep 17 00:00:00 2001 From: Sebastian Melendez Date: Wed, 19 Jun 2024 19:05:02 -0400 Subject: [PATCH] updating curve models --- .../fact_curve_dex_swaps.sql | 28 +++++++------ models/staging/curve/dim_curve_pools.sql | 39 ++++++++++--------- .../curve/fact_curve_avalanche_dex_swaps.sql | 2 +- .../curve/fact_curve_ethereum_dex_swaps.sql | 2 +- .../curve/fact_curve_optimism_dex_swaps.sql | 2 +- .../curve/fact_curve_polygon_dex_swaps.sql | 2 +- 6 files changed, 37 insertions(+), 38 deletions(-) diff --git a/macros/decentralized_exchanges/fact_curve_dex_swaps.sql b/macros/decentralized_exchanges/fact_curve_dex_swaps.sql index 14ec26c4..5adbb55f 100644 --- a/macros/decentralized_exchanges/fact_curve_dex_swaps.sql +++ b/macros/decentralized_exchanges/fact_curve_dex_swaps.sql @@ -84,7 +84,7 @@ where event_name in ('TokenExchange', 'TokenExchangeUnderlying') {% if is_incremental() %} - and t1.block_timestamp >= (select max(block_timestamp) from {{ this }}) + and t1.block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }}) {% endif %} ), average_token_price_per_day as ( @@ -117,20 +117,16 @@ t1.token_out, t2.symbol as token_out_symbol, - coalesce(t1.amount_out, 0) as amount_out, + t1.amount_out, 0 as amount_out, t2.decimals as token_out_decimals, - coalesce(t2.price, t3.price) as token_out_price, - coalesce( - (amount_out / pow(10, token_out_decimals)) * token_out_price, 0 - ) as amount_out_usd, + t2.price as token_out_price, + amount_out / pow(10, token_out_decimals) * token_out_price as amount_out_usd, t1.token_in, t3.symbol as token_in_symbol, - coalesce(t1.amount_in, 0) as amount_in, + t1.amount_in as amount_in, t3.decimals as token_in_decimals, - coalesce(t3.price, 0) as token_in_price, - coalesce( - (amount_in / pow(10, token_in_decimals)) * token_in_price, 0 - ) as amount_in_usd, + t3.price as token_in_price, + amount_in / pow(10, token_in_decimals) * token_in_price as amount_in_usd, amount_out_usd * t1.swap_fee as trading_fee, trading_fee * admin_fee as revenue from pool_events t1 @@ -144,7 +140,7 @@ and lower(t1.token_in) = lower(t3.token_address) ), events as ( - select + select block_timestamp, tx_hash, event_index, @@ -155,7 +151,7 @@ token_out_symbol, token_in, token_in_symbol, - least(amount_in_usd, amount_out_usd) as trading_volume, + coalesce(least(amount_in_usd, amount_out_usd), 0) as trading_volume, trading_fee as trading_fees, revenue as trading_revenue, ROW_NUMBER() OVER (PARTITION by tx_hash, pool ORDER BY event_index) AS row_number @@ -177,8 +173,7 @@ and substr(t1.input, 0, 10) in ('0x3df02124', '0xa6417ed6') left join {{ _chain }}_flipside.core.fact_transactions t3 on t1.tx_hash = t3.tx_hash {% if is_incremental() %} - where t1.block_timestamp - >= (select max(block_timestamp) from {{ this }}) + where t1.block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }}) {% endif %} ) @@ -208,4 +203,7 @@ and events.pool = traces.to_address and events.row_number = traces.row_number where events.block_timestamp is not null + {% if is_incremental() %} + and events.block_timestamp >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }}) + {% endif %} {% endmacro %} diff --git a/models/staging/curve/dim_curve_pools.sql b/models/staging/curve/dim_curve_pools.sql index 14fe397d..395811cd 100644 --- a/models/staging/curve/dim_curve_pools.sql +++ b/models/staging/curve/dim_curve_pools.sql @@ -34,25 +34,26 @@ with select chain, - app, + max(app) as app, 'DeFi' as category, - pool_type, - registration_address, + max(pool_type) as pool_type, + max(registration_address) as registration_address, pool_address, - token, - amplification_coefficient, - name, - symbol, - swap_fee, - admin_fee, - mid_fee, - out_fee, - coin_0, - coin_1, - coin_2, - coin_3, - underlying_coin_0, - underlying_coin_1, - underlying_coin_2, - underlying_coin_3 + max(token) as token, + max(amplification_coefficient) as amplification_coefficient, + max(name) as name, + max(symbol) as symbol, + max(swap_fee) as swap_fee, + max(admin_fee) as admin_fee, + max(mid_fee) as mid_fee, + max(out_fee) as out_fee, + max(coin_0) as coin_0, + max(coin_1) as coin_1, + max(coin_2) as coin_2, + max(coin_3) as coin_3, + max(underlying_coin_0) as underlying_coin_0, + max(underlying_coin_1) as underlying_coin_1, + max(underlying_coin_2) as underlying_coin_2, + max(underlying_coin_3) as underlying_coin_3 from latest_entry +group by pool_address, chain diff --git a/models/staging/curve/fact_curve_avalanche_dex_swaps.sql b/models/staging/curve/fact_curve_avalanche_dex_swaps.sql index 6ead19c0..f77947b3 100644 --- a/models/staging/curve/fact_curve_avalanche_dex_swaps.sql +++ b/models/staging/curve/fact_curve_avalanche_dex_swaps.sql @@ -1,6 +1,6 @@ {{ config( - materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE_SM" + materialized="table", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE_SM" ) }} {{ fact_curve_dex_swaps("avalanche") }} diff --git a/models/staging/curve/fact_curve_ethereum_dex_swaps.sql b/models/staging/curve/fact_curve_ethereum_dex_swaps.sql index 54874a97..3298b6d3 100644 --- a/models/staging/curve/fact_curve_ethereum_dex_swaps.sql +++ b/models/staging/curve/fact_curve_ethereum_dex_swaps.sql @@ -1,6 +1,6 @@ {{ config( - materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE_SM" + materialized="table", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE_SM" ) }} {{ fact_curve_dex_swaps("ethereum") }} diff --git a/models/staging/curve/fact_curve_optimism_dex_swaps.sql b/models/staging/curve/fact_curve_optimism_dex_swaps.sql index 9be33a67..c592c066 100644 --- a/models/staging/curve/fact_curve_optimism_dex_swaps.sql +++ b/models/staging/curve/fact_curve_optimism_dex_swaps.sql @@ -1,6 +1,6 @@ {{ config( - materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE_SM" + materialized="table", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE_SM" ) }} {{ fact_curve_dex_swaps("optimism") }} diff --git a/models/staging/curve/fact_curve_polygon_dex_swaps.sql b/models/staging/curve/fact_curve_polygon_dex_swaps.sql index bf5f05a6..62d0990c 100644 --- a/models/staging/curve/fact_curve_polygon_dex_swaps.sql +++ b/models/staging/curve/fact_curve_polygon_dex_swaps.sql @@ -1,6 +1,6 @@ {{ config( - materialized="incremental", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE_SM" + materialized="table", unique_key=["tx_hash", "event_index"], snowflake_warehouse="CURVE_SM" ) }} {{ fact_curve_dex_swaps("polygon") }}