flyingfishtest real tx - ExecuteDecreasePosition
    Updated 2022-09-18
    WITH allgmx AS (
    SELECT *
    FROM arbitrum.core.fact_event_logs
    WHERE block_timestamp >= '2022-09-16'
    AND tx_hash = '0xcb92f572a14239d3a5a5b04bba3c42bc00d81eca6bd3fe05f5ecc9bb99d85388'
    AND origin_to_address = '0x8960d1b45a2d15d063b84b34dfb2fb2ca7535527'
    AND origin_function_signature = '0x574ec1be'
    --AND concat('0x', right(substr(data, 3 + 64 * 5, 64), 40)) = lower('0xef8E83c0383351EF644ffd6ab827C1162818adEE')
    AND topics[0] = '0x73af1d417d82c240fdb6d319b34ad884487c6bf2845d98980cc52ad9171cb455'
    /* AND topics[0] IN
    ('0x21435c5b618d77ff3657140cd3318e2cffaebc5e0e1b7318f56a9ba4044c3ed2', -- ExecuteDecreasePosition - Looks like no need for this event
    '0x73af1d417d82c240fdb6d319b34ad884487c6bf2845d98980cc52ad9171cb455', -- ClosePosition - To get realisedPnl and execution price
    '0x93d75d64d1f84fc6f430a64fc578bdd4c1e090e90ea2d51773e626d19de56d30') -- DecreasePosition - To get user address, collateral token, indexToken and fee */
    LIMIT 50
    ),
    /* decreasePosition AS (
    SELECT
    block_timestamp
    , tx_hash
    --, substr(data, 3, 64) as key
    , concat('0x', right(substr(data, 3 + 64 * 1, 64), 40)) as userAddress
    , concat('0x', right(substr(data, 3 + 64 * 2, 64), 40)) as collateralToken
    , concat('0x', right(substr(data, 3 + 64 * 3, 64), 40)) as indexToken
    --, ethereum.public.udf_hex_to_int(substr(data, 3 + 64 * 4, 64)) / pow(10,30) as collateralDelta
    --, ethereum.public.udf_hex_to_int(substr(data, 3 + 64 * 5, 64)) / pow(10,30) as sizeDelta
    , ethereum.public.udf_hex_to_int(substr(data, 3 + 64 * 6, 64)) as isLong
    , ethereum.public.udf_hex_to_int(substr(data, 3 + 64 * 7, 64)) / pow(10,30) as price -- Closing price
    , ethereum.public.udf_hex_to_int(substr(data, 3 + 64 * 8, 64)) / pow(10,30) as fee
    FROM allgmx
    WHERE topics[0] = '0x93d75d64d1f84fc6f430a64fc578bdd4c1e090e90ea2d51773e626d19de56d30'
    LIMIT 1
    ), */
    closePosition AS (
    SELECT
    block_timestamp
    , tx_hash
    Run a query to Download Data