phi-deltalyticsGainsNetworkDaily
    Updated 2023-02-13
    with
    gains_arb_orders as (
    select
    --'arbitrum' as chain,
    date_trunc(day, block_timestamp) as date,
    tx_hash,
    --origin_from_address,
    origin_to_address,
    event_index,
    contract_address,
    case
    when topics[0] = '0x2739a12dffae5d66bd9e126a286078ed771840f2288f0afa5709ce38c3330997' then ['marketExecuted',1,6,7,8,12,14,'gains','arbitrum'] -- GNSTradingCallbacksV5_evt_MarketExecuted
    when topics[0] = '0x165b0f8d6347f7ebe92729625b03ace41aeea8fd7ebf640f89f2593ab0db63d1' then ['limitExecuted',2,7,8,9,13,15,'gains','arbitrum'] -- GNSTradingCallbacksV6_evt_MarketExecuted
    --when topics[0] = '0x2fe68525253654c21998f35787a8d0f361905ef647c854092430ab65f2f15022' then ['perp',2,0,0,0,0,6,'gmx','arbitrum']
    --when topics[0] = '0x165b0f8d6347f7ebe92729625b03ace41aeea8fd7ebf640f89f2593ab0db63d1' then ['perp',2,7,8,9,13,15,'gmx','arbitrum']
    end as order_info,
    order_info[7] as dexName,
    order_info[8] as chain,
    order_info[0] as orderType,
    substr(data, 3 + (order_info[1] - 1) * 64, 64) as trader,
    ethereum.public.udf_hex_to_int( substr(data, 3 + (order_info[2] - 1) * 64, 64) ) as openPrice,
    ethereum.public.udf_hex_to_int( substr(data, 3 + (order_info[3] - 1) * 64, 64) ) as long,
    ethereum.public.udf_hex_to_int( substr(data, 3 + (order_info[4] - 1) * 64, 64) ) as leverage,
    ethereum.public.udf_hex_to_int( substr(data, 3 + (order_info[5] - 1) * 64, 64) ) as price,
    ethereum.public.udf_hex_to_int( substr(data, 3 + (order_info[6] - 1) * 64, 64) ) / pow(10, 18) as positionSizeDai,
    case when long = 1 then leverage * (price / openPrice - 1)
    when long = 0 then leverage * (1 - price / openPrice) end
    as percentProfit,
    positionSizeDai * leverage as volume,
    volume * 0.08 / 100 as tradeFee,
    case when percentProfit = 0 then 0 else 1 end as closeTrade,
    case when percentProfit > 0 then 1 else 0 end as profitTrade,
    percentProfit * positionSizeDai as pnl
    from arbitrum.core.fact_event_logs
    Run a query to Download Data