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

Jupiter pricing data #570

Merged
merged 6 commits into from
Oct 24, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{
config(
materialized="table",
snowflake_warehouse="JUPITER",
database="jupiter",
schema="raw",
alias="ez_perps_price_performance",
)
}}

with agg as (
SELECT
date_trunc('hour', agg.block_timestamp) as hour,
agg.price,
agg.mint,
m.symbol,
CASE
when date_part('DOW', convert_timezone('UTC', 'America/New_York', block_timestamp)) IN (0, 6) then 'FALSE'
when convert_timezone('UTC', 'America/New_York', block_timestamp)::time between '09:00:00' and '15:59:59' then 'TRUE'
else 'FALSE'
END AS nyc_operating_hours
FROM {{ ref('fact_jupiter_perps_txs') }} agg
LEFT JOIN solana_flipside.price.ez_asset_metadata m ON m.token_address = agg.mint
)

SELECT
hour
, symbol
, mint
, max(price) as high
, min(price) as low
, avg(price) as average
, median(price) as median
, nyc_operating_hours
FROM agg
GROUP BY hour, symbol, mint, nyc_operating_hours
20 changes: 20 additions & 0 deletions models/projects/jupiter/raw/fact_jupiter_perps_token_prices.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{
config(
materialized="table",
snowflake_warehouse="JUPITER",
database="jupiter",
schema="raw",
alias="fact_perps_token_prices",
)
}}

SELECT
block_timestamp,
tx_id,
chain,
app,
mint,
price,
size_usd,
fee_usd
FROM {{ ref('fact_jupiter_perps_txs') }}
18 changes: 18 additions & 0 deletions models/staging/jupiter/fact_jupiter_perps_silver.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{
config(
materialized="table",
unique_key="date",
snowflake_warehouse="JUPITER",
)
}}


SELECT
block_timestamp::date as date
, sum(size_usd) as volume
, sum(fee_usd) as fees
, count(distinct owner) as traders
, count(distinct tx_id) as txns
FROM {{ ref('fact_jupiter_perps_txs') }}
GROUP BY 1
ORDER BY 1 DESC
Original file line number Diff line number Diff line change
@@ -1,71 +1,102 @@
{{
config(
materialized="incremental",
unique_key="date",
snowflake_warehouse="JUPITER",
materialized='incremental',
unique_key=['tx_id'],
snowflake_warehouse='JUPITER',
)
}}


with hex_cte as (
SELECT
date(block_timestamp) as date,
block_timestamp,
tx_id,
PC_DBT_DB.PROD.BASE58_TO_HEX(f.value:data) as hex_data,
f.value:data as base58_data,
tx_id
f.value:data as base58_data
FROM solana_flipside.core.fact_events,
LATERAL FLATTEN(input => get_path(inner_instruction, 'instructions')) AS f
where
{% if is_incremental() %}
-- this filter will only be applied on an incremental run
block_timestamp::date >= (select dateadd('day', -7, max(date)) from {{ this }})
block_timestamp >= (select dateadd('day', -7, max(block_timestamp)) from {{ this }})
{% else %}
block_timestamp::date > '2023-07-17'
block_timestamp > date('2023-07-17')
{% endif %}
and program_id = 'PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu'
and f.value:data is not null
and succeeded = 1
),
perps_mat as(
)
, agg as (
SELECT
date,
block_timestamp,
tx_id,
'inc' as type,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,582+1,16))/1e6 as size_usd, -- position size fee
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,680+1,16))/1e6 as fees_usd, --open fee
PC_DBT_DB.PROD.HEX_TO_BASE58(SUBSTRING(hex_data,454+1,64)) as owner, -- owner/trader address
tx_id
PC_DBT_DB.PROD.HEX_TO_BASE58(SUBSTRING(hex_data,243,64)) as mint,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,630+1,16))/1e6 as price,
hex_data
FROM hex_cte
WHERE SUBSTRING(hex_data,17,16) = 'f5715534d6bb9984' -- IncreasePosition

UNION ALL

SELECT
date,
block_timestamp,
tx_id,
'dec' as type,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data, 584+1,16))/1e6 as size_usd, --open fee
case when substring(hex_data,1+651,1) = 1
then PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,1+668,16)) / 1e6
else PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,1+652,16)) / 1e6
end as fee_usd, --close fee, has an optional param priceSlippage before it so we need this case when.
PC_DBT_DB.PROD.HEX_TO_BASE58(SUBSTRING(hex_data,456+1,64)) as owner, -- owner/trader address
tx_id
PC_DBT_DB.PROD.HEX_TO_BASE58(SUBSTRING(hex_data,243,64)) as mint,
-- Price calculation with dynamic offset
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(
SUBSTRING(
hex_data,
619 -- Base offset to price
+ (CASE WHEN SUBSTRING(hex_data, 618, 1) = '1'
THEN 16
ELSE 0
END) -- Add offset if transferToken present
, 16
)
)/1e6 as price,
hex_data
FROM hex_cte
WHERE substring(hex_data,1+16,16) = '409c2b4a6d83107f' -- DecreasePosition

UNION ALL


SELECT
date,
block_timestamp,
tx_id,
'liq' as type,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,1+354,16)) / 1e6 as size_usd, --close fee
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,1+564,16)) / 1e6 as fee_usd, --close fee
PC_DBT_DB.PROD.HEX_TO_BASE58(SUBSTRING(hex_data,1+388,64)) as owner, -- owner/trader address
tx_id
PC_DBT_DB.PROD.HEX_TO_BASE58(SUBSTRING(hex_data,291,64)) as mint,
case when block_timestamp >= date('2023-10-16 07:33:16.000')
THEN PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,549,16))/1e6
ELSE
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data,531,16))/1e6
END as price,
hex_data
FROM hex_cte
WHERE substring(hex_data,1+16,16) IN ('68452084d423bf2f', '806547a880485654') --LiquidatePosition, LiquidateFullPosition
)
SELECT
date
, sum(size_usd) as volume
, sum(fees_usd) as fees
, count(distinct owner) as traders
, count(distinct tx_id) as txns
FROM perps_mat group by 1 order by 1 desc
block_timestamp,
tx_id,
'solana' as chain,
'jupiter' as app,
size_usd,
fees_usd as fee_usd,
owner,
mint,
price,
hex_data
FROM agg
Loading