Updated 2025-04-12
    WITH open_trove AS (
    SELECT
    '0x' || SUBSTR(topics[1], 27) AS borrower_address,
    ethereum.public.udf_hex_to_int(SUBSTRING(data, 3, 64))::NUMERIC / 1e18 AS open_principal,
    ethereum.public.udf_hex_to_int(SUBSTRING(data, 131, 64))::NUMERIC / 1e18 AS open_collateral,
    tx_hash,
    block_timestamp
    FROM
    mezo.testnet.fact_event_logs
    WHERE
    topics[0] = '0xf575eb5cdee005607f56587351e18943ddacd11756b9d37980ec251797ff136c'
    AND origin_function_signature = '0x8f09162b'
    AND contract_address = '0x20faea18b6a1d0fcdbccfffe3d164314744baf30'
    ),

    adjust_trove AS (
    SELECT
    '0x' || SUBSTR(topics[1], 27) AS borrower_address,
    ethereum.public.udf_hex_to_int(SUBSTRING(data, 3, 64))::NUMERIC / 1e18 AS adjust_principal,
    ethereum.public.udf_hex_to_int(SUBSTRING(data, 131, 64))::NUMERIC / 1e18 AS adjust_collateral,
    tx_hash,
    block_timestamp
    FROM
    mezo.testnet.fact_event_logs
    WHERE
    topics[0] = '0xf575eb5cdee005607f56587351e18943ddacd11756b9d37980ec251797ff136c'
    AND origin_function_signature = '0x8e54c119'
    AND contract_address = '0x20faea18b6a1d0fcdbccfffe3d164314744baf30'
    ),

    combined_all_time AS (
    SELECT
    o.borrower_address,
    COALESCE(SUM(o.open_collateral), 0) + COALESCE(SUM(a.adjust_collateral), 0) AS total_collateral_per_user,
    COALESCE(SUM(o.open_principal), 0) + COALESCE(SUM(a.adjust_principal), 0) AS total_borrowed_per_user,
    COUNT(DISTINCT o.tx_hash) + COUNT(DISTINCT a.tx_hash) AS total_transactions_per_user
    Last run: 14 days ago
    AVG_COLLATERAL_PER_USER
    AVG_BORROWED_PER_USER
    AVG_TRANSACTIONS_PER_USER
    1
    0.00201894167233115.310873783531.154086089
    1
    48B
    15s