-- -- 两个方向
-- -- 1.获取某一个token在某一个时间段的swap记录 ，和并分析 获取 token理论持有 买卖 token，金额，次数，价格 等 ，获取某一个方面的top
-- -- 2.指定一些trader 获取他们的swap记录并汇总
-- -- 为了节省private 空间 公用一个。
 

-- 第一个方向代码 
-- 开始 开始 开始 开始 开始 开始 开始 开始 开始
with swap_table as (
        select
            block_time,
            tx_id,
            token_bought_amount,
            token_sold_amount,
            token_bought_mint_address,
            token_sold_mint_address,
            trader_id
        from
            dex_solana.trades
        where
      
            block_time > timestamp '2025-03-17 15:30:00 +08:00'
            and
            block_time < timestamp '2025-03-17 16:00:00 +08:00'
               -- block_time < timestamp '2025-03-17 08:00:00 +08:00'
            
            and (
            (
                    token_bought_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump'
                    and token_sold_mint_address = 'So11111111111111111111111111111111111111112'
                )
                or (
                    token_bought_mint_address = 'So11111111111111111111111111111111111111112'
                    and token_sold_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump'
                )
            )
            
    ),
    swap_table_02 as (
        select
            trader_id,
            COALESCE(
                SUM(
                    CASE
                        WHEN token_bought_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN token_bought_amount
                        ELSE 0
                    END
                ),
                0
            ) AS token_bought,
            COALESCE(
                SUM(
                    CASE
                        WHEN token_bought_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN token_sold_amount
                        ELSE 0
                    END
                ),
                0
            ) AS eth_bought,
            COALESCE(
                SUM(
                    CASE
                        WHEN token_bought_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN 1
                        ELSE 0
                    END
                ),
                0
            ) AS bought_count,
            COALESCE(
                SUM(
                    CASE
                        WHEN token_sold_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN token_sold_amount
                        ELSE 0
                    END
                ),
                0
            ) AS token_sold,
            COALESCE(
                SUM(
                    CASE
                        WHEN token_sold_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN token_bought_amount
                        ELSE 0
                    END
                ),
                0
            ) AS eth_sold,
            COALESCE(
                SUM(
                    CASE
                        WHEN token_sold_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN 1
                        ELSE 0
                    END
                ),
                0
            ) AS sold_count
        from
            swap_table
        group by
            trader_id
    )
select
    trader_id as swap_trader_id,
    round( (token_bought - token_sold) / 1e6, 2) as li_lun_hold,
    round(token_bought / 1e6, 2) as token_b_M,
    round(token_sold / 1e6, 2) as token_s_M,
    round(eth_bought, 1) as eth_b,
    round(eth_sold, 1) as eth_s,
    bought_count as b_count,
    sold_count as s_count
    ,(
        CASE
            WHEN token_bought != 0 THEN round(eth_bought / token_bought*1e3*1e6, 1)
            ELSE 0
        END
    ) as avg_b_mc
    , (
        CASE
            WHEN token_sold != 0 THEN round(eth_sold / token_sold*1e3*1e6,1 )
            ELSE 0
        END
    ) as avg_s_mc
from
    swap_table_02
    where 
      -- abs(token_bought - token_sold) > 0.1*1e6 and
    -- (token_bought - token_sold) < 0.1*1e6 and
    -- (token_bought - token_sold) >0  and
     -- token_bought > 0.5 * 1e6 and 
    -- eth_bought >1.9
     eth_bought >1.5
   
    
     -- ( eth_bought > 1.9 or       eth_sold >1.9)
     
order by
    --  2 diff amount
    eth_b
    --  3 buy amount
    -- 3
    -- 7 buy count
    -- 7
    -- 8 sell count
    -- 8
    desc
limit
    75

-- 第一个方向代码 
-- 结束 结束 结束 结束 结束 结束 结束 结束 结束



