Query editor
WITH base_delegation_data AS (SELECTblock_date,delegate,previousVotes / POW(10,18) as previous_votes,newVotes / POW(10,18) as new_votes,newVotes / POW(10,18) - previousVotes / POW(10,18) as votes_change,tx_from,tx_hashFROM TABLE(DECODE_EVM_EVENT(abi => '{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"delegate","type":"address"},{"indexed":false,"internalType":"uint256","name":"previousVotes","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newVotes","type":"uint256"}],"name":"DelegateVotesChanged","type":"event"}',input => TABLE(SELECT *FROM scroll.logsWHERE contract_address IN (0xd29687c813d741e2f938f4ac377128810e217b1b)AND topic0 = 0xdec2bacdd2f05b59de34da9b523dff8be42e5e38e818c82fdb0bae774387a724),null_on_error => FALSE))),daily_metrics AS (SELECTd.block_date,COUNT(DISTINCT d.delegate) as number_of_voted_delegates,SUM(CASE WHEN d.votes_change > 0 THEN d.votes_change ELSE 0 END) as delegated_scr,SUM(CASE WHEN d.votes_change < 0 THEN ABS(d.votes_change) ELSE 0 END) as undelegated_scr,COUNT(DISTINCT d.tx_from) as number_of_delegators,COUNT(DISTINCT d.tx_hash) as number_of_transactions,SUM(d.votes_change) as net_delegation_changeFROM base_delegation_data dGROUP BY 1),cumulative_metrics AS (SELECTblock_date,SUM(net_delegation_change) OVER (ORDER BY block_date) as total_scr_delegatedFROM daily_metrics)SELECTd.block_date,d.number_of_voted_delegates,d.delegated_scr,d.undelegated_scr,d.number_of_delegators,d.number_of_transactions,c.total_scr_delegated,p.price,c.total_scr_delegated * p.price as scr_delegated_market_valueFROM daily_metrics dLEFT JOIN cumulative_metrics c ON d.block_date = c.block_dateLEFT JOIN prices.usd_daily p ON d.block_date = p.dayAND p.contract_address = 0xd29687c813d741e2f938f4ac377128810e217b1bORDER BY d.block_date;