Query editor
WITH-- all unstakes from $MOCA stakingunstakes AS (SELECTDATE_TRUNC('day', a.evt_block_time) AS day,a."to" AS wallet,SUM(a.value/1e18) AS amountFROM erc20_ethereum.evt_transfer aJOIN ethereum.transactions b ON a.evt_tx_hash = b.hashWHERE a.contract_address = 0xF944e35f95E819E752f3cCB5Faf40957d311e8c5AND a."from" = 0x9a98E6B60784634AE273F2FB84519C7F1885AeD2AND CAST(b.data AS VARCHAR) LIKE '%0x2e17de78%'GROUP BY DATE_TRUNC('day', a.evt_block_time), a."to"),-- Airdrop 1 walletsairdrop_wallets AS (SELECTwallet,SUM(behalf_stake/1e18) AS airdrop_tokens,DATE_TRUNC('day', MIN(tx_date)) AS behalf_stake_dateFROM dune.abtokenomics.moca_behalf_stakingGROUP BY wallet),-- total daily unstakes per airdrop walletfiltered_unstakes AS (SELECTu.day,u.wallet,u.amount AS amount_unstaked,SUM(u.amount) OVER (PARTITION BY u.walletORDER BY u.dayROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumumaltive_amount_unstaked,aw.airdrop_tokensFROM unstakes uJOIN airdrop_wallets aw ON u.wallet = aw.wallet AND u.day >= aw.behalf_stake_date),-- filtered unstakes with flagging whether wallet fully unstakedfull_unstakes AS (SELECT*,CASEWHEN airdrop_tokens - cumumaltive_amount_unstaked <= 0 THEN 1ELSE 0END AS is_fully_unstakedFROM filtered_unstakes),-- defining exact day of wallet fully unstakingfull_unstake_date AS (SELECTwallet,MIN(day) AS full_unstake_dayFROM full_unstakesWHERE is_fully_unstaked = 1GROUP BY wallet),-- filtering only those records where wallets fully unstakedfull_unstakes_filtered AS (SELECT