Query editor
WITH safes AS (SELECT addr as address, 'bnb' as blockchainFROM ambirewallet_bnb.IdentityFactory_evt_LogDeployedUNION ALLSELECT addr as address, 'polygon' as blockchainFROM ambirewallet_polygon.IdentityFactory_evt_LogDeployedUNION ALLSELECT addr as address, 'ethereum' as blockchainFROM ambirewallet_ethereum.IdentityFactory_evt_LogDeployed),raw_balances AS (-- BNB INFLOWS (Positive)SELECT'bnb' as blockchain,tr.contract_address,SUM(CAST(tr.value AS INT256)) as raw_amountFROM erc20_bnb.evt_transfer trJOIN safes s ON tr."to" = s.address AND s.blockchain = 'bnb'GROUP BY 1, 2UNION ALL-- BNB OUTFLOWS (Negative)SELECT'bnb' as blockchain,tr.contract_address,SUM(CAST(tr.value AS INT256)) as raw_amount -- Note the negative signFROM erc20_bnb.evt_transfer trJOIN safes s ON tr."from" = s.address AND s.blockchain = 'bnb'GROUP BY 1, 2UNION ALL-- POLYGON INFLOWSSELECT'polygon' as blockchain,tr.contract_address,SUM(CAST(tr.value AS INT256)) as raw_amountFROM erc20_polygon.evt_transfer trJOIN safes s ON tr."to" = s.address AND s.blockchain = 'polygon'GROUP BY 1, 2UNION ALL-- POLYGON OUTFLOWSSELECT'polygon' as blockchain,tr.contract_address,SUM(CAST(tr.value AS INT256)) as raw_amountFROM erc20_polygon.evt_transfer trJOIN safes s ON tr."from" = s.address AND s.blockchain = 'polygon'GROUP BY 1, 2UNION ALL-- ETHEREUM INFLOWSSELECT'ethereum' as blockchain,tr.contract_address,SUM(CAST(tr.value AS INT256)) as raw_amount