Skip to content

Commit

Permalink
Add solana dex price hrs (#6080)
Browse files Browse the repository at this point in the history
* Add solana dex price hrs

* Update models/dex/solana/dex_solana_price_hour.sql

Co-authored-by: Huang Geyang <[email protected]>

* Use the variable

* Update models/dex/solana/dex_solana_price_hour.sql

Co-authored-by: Huang Geyang <[email protected]>

* Fix project_start_date

* Update models/dex/solana/dex_solana_schema.yml

---------

Co-authored-by: Huang Geyang <[email protected]>
  • Loading branch information
thanhlmm and Hosuke authored Jun 18, 2024
1 parent 7182639 commit 4848fc3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
70 changes: 70 additions & 0 deletions models/dex/solana/dex_solana_price_hour.sql
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions models/dex/solana/dex_solana_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 4848fc3

Please sign in to comment.