-- 通过token过去的transfer记录， 记录wallet 的 token amount ，从而获取top 100 的 holder

with
    input_address (address) as (
        values
('6xMPaDn8jZ1r68BzXAunvumU8BTG2vDbgemNkWwRd1Fo')
-- ('5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1'),
-- ('Cet3TXGhtFcBo6vX9kfYjbtNyANbCWqwVC6G1QioKUhc'),
-- ('7m2XQP2NDebaEKDhegj7WYpZBmrKF5yhiVi1JZU8cwoj'),
-- ('68WLD7YzwjqBoCEVuJ3rjVbaPaAog6ATCkjUTpNqiMNF'),
-- ('4ttYJgVcZMMXFsTPBAEKVUEDUshvsXqgvfUv3W7bdv7e'),
-- ('14qLxu9XDQtc2pgnxX2SSdWPpnuVrEqccgwmATH1khcY'),
-- ('Hh6XswyZK9WPUbb6csqi7YggDqYTstKkHFvvh7PFx7kw'),
-- ('DAr4CE2rFgvtDHSFG75rEhmxmqgfEQYir91RGY458pkZ'),
-- ('CC1xmUJCvq4wPzHSNKVHfMV9T3vRjHTZnzvW9VLVJ4gs'),
-- ('GDwiC6S14topnYmWquQb6JdQxrRsvtH7N3K8UCLZmVrk'),
-- ('HjUrUJi7zUCXu6NpXBUujYsoG5TJsXkPUrYJdWMKVtts'),
-- ('5PZs2fmTJpjaMhYbdAH6uEx7Jbbhy2RdAdkNnPw83H3a'),
-- ('FH8xnCwgoQgk3nXuePEdxYB5SZDTWSZav27smVcHjN1q'),
-- ('ET3VpuSMr15ufn4z9a5kwHhyLRse2H96TY2HXVKZxCJM'),
-- ('GD9Q87EY9guqAqkxgNwiqoqUKmBs958tcFEkQ8q1HD1N'),
-- ('5pW6xYQZxZLs67eKuurGcTVm2nHqFcDsiuxesqagoW6V'),
-- ('7V7BFFLNgHhrJq7Udm9Zi26Tbgvytg9GWKLdDLRzf4z6'),
-- ('BC8yiFFQWFEKrEEj75zYsuK3ZDCfv6QEeMRif9oZZ9TW'),
-- ('6QfdT1XZauUMCJU3SaN2d2aqdoFa4TDVNmxJig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CmxVys1vK3dB6t'),
-- ('Gy5GSoHmrfnv49TZHku1bxNv2Dy4tFDjgm7vGbmp8S2h'),
-- ('9UPcMHdTw8pJSvv13dH1tGawwCjSCSjjxwU5cKu9PTiq'),
-- ('2k77ScTf9ULptpVRG4M4X6SRZiuUxWCTzG2RWBT7KnHR'),
-- ('9wNu1SLCfouEsR4B8PCck6p15mcSgun7k5KEKDw43Cyu'),
-- ('3nxNfz8fx4DPW37i97Qs4Ue4Z2zVinEFeDAAymwtEtYd'),
-- ('2ydwYpyTwn1NrPpRN4n6NaHHtg82G65QAN2rQxX6e9ep'),
-- ('8Mnb87uUYXcqejP6RuUyDiX9Fd1unCA3YdM3zLGSVEPQ'),
-- ('CdCM27tboWquCEC3Pq3CRphSdiGV6Q48VewWmrxbUyYW'),
-- ('74F2yUFB6kQMmkW3NkXvw1uypwGTKT34PkYmvJLw31Fb'),
-- ('4WEJwCpdDWoQQMDT7ugqiX5VuE3KY3QeYpTTqRoKN9Ea'),
-- ('JD6GNPCYsrEe95dzbfvzWV548ATFBCiv1VpPTeivqtp'),
-- ('38nYRZ7ENv1QNZgDk2d5rARsQroCDC6pRhJmCU8yVMbR'),
-- ('43TMSiFPiw721VP6ue58Kc4cPocFuU1qju1ok9ibDsS6'),
-- ('HmgJG6XqV4HjDEXG47gTx8oobq6Y5W11oeYBD86N2RPR'),
-- ('ETXkmPKnSx8jPqGvvES5Ss3k9q811r7BuqPPwcKZn2pv'),
-- ('YWzEJLhgobeRvYnqVhPRNRC36sEiB9KJWybkXdXoyyb') 
 
    ),
    temp_01 as (
        select
            block_time AS time,
            amount AS amount,
            from_owner AS from_owner,
            to_owner AS to_owner
        from
            tokens_solana.transfers
        where
            block_time > timestamp '2025-03-10 00:00:00 +08:00'
            and
            -- block_time < timestamp '2025-03-17 00:00:00 +08:00'
            block_time < timestamp '2025-03-16 02:00:00 +08:00'
            and token_mint_address = '3DRnsQ5GA2XKk8WJ3yB46KbcRWGtA3E6cgFaqzJWpump'
            AND action = 'transfer'
    ),
    temp_from as (
        SELECT
            from_owner AS owner,
            SUM(- amount) AS from_amount
        FROM
            temp_01
        GROUP BY
            from_owner
    ),
    temp_to as (
        SELECT
            to_owner AS owner,
            SUM(amount) AS to_amount
        FROM
            temp_01
        GROUP BY
            to_owner
    ),
    temp_from_to as (
        select
            COALESCE(temp_from.owner, temp_to.owner) AS owner,
            round(
                (
                    COALESCE(temp_from.from_amount, 0) + COALESCE(temp_to.to_amount, 0)
                ) / 1e6 / 1e6,
                2
            ) AS amount_M
        from
            temp_from
            full outer join temp_to on temp_from.owner = temp_to.owner
            -- 指定specila address
            -- inner join input_address on input_address.address = COALESCE(temp_from.owner, temp_to.owner)
        order by
            2 desc
        limit
            -- 100
            50
    )
select
    temp_from_to.owner as holder_owner,
    sns_domains.domains_owned,
    amount_M,
    round(amount_M / 10, 2) as pct_supply
from
    temp_from_to
    left join solana_utils.sns_domains sns_domains on sns_domains.owner = temp_from_to.owner
order by
    3 desc