-- -- -- 第二个方向代码 
-- -- -- 开始 开始 开始 开始 开始 开始 开始 开始 开始
-- with
--     input_address (address) as (
--         VALUES
-- ('5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1'),
-- ('Cet3TXGhtFcBo6vX9kfYjbtNyANbCWqwVC6G1QioKUhc'),
-- ('7m2XQP2NDebaEKDhegj7WYpZBmrKF5yhiVi1JZU8cwoj'),
-- ('8Mnb87uUYXcqejP6RuUyDiX9Fd1unCA3YdM3zLGSVEPQ'),
-- ('CfpFiySF5c63fvgtjjgbefjS2JSATJYeqp8qFQX76hKT'),
-- ('CdCM27tboWquCEC3Pq3CRphSdiGV6Q48VewWmrxbUyYW')

--     ),
--     swap_table as (
--         select
--             block_time,
--             tx_id,
--             token_bought_amount,
--             token_sold_amount,
--             token_bought_mint_address,
--             token_sold_mint_address,
--             trader_id
--         from
--             dex_solana.trades
--         where
--             block_time > timestamp '2025-01-06 21:00:00 +08:00'
--             and block_time < timestamp '2025-01-15 02:00:00 +08:00'
--             and (
--                 (
--                     token_bought_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump'
--                     and token_sold_mint_address = 'So11111111111111111111111111111111111111112'
--                 )
--                 or (
--                     token_bought_mint_address = 'So11111111111111111111111111111111111111112'
--                     and token_sold_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump'
--                 )
--             )
--     ),
--     swap_table_02 as (
--         select
--             trader_id,
--             COALESCE(
--                 SUM(
--                     CASE
--                         WHEN token_bought_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN token_bought_amount
--                         ELSE 0
--                     END
--                 ),
--                 0
--             ) AS token_bought,
--             COALESCE(
--                 SUM(
--                     CASE
--                         WHEN token_bought_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN token_sold_amount
--                         ELSE 0
--                     END
--                 ),
--                 0
--             ) AS eth_bought,
--             COALESCE(
--                 SUM(
--                     CASE
--                         WHEN token_bought_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN 1
--                         ELSE 0
--                     END
--                 ),
--                 0
--             ) AS bought_count,
--             COALESCE(
--                 SUM(
--                     CASE
--                         WHEN token_sold_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN token_sold_amount
--                         ELSE 0
--                     END
--                 ),
--                 0
--             ) AS token_sold,
--             COALESCE(
--                 SUM(
--                     CASE
--                         WHEN token_sold_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN token_bought_amount
--                         ELSE 0
--                     END
--                 ),
--                 0
--             ) AS eth_sold,
--             COALESCE(
--                 SUM(
--                     CASE
--                         WHEN token_sold_mint_address = '2ZoJVM15fbbDgo6s5cTTX2Sj8V4sJ47rimb7gQhGpump' THEN 1
--                         ELSE 0
--                     END
--                 ),
--                 0
--             ) AS sold_count
--         from
--             swap_table
--             inner join input_address on input_address.address = swap_table.trader_id
--         group by
--             trader_id
--     )
-- select
--     trader_id as swap_trader_id,
--     round((token_bought - token_sold) / 1e6, 2) as li_lun_hold,
--     round(token_bought / 1e6, 2) as token_b_M,
--     round(token_sold / 1e6, 2) as token_s_M,
--     round(eth_bought, 2) as eth_b,
--     round(eth_sold, 2) as eth_s,
--     bought_count as b_count,
--     sold_count as s_count,
--     (
--         CASE
--             WHEN token_bought != 0 THEN round(eth_bought / token_bought * 1e6, 3)
--             ELSE 0
--         END
--     ) as avg_b_pri,
--     (
--         CASE
--             WHEN token_sold != 0 THEN round(eth_sold / token_sold * 1e6, 3)
--             ELSE 0
--         END
--     ) as avg_s_pri
-- from
--     swap_table_02
-- order by
--     --  2 diff amount
--     2
--     --  3 buy amount
--     -- 3
--     -- 7 buy count
--     -- 7
--     -- 8 sell count
--     -- 8
--     desc