Updated 2024-10-12
    WITH relevant_txs AS (
    SELECT DISTINCT tx_hash,
    left(input, 10) as function_sig
    FROM
    berachain.testnet.fact_traces
    WHERE to_address = LOWER('{{lpAddress}}')
    AND tx_succeeded = true
    AND function_sig NOT IN ('0x095ea7b3')
    AND block_timestamp BETWEEN '2024-09-19 4:59:59.000' AND '2024-09-19 5:59:59.000'
    ),

    token_transfers AS (
    SELECT
    block_number,
    block_timestamp,
    tx_hash,
    from_address,
    to_address,
    left(input, 10) as function_sig,
    CASE
    WHEN to_address = LOWER('{{lpAddress}}') THEN utils.udf_hex_to_int(regexp_substr_all(SUBSTR(input, 11), '.{64}')[4] :: string)
    WHEN regexp_substr_all(SUBSTR(input, 11), '.{64}')[2] IS NULL OR regexp_substr_all(SUBSTR(input, 11), '.{64}')[2] = '' THEN utils.udf_hex_to_int(regexp_substr_all(SUBSTR(input, 11), '.{64}')[1] :: string)
    ELSE utils.udf_hex_to_int(regexp_substr_all(SUBSTR(input, 11), '.{64}')[2] :: string)
    END as token_value
    FROM
    berachain.testnet.fact_traces
    WHERE
    tx_hash IN (SELECT tx_hash FROM relevant_txs)
    AND function_sig NOT IN ('0x095ea7b3')
    AND to_address IN (
    LOWER('{{token0}}'),
    LOWER('{{token1}}'),
    LOWER('{{lpAddress}}')
    )
    AND tx_succeeded = true
    ),
    QueryRunArchived: QueryRun has been archived