Skip to content

Commit

Permalink
adding orca
Browse files Browse the repository at this point in the history
  • Loading branch information
SebMelendez01 committed Nov 26, 2024
1 parent 7c2b4b3 commit 5e2423f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 48 deletions.
30 changes: 30 additions & 0 deletions macros/decentralized_exchanges/fact_solana_dex_swaps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% 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 %}
21 changes: 21 additions & 0 deletions macros/decentralized_exchanges/fact_solana_trading_metrics.sql
Original file line number Diff line number Diff line change
@@ -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 %}
13 changes: 13 additions & 0 deletions models/projects/orca/core/ez_orca_metrics_by_chain.sql
Original file line number Diff line number Diff line change
@@ -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())
8 changes: 8 additions & 0 deletions models/staging/orca/fact_orca_trades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{
config(
materialized="incremental",
unique_key="fact_swaps_id",
snowflake_warehouse="ORCA",
)
}}
{{fact_solana_dex_swaps('orca')}}
9 changes: 9 additions & 0 deletions models/staging/orca/fact_orca_trading_metrics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{
config(
materialized="incremental",
unique_key="date",
snowflake_warehouse="ORCA",
)
}}

{{fact_solana_trading_metrics('orca')}}
30 changes: 1 addition & 29 deletions models/staging/raydium/fact_raydium_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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_trading_metrics('raydium')}}
20 changes: 1 addition & 19 deletions models/staging/raydium/fact_raydium_trading_volumes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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')}}

0 comments on commit 5e2423f

Please sign in to comment.