Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orca: Adding Trading Metrics #645

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
{% 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 %}
Comment on lines +27 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SebMelendez01 Should we also have a case for orca here too?

{% 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 %}
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as for the other macro, should we add a protocol-specific block/comment?

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_dex_swaps('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')}}