Updated 1 day ago
    --------------------------------------------------------------------------------
    -- 1. Extract MarginFi Borrow events for calculating Borrow Volume (total borrowed amount)
    -- Source: solana.core.fact_decoded_instructions
    -- Source table containing decoded instruction data from all Solana programs (preferred for clarity and ease of parsing)
    -- Note: This part takes a "user perspective", reflecting the actual asset outflow from MarginFi protocol
    --------------------------------------------------------------------------------
    WITH marginfi_borrows AS (
    SELECT
    DATE_TRUNC('hour',a.BLOCK_TIMESTAMP) as borrow_time, -- Borrow time, used for hourly price matching
    a.decoded_instruction:"args":"amount"::FLOAT AS raw_amount, -- Borrowed amount (not normalized by decimals)
    acc.value:"pubkey"::STRING AS account_address -- Token ATA from which the user receives the loan (destinationTokenAccount)
    FROM solana.core.fact_decoded_instructions a,
    LATERAL FLATTEN(input => a.decoded_instruction:"accounts") acc -- Flatten account list from decoded instruction
    WHERE a.program_id = 'MFv2hWf31Z9kbCa1snEPYctwafyhdvnV7FZnsebVacA' -- MarginFi V2 Program ID
    AND a.event_type = 'lendingAccountBorrow' -- Borrow event type
    AND acc.value:"name"::STRING = 'destinationTokenAccount' -- Account name must be destinationTokenAccount
    -- destinationTokenAccount reflects the user's receiving account, capturing actual outflow from the protocol
    ),
    --------------------------------------------------------------------------------
    -- 2. Map Token Account to Mint for identifying token types
    -- Source: solana.core.fact_token_balances
    -- This table maps each token account (SPL ATA) to its corresponding mint address
    -- Note: Token accounts (ATA) are linked to their mint addresses for metadata lookup
    --------------------------------------------------------------------------------
    token_info AS (
    SELECT distinct
    account_address, -- Token account (SPL ATA)
    mint -- Associated token mint (unique identifier)
    FROM solana.core.fact_token_balances tf
    WHERE EXISTS (
    SELECT 1
    FROM marginfi_borrows mb
    WHERE mb.account_address = tf.account_address
    )
    ),

    Last run: 1 day ago
    METRIC
    VALUE
    1
    borrow_volume_usd1358339909601.23
    1
    40B
    3s