diff --git a/macros/decentralized_exchanges/fact_solana_dex_swaps.sql b/macros/decentralized_exchanges/fact_solana_dex_swaps.sql new file mode 100644 index 00000000..ac296371 --- /dev/null +++ b/macros/decentralized_exchanges/fact_solana_dex_swaps.sql @@ -0,0 +1,30 @@ +{% macro fact_solana_dex_swaps(protocol) %} +With price as( + select + date + , price + , token_address + from {{ ref("fact_solana_dex_token_prices") }} +) + +select + block_timestamp + , swapper + , inserted_timestamp + , fact_swaps_id + , swap_from_mint + , swap_from_amount + , swap_to_mint + , swap_to_amount + , coalesce(swap_from_amount * from_price.price, 0) as swap_from_amount_usd + , coalesce(swap_to_amount * to_price.price, 0) as swap_to_amount_usd + from solana_flipside.defi.fact_swaps as swaps + left join price as from_price on from_price.date = date_trunc('day', swaps.block_timestamp) and swap_from_mint = from_price.token_address + left join price as to_price on to_price.date = date_trunc('day', swaps.block_timestamp) and swap_to_mint = to_price.token_address +where succeeded and lower(swap_program) like '%{{protocol}}%' +{% if is_incremental() %} + AND block_timestamp::date >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }}) +{% elif protocol == 'raydium' %} + AND block_timestamp::date >= date('2022-04-22') +{% endif %} +{% endmacro %} \ No newline at end of file diff --git a/macros/decentralized_exchanges/fact_solana_trading_metrics.sql b/macros/decentralized_exchanges/fact_solana_trading_metrics.sql new file mode 100644 index 00000000..bcd7dd34 --- /dev/null +++ b/macros/decentralized_exchanges/fact_solana_trading_metrics.sql @@ -0,0 +1,21 @@ +{% macro fact_solana_trading_metrics(protocol) %} + select + date_trunc('day', block_timestamp) as date + , sum(coalesce(swap_from_amount_usd, swap_to_amount_usd, 0)) as trading_volume + , count(distinct swapper) as unique_traders + , count(*) as number_of_swaps + from {{ ref("fact_"~protocol~"_trades") }} + where + (swap_from_amount_usd > 0 and swap_to_amount_usd > 0) + and abs( + ln(coalesce(nullif(swap_from_amount_usd, 0), 1)) / ln(10) + - ln(coalesce(nullif(swap_to_amount_usd, 0), 1)) / ln(10) + ) < 1 + {% if is_incremental() %} + AND block_timestamp::date >= (select dateadd('day', -3, max(date)) from {{ this }}) + {% else %} + AND block_timestamp::date >= date('2022-04-22') + {% endif %} + group by 1 + order by 1 desc +{% endmacro %} \ No newline at end of file diff --git a/models/projects/orca/core/ez_orca_metrics_by_chain.sql b/models/projects/orca/core/ez_orca_metrics_by_chain.sql new file mode 100644 index 00000000..add9998a --- /dev/null +++ b/models/projects/orca/core/ez_orca_metrics_by_chain.sql @@ -0,0 +1,13 @@ +{{ + config( + materialized="table", + snowflake_warehouse="ORCA", + database="orca", + schema="core", + alias="ez_metrics_by_chain", + ) +}} + +select date,'solana' as chain, 'orca' as protocol, trading_volume, unique_traders, number_of_swaps +from {{ ref("fact_orca_trading_metrics") }} +where date < to_date(sysdate()) diff --git a/models/staging/orca/fact_orca_trades.sql b/models/staging/orca/fact_orca_trades.sql new file mode 100644 index 00000000..7ee6fb43 --- /dev/null +++ b/models/staging/orca/fact_orca_trades.sql @@ -0,0 +1,8 @@ +{{ + config( + materialized="incremental", + unique_key="fact_swaps_id", + snowflake_warehouse="ORCA", + ) +}} +{{fact_solana_dex_swaps('orca')}} \ No newline at end of file diff --git a/models/staging/orca/fact_orca_trading_metrics.sql b/models/staging/orca/fact_orca_trading_metrics.sql new file mode 100644 index 00000000..1ba2761b --- /dev/null +++ b/models/staging/orca/fact_orca_trading_metrics.sql @@ -0,0 +1,9 @@ +{{ + config( + materialized="incremental", + unique_key="date", + snowflake_warehouse="ORCA", + ) +}} + +{{fact_solana_trading_metrics('orca')}} \ No newline at end of file diff --git a/models/staging/raydium/fact_raydium_trades.sql b/models/staging/raydium/fact_raydium_trades.sql index 98f33d3b..840ccb3d 100644 --- a/models/staging/raydium/fact_raydium_trades.sql +++ b/models/staging/raydium/fact_raydium_trades.sql @@ -5,32 +5,4 @@ snowflake_warehouse="RAYDIUM", ) }} - -With price as( - select - date - , price - , token_address - from {{ ref("fact_solana_dex_token_prices") }} -) - -select - block_timestamp - , swapper - , inserted_timestamp - , fact_swaps_id - , swap_from_mint - , swap_from_amount - , swap_to_mint - , swap_to_amount - , coalesce(swap_from_amount * from_price.price, 0) as swap_from_amount_usd - , coalesce(swap_to_amount * to_price.price, 0) as swap_to_amount_usd - from solana_flipside.defi.fact_swaps as swaps - left join price as from_price on from_price.date = date_trunc('day', swaps.block_timestamp) and swap_from_mint = from_price.token_address - left join price as to_price on to_price.date = date_trunc('day', swaps.block_timestamp) and swap_to_mint = to_price.token_address -where succeeded and lower(swap_program) like '%raydium%' -{% if is_incremental() %} - AND block_timestamp::date >= (select dateadd('day', -3, max(block_timestamp)) from {{ this }}) -{% else %} - AND block_timestamp::date >= date('2022-04-22') -{% endif %} +{{fact_solana_dex_swaps('raydium')}} \ No newline at end of file diff --git a/models/staging/raydium/fact_raydium_trading_volumes.sql b/models/staging/raydium/fact_raydium_trading_volumes.sql index aded9023..3f65cf4e 100644 --- a/models/staging/raydium/fact_raydium_trading_volumes.sql +++ b/models/staging/raydium/fact_raydium_trading_volumes.sql @@ -7,22 +7,4 @@ }} -select - date_trunc('day', block_timestamp) as date - , sum(coalesce(swap_from_amount_usd, swap_to_amount_usd, 0)) as trading_volume - , count(distinct swapper) as unique_traders - , count(*) as number_of_swaps -from {{ ref("fact_raydium_trades") }} -where -(swap_from_amount_usd > 0 and swap_to_amount_usd > 0) -and abs( - ln(coalesce(nullif(swap_from_amount_usd, 0), 1)) / ln(10) - - ln(coalesce(nullif(swap_to_amount_usd, 0), 1)) / ln(10) -) < 1 -{% if is_incremental() %} - AND block_timestamp::date >= (select dateadd('day', -3, max(date)) from {{ this }}) -{% else %} - AND block_timestamp::date >= date('2022-04-22') -{% endif %} -group by 1 -order by 1 desc +{{fact_solana_trading_metrics('raydium')}} \ No newline at end of file