Query editor
WITH comps(project, chain, rpc, comptroller) AS (VALUES('bao_markets','ethereum','https://ethereum-rpc.publicnode.com','0x0BE1FDC1E87127C4FE7C05BAE6437E3CF90BF8D8'),('unfederalreserve_v1','ethereum','https://ethereum-rpc.publicnode.com','0x3105d328c66d8d55092358cf595d54608178e9b5'),('mendi','linea','https://rpc.linea.build','0x1b4d3b0421ddc1eb216d230bc01527422fb93103'),('moonwell','base','https://base-rpc.publicnode.com','0xFBB21D0380BEE3312B33C4353C8936A0F13EF26C')),borrowers_raw AS (SELECT 'bao_markets' AS project, 'ethereum' AS chain, borrower, max(call_block_time) AS last_seenFROM bao_markets_ethereum.comptroller_call_borrowverifyWHERE call_success AND call_block_date >= CURRENT_DATE - INTERVAL '1460' DAYGROUP BY 1,2,3UNION ALLSELECT 'unfederalreserve_v1','ethereum', borrower, max(call_block_time)FROM unfederalreserve_v1_ethereum.comptroller_call_borrowverifyWHERE call_success AND call_block_date >= CURRENT_DATE - INTERVAL '1460' DAYGROUP BY 1,2,3UNION ALLSELECT 'mendi','linea', borrower, max(evt_block_time)FROM mendi_linea.cerc20upgradable_evt_borrowWHERE evt_block_date >= CURRENT_DATE - INTERVAL '730' DAYGROUP BY 1,2,3UNION ALLSELECT 'moonwell','base', borrower, max(evt_block_time)FROM moonwell_base.mcbeth_evt_borrowWHERE evt_block_date >= CURRENT_DATE - INTERVAL '365' DAYGROUP BY 1,2,3UNION ALLSELECT 'moonwell','base', borrower, max(evt_block_time)FROM moonwell_base.mcbbtc_evt_borrowWHERE evt_block_date >= CURRENT_DATE - INTERVAL '365' DAYGROUP BY 1,2,3UNION ALLSELECT 'moonwell','base', borrower, max(evt_block_time)FROM moonwell_base.mamo_evt_borrowWHERE evt_block_date >= CURRENT_DATE - INTERVAL '365' DAYGROUP BY 1,2,3),ranked AS (SELECT project, chain, borrower, max(last_seen) AS last_seen,row_number() OVER (PARTITION BY project ORDER BY max(last_seen) DESC) AS rnFROM borrowers_rawGROUP BY 1,2,3),limited AS (SELECT r.project, r.chain, r.borrower, r.last_seen, c.rpc, c.comptrollerFROM ranked r JOIN comps c ON r.project=c.project AND r.chain=c.chainWHERE r.rn <= 25),sel AS (SELECT concat('0x', substr(to_hex(keccak(to_utf8('getAccountLiquidity(address)'))),1,8)) AS liquidity_sel),calls AS (SELECT l.project, l.chain, l.borrower, l.last_seen, l.rpc, l.comptroller,json_extract_scalar(json_parse(http_post(l.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', l.comptroller, '","data":"', (SELECT liquidity_sel FROM sel), lpad(substr(concat('0x', to_hex(l.borrower)),3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') AS res_hexFROM limited l),decoded AS (SELECT project, chain, concat('0x', to_hex(borrower)) AS borrower, last_seen,try(bytearray_to_uint256(from_hex(substr(res_hex,3,64)))) AS err,try(CAST(bytearray_to_uint256(from_hex(substr(res_hex,67,64))) AS DOUBLE))/1e18 AS liquidity_usd_assuming_1e18,try(CAST(bytearray_to_uint256(from_hex(substr(res_hex,131,64))) AS DOUBLE))/1e18 AS shortfall_usd_assuming_1e18