From 4848fc383f0b98c7a5f8eebd949bd42f85c3a7d5 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Tue, 18 Jun 2024 20:39:59 +0700 Subject: [PATCH] Add solana dex price hrs (#6080) * Add solana dex price hrs * Update models/dex/solana/dex_solana_price_hour.sql Co-authored-by: Huang Geyang * Use the variable * Update models/dex/solana/dex_solana_price_hour.sql Co-authored-by: Huang Geyang * Fix project_start_date * Update models/dex/solana/dex_solana_schema.yml --------- Co-authored-by: Huang Geyang --- models/dex/solana/dex_solana_price_hour.sql | 70 +++++++++++++++++++++ models/dex/solana/dex_solana_schema.yml | 30 +++++++++ 2 files changed, 100 insertions(+) create mode 100644 models/dex/solana/dex_solana_price_hour.sql diff --git a/models/dex/solana/dex_solana_price_hour.sql b/models/dex/solana/dex_solana_price_hour.sql new file mode 100644 index 00000000000..c30da89c0c1 --- /dev/null +++ b/models/dex/solana/dex_solana_price_hour.sql @@ -0,0 +1,70 @@ +{{ + config( + schema = 'dex_solana', + alias = 'price_hour', + partition_by = ['block_month'], + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.hour')], + unique_key = ['blockchain', 'contract_address', 'hour'], + pre_hook='{{ enforce_join_distribution("PARTITIONED") }}', + post_hook='{{ expose_spells(\'["solana"]\', + "project", + "dex", + \'["get_nimbus"]\') }}') +}} + +{% set project_start_date = '2022-03-10' %} --grabbed min block time from whirlpool_solana.whirlpool_call_swap +with + raw_data as ( + SELECT + * + FROM + {{ ref('dex_solana_trades') }} + WHERE 1 = 1 + {% if is_incremental() %} + AND {{ incremental_predicate('block_time') }} + {% else %} + AND block_time >= DATE '{{project_start_date}}' + {% endif %} + ), + bought_price as ( + SELECT + token_bought_mint_address as token_mint, + DATE_TRUNC('hour', block_time) AS hour, + SUM(amount_usd) / SUM(token_bought_amount) AS price + FROM raw_data + GROUP BY + 1, + 2 + ), + sold_price as ( + SELECT token_sold_mint_address as token_mint, + DATE_TRUNC('hour', block_time) AS hour, + SUM(amount_usd) / SUM(token_sold_amount) AS price + FROM raw_data + GROUP BY 1, + 2 + ), + all_trades as ( + SELECT * + FROM bought_price + UNION ALL + SELECT * + FROM sold_price + ) +SELECT t1.token_mint as contract_address, + t1.hour as hour, + t2.symbol, + t2.decimals, + 'solana' as blockchain, + avg(t1.price) as price, + CAST(DATE_TRUNC('month', t1.hour) as date) as block_month +FROM all_trades t1 + JOIN + {{ ref('tokens_solana_fungible') }} t2 ON t1.token_mint = t2.token_mint_address +GROUP BY 1, + 2, + 3, + 4 \ No newline at end of file diff --git a/models/dex/solana/dex_solana_schema.yml b/models/dex/solana/dex_solana_schema.yml index 9bbcabd2184..d34e0d9a1e4 100644 --- a/models/dex/solana/dex_solana_schema.yml +++ b/models/dex/solana/dex_solana_schema.yml @@ -92,3 +92,33 @@ models: - &tx_index name: tx_index description: "index of the transaction in the block slot" + + - name: dex_solana_price_hour + meta: + blockchain: solana + contributors: [get_nimbus] + config: + tags: ['solana','dex', 'price'] + description: > + All token price in Orca pool + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - blockchain + - contract_address + - hour + columns: + - name: blockchain + description: "Blockchain which the DEX is deployed" + - name: contract_address + description: "contract_address of token" + - name: hour + description: "Time of price in hour" + - name: block_month + description: "UTC event block month of each DEX trade" + - name: symbol + description: "Token symbol" + - name: decimals + description: "Token decimals" + - name: price + description: "Token price" \ No newline at end of file