Sandeshnekodex swaps chains date today total
    Updated 2025-01-13
    -- forked from nekodex swaps chains date @ https://flipsidecrypto.xyz/studio/queries/de68b16e-a494-4698-8495-710a5fa6e5ed

    /*
    This SQL query analyzes cross-chain token swaps across multiple blockchain networks
    (Optimism, Ethereum, Base, and Arbitrum). It decodes event logs to extract swap details,
    combines data from different chains, and calculates trading volumes and user counts.
    */

    WITH decoded_swaps AS
    (
    -- Decode and combine swap data from multiple chains
    WITH optimism_swaps AS
    (
    -- Decode Optimism intra-chain swaps
    WITH intra_chain_swaps AS
    (
    SELECT
    tx_hash,
    block_timestamp,
    regexp_substr_all(SUBSTR(data, 3), '.{64}') AS decoded_data,
    concat('0x', substr(topics[1], -40)) AS swap_user,
    10 AS from_chain_id, -- Optimism chain ID
    10 AS to_chain_id, -- Optimism chain ID for intra-chain swaps
    concat('0x', substr(decoded_data[0], -40)) AS from_token,
    concat('0x', substr(decoded_data[2], -40)) AS from_token_value,
    utils.udf_hex_to_int(from_token_value) AS from_token_value_int,
    concat('0x', substr(decoded_data[1], -40)) AS to_token,
    concat('0x', substr(decoded_data[3], -40)) AS to_token_value,
    utils.udf_hex_to_int(to_token_value) AS to_token_value_int
    FROM optimism.core.fact_event_logs
    WHERE 1 = 1
    AND contract_address = '0x6cca65f0bc3140019a81a1619aa75a6925c2601e' -- Swap contract
    AND topics[0] = '0x900d96b4c36401fe685c5409d45d609db06c639ef658c6cfef098061d05aadc9' -- Swap event signature
    AND origin_from_address = '0xd19454648eedd9bee8d75142d503c5ee9179fc70' -- Specific swap initiator
    ),
    -- Decode Optimism inter-chain swaps
    QueryRunArchived: QueryRun has been archived