Updated 2024-09-16
    with account_connection as (
    select distinct * from
    (
    select
    DECODED_LOG['primaryAccount'] as account,
    DECODED_LOG['subAccount'] as subAccount,
    from blast.core.fact_decoded_event_logs
    where CONTRACT_ADDRESS = lower('0x0a8D9c0A4a039dDe3Cb825fF4c2f063f8B54313A') -- tradeService
    and EVENT_NAME = 'LogIncreasePosition'
    )
    ),
    trade_logs as (
    select
    BLOCK_TIMESTAMP,
    DECODED_LOG['subAccount'] as subAccount,
    DECODED_LOG['marketIndex'] as marketIndex,
    case
    when EVENT_NAME = 'LogReceivedUnRealizedPnlValue' then DECODED_LOG['usd']
    when EVENT_NAME = 'LogSettleUnRealizedPnlValue' then -DECODED_LOG['usd']
    when EVENT_NAME = 'LogSettleTradingFeeAmount' then -DECODED_LOG['feeUsd']
    when EVENT_NAME = 'LogSettleBorrowingFeeValue' then -DECODED_LOG['feeUsd']
    when EVENT_NAME = 'LogSettleLiquidationFeeValue' then -DECODED_LOG['usd']
    end as usd,
    case
    when EVENT_NAME = 'LogReceivedUnRealizedPnlValue' then 'pnl'
    when EVENT_NAME = 'LogSettleUnRealizedPnlValue' then 'pnl'
    when EVENT_NAME = 'LogSettleTradingFeeAmount' then 'fees'
    when EVENT_NAME = 'LogSettleBorrowingFeeValue' then 'fees'
    when EVENT_NAME = 'LogSettleLiquidationFeeValue' then 'fees'
    end as type
    from blast.core.fact_decoded_event_logs
    where CONTRACT_ADDRESS = lower('0x9F1f13eBC178122C3ef6c14FA3A523680563F58b')
    and EVENT_NAME in (
    'LogReceivedUnRealizedPnlValue',
    'LogSettleUnRealizedPnlValue',
    'LogSettleTradingFeeAmount',
    QueryRunArchived: QueryRun has been archived