Query editor
/*Query outputs the list of holders of a target token, including:1. Wallet addresses2. Wallet balance3. % of the total token supply held4. Staking balance if the project has any token staking*/WITH/*1. Calculating net balances of wallets which interacted with the target token at least once:-- token_sales -> wallets which sold the token and amounts sold by wallet-- token_buys -> wallets which bought the token and amounts bought by wallet-- holders_balances -> net (bought-sold) per walletNOTE! We use token transfer events here, so this will include airdrops and other indirect token flows in and out.*/token_sales AS (SELECT "from" AS seller, SUM(value) AS amt_soldFROM erc20_{{blockchain}}.evt_transferWHERE contract_address = {{token_address}}AND "from" NOT IN ({{addresses_to_ignore}})GROUP BY "from"),token_buys AS (SELECT "to" AS buyer, SUM(value) AS amt_boughtFROM erc20_{{blockchain}}.evt_transferWHERE contract_address = {{token_address}}AND "to" NOT IN ({{addresses_to_ignore}})GROUP BY "to"),holders_balances AS (SELECTCOALESCE(a.buyer, b.seller) AS holder,CASEWHEN a.amt_bought IS NULL THEN 0ELSE (a.amt_bought / 1e18 - COALESCE(b.amt_sold / 1e18, 0)) -- can param it when refactoringEND AS balanceFROM token_buys aFULL JOIN token_sales bON a.buyer = b.seller),/*2. Similarly to balances held, calculate staking balances per wallet.NOTE! If there's a negative staking balance, this might mean that there was an on-behalf staking,where the project staked tokens on behalf of the entitled owner. Example: $MOCA on-behalf staking of Jul'2024.*/stakes AS (SELECT a."from" AS staker, SUM(a.value) AS amt_stakedFROM erc20_{{blockchain}}.evt_transfer aJOIN {{blockchain}}.transactions bON a.evt_tx_hash = b.hashWHERE a.contract_address = {{token_address}}AND a."from" NOT IN ({{addresses_to_ignore}})AND a."to" = {{staking_address}}AND CAST(b.data AS VARCHAR) LIKE '%{{stake_method_id}}%'GROUP BY a."from"),