AntonidasGHO Debt Migration - Version2 - GHO<>USDC
    Updated 2025-01-19
    -- forked from GHO Debt Migration @ https://flipsidecrypto.xyz/studio/queries/783870d3-549a-4579-871f-c756e877e9fa

    with token_table as (
    /*select '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' as token_address, 'USDC' as token_name, 6 as decimals
    union all select '0x00000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f' as token_address, 'GHO' as token_name, 18 as decimals
    union all select '0x000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7' as token_address, 'USDT' as token_name, 6 as decimals
    union all*/ select '0x786dbff3f1292ae8f92ea68cf93c30b34b1ed04b' as token_address, 'variableDebtEthGHO' as token_name, 18 as decimals
    --union all select '0x6df1c1e379bc5a00a7b4c6e67a203333772f45a8' as token_address, 'variableDebtEthUSDT' as token_name, 6 as decimals
    union all select '0x72e95b8931767c79ba4eee721354d6e99a61d004' as token_address, 'variableDebtEthUSDC' as token_name, 6 as decimals
    ), raws as (
    -- look for all swaps that went through paraswap v6
    select
    block_timestamp
    , topics[1] as token_from
    , topics[2] as token_to
    , regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') as amounts
    , tx_hash
    from ethereum.core.fact_event_logs
    where contract_address = '0xd7852e139a7097e119623de0751ae53a61efb442'
    and block_timestamp > '2024-12-01'
    and topics[0] = '0xbf77fd13a39d14dc0da779342c14105c38d9a5d0c60f2caa22f5fd1d5525416d' -- bought
    /*
    and (
    (topics[1] = '0x00000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f' and topics[2] in (select token_address from token_table))
    or (topics[2] = '0x00000000000000000000000040d16fc0246ad3160ccc09b8d0d3a2cd28ae6c2f' and topics[1] in (select token_address from token_table))
    )
    */
    )
    , clean_txs as (
    select
    r.block_timestamp
    , r.tx_hash
    , t_from.token_name as token_name_from
    , t_to.token_name as token_name_to
    , ethereum.public.udf_hex_to_int(r.amounts[0]) / pow(10, t_from.decimals) as amount_from
    , ethereum.public.udf_hex_to_int(r.amounts[1]) / pow(10, t_to.decimals) as amount_to
    QueryRunArchived: QueryRun has been archived