Skip to content

Commit

Permalink
fix edge hour issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwes committed Oct 24, 2024
1 parent f06bda4 commit 244d9c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ with agg as (
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 '17:00:00' then 'TRUE'
when convert_timezone('UTC', 'America/New_York', block_timestamp)::time between '09:00:00' and '16:59:59' then 'TRUE'
else 'FALSE'
END AS nyc_operating_hours
FROM {{ ref('fact_jupiter_perps_txs') }} agg
Expand Down
62 changes: 19 additions & 43 deletions models/staging/jupiter/fact_jupiter_perps_txs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,13 @@ with hex_cte as (
and f.value:data is not null
and succeeded = 1
)
, position_events AS (
SELECT
block_timestamp,
tx_id,
hex_data,
-- First check if transferToken is present (optional parameter)
CASE
WHEN SUBSTRING(hex_data, 618, 1) = '1' THEN 16
ELSE 0
END as transfer_token_offset,
-- Check if priceSlippage is present (optional parameter)
CASE
WHEN SUBSTRING(hex_data,
651 + -- Base offset to priceSlippage flag
CASE WHEN SUBSTRING(hex_data, 601, 1) = '1' THEN 16 ELSE 0 END, -- Add transfer_token offset if present
1) = '1' THEN 16
ELSE 0
END as price_slippage_offset
FROM hex_cte
WHERE SUBSTRING(hex_data, 17, 16) = '409c2b4a6d83107f' -- DecreasePosition event identifier
)
, agg as (
SELECT
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 fee_usd, --open 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
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,
Expand All @@ -65,31 +44,28 @@ with hex_cte as (
block_timestamp,
tx_id,
'dec' as type,
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(SUBSTRING(hex_data, 585, 16))/1e6 as size_usd,
-- Fee calculation with dynamic offset
PC_DBT_DB.PROD.BIG_ENDIAN_HEX_TO_DECIMAL(
SUBSTRING(
hex_data,
667 + -- Base offset to fee
transfer_token_offset + -- Add offset if transferToken present
price_slippage_offset, -- Add offset if priceSlippage present
16
)
)/1e6 as fee_usd,
PC_DBT_DB.PROD.HEX_TO_BASE58(SUBSTRING(hex_data, 457, 64)) as owner,
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
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
transfer_token_offset, -- Add offset if transferToken present
16
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,
)/1e6 as price,
hex_data
FROM position_events

FROM hex_cte
WHERE substring(hex_data,1+16,16) = '409c2b4a6d83107f' -- DecreasePosition

UNION ALL

Expand All @@ -102,12 +78,12 @@ with hex_cte as (
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
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')
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
hex_data
FROM hex_cte
WHERE substring(hex_data,1+16,16) IN ('68452084d423bf2f', '806547a880485654') --LiquidatePosition, LiquidateFullPosition
)
Expand All @@ -117,7 +93,7 @@ SELECT
'solana' as chain,
'jupiter' as app,
size_usd,
fee_usd,
fees_usd as fee_usd,
owner,
mint,
price,
Expand Down

0 comments on commit 244d9c6

Please sign in to comment.