Updated 2025-04-30
    WITH swap_transactions AS (
    SELECT DISTINCT
    s.tx_hash,
    s.block_timestamp,
    t.transaction_fee / 1e24 AS transaction_fee_near,
    s.platform
    FROM
    near.defi.ez_dex_swaps s
    JOIN near.core.fact_transactions t ON s.tx_hash = t.tx_hash
    ),
    daily_near_prices AS (
    SELECT
    DATE_TRUNC('day', hour) AS day,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY price) AS median_near_price_usd
    FROM
    near.price.ez_prices_hourly
    WHERE
    symbol = 'NEAR'
    GROUP BY
    DATE_TRUNC('day', hour)
    )
    SELECT
    COUNT(DISTINCT s.tx_hash) AS total_transactions,
    SUM(s.transaction_fee_near) AS total_fee_near,
    SUM(s.transaction_fee_near * p.median_near_price_usd) AS total_fee_usd,
    CASE
    WHEN COUNT(DISTINCT s.tx_hash) > 0 THEN
    SUM(s.transaction_fee_near * p.median_near_price_usd) / COUNT(DISTINCT s.tx_hash)
    ELSE
    NULL
    END AS avg_gas_per_swap_near
    FROM
    swap_transactions s
    JOIN daily_near_prices p
    ON DATE_TRUNC('day', s.block_timestamp) = p.day
    WHERE
    Last run: about 1 month ago
    TOTAL_TRANSACTIONS
    TOTAL_FEE_NEAR
    TOTAL_FEE_USD
    AVG_GAS_PER_SWAP_NEAR
    1
    2150401056839.886683296138.4428221560.01377131255
    1
    56B
    388s