Query editor
WITH accounts(project, chain, rpc, comptroller, borrower, borrower_label) AS (VALUES('unfederalreserve_v1','ethereum','https://ethereum-rpc.publicnode.com','0x3105d328c66d8d55092358cf595d54608178e9b5','0x7ED1E856230840868CD731ADA5C45BED497AF1A5','unfed_shortfall_402k'),('unfederalreserve_v1','ethereum','https://ethereum-rpc.publicnode.com','0x3105d328c66d8d55092358cf595d54608178e9b5','0xB66ADDB2EF71EAC4F83E570EBFD4EBE5A7FF402A','unfed_admin_shortfall_119k'),('unfederalreserve_v1','ethereum','https://ethereum-rpc.publicnode.com','0x3105d328c66d8d55092358cf595d54608178e9b5','0x498D019045A19DC7B7A6E0BFF1568D88CFA5D57B','unfed_shortfall_33'),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','0x7063B9C4FA70B10FF6FEB663D09C02399A6B71B5','hundred_shortfall_64k_a'),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','0xEC9F47EDADD4908C02091E0C8CAC23921E7ADAA2','hundred_shortfall_64k_b'),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','0x7D291407ECE9FDB43B95620E7A4417B678127A3E','hundred_shortfall_1_9k'),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','0x069A019948B81C5679E2D10E9814389D74C3230C','hundred_shortfall_1_7k'),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','0x6DE21A3009C355F02E489C72754FB804A4DF0E32','hundred_shortfall_243')),markets AS (SELECT project, chain, market, cf_pctFROM query_7410547WHERE project IN ('unfederalreserve_v1','hundred_finance')),base AS (SELECT a.project, a.chain, a.rpc, a.comptroller, a.borrower, a.borrower_label, m.market, m.cf_pctFROM accounts a JOIN markets m ON a.project=m.project AND a.chain=m.chain),sel AS (SELECT '0x70a08231' balance_sel,concat('0x', substr(to_hex(keccak(to_utf8('borrowBalanceStored(address)'))),1,8)) borrow_stored_sel,'0x182df0f5' exchange_rate_sel, '0x3b1d21a2' get_cash_sel, '0x8f840ddd' reserves_sel, '0x6f307dc3' underlying_sel,concat('0x', substr(to_hex(keccak(to_utf8('oracle()'))),1,8)) oracle_sel,concat('0x', substr(to_hex(keccak(to_utf8('getUnderlyingPrice(address)'))),1,8)) price_sel,concat('0x', substr(to_hex(keccak(to_utf8('getAccountLiquidity(address)'))),1,8)) liquidity_sel),comp AS (SELECT DISTINCT a.project, a.chain, a.rpc, a.comptroller,json_extract_scalar(json_parse(http_post(a.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', a.comptroller, '","data":"', (SELECT oracle_sel FROM sel), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') oracle_hexFROM accounts a),comp_dec AS (SELECT project, chain, rpc, comptroller, CASE WHEN oracle_hex IS NOT NULL AND length(oracle_hex)>=66 THEN concat('0x', substr(oracle_hex,27,40)) END oracle FROM comp),rpc_calls AS (SELECT b.*, c.oracle,json_extract_scalar(json_parse(http_post(b.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', b.market, '","data":"', (SELECT balance_sel FROM sel), lpad(substr(b.borrower,3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') balance_hex,json_extract_scalar(json_parse(http_post(b.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', b.market, '","data":"', (SELECT borrow_stored_sel FROM sel), lpad(substr(b.borrower,3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') borrow_hex,json_extract_scalar(json_parse(http_post(b.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', b.market, '","data":"', (SELECT exchange_rate_sel FROM sel), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') exchange_rate_hex,json_extract_scalar(json_parse(http_post(b.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', b.market, '","data":"', (SELECT get_cash_sel FROM sel), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') cash_hex,json_extract_scalar(json_parse(http_post(b.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', b.market, '","data":"', (SELECT reserves_sel FROM sel), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') reserves_hex,json_extract_scalar(json_parse(http_post(b.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', b.market, '","data":"', (SELECT underlying_sel FROM sel), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') underlying_hex,json_extract_scalar(json_parse(http_post(b.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', b.comptroller, '","data":"', (SELECT liquidity_sel FROM sel), lpad(substr(b.borrower,3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') account_liquidity_hexFROM base b JOIN comp_dec c ON b.project=c.project AND b.chain=c.chain AND lower(b.comptroller)=lower(c.comptroller)),dec1 AS (SELECT *, CASE WHEN underlying_hex IS NOT NULL AND length(underlying_hex)>=66 THEN concat('0x', substr(underlying_hex,27,40)) END underlying,try(CAST(bytearray_to_uint256(from_hex(substr(balance_hex,3,64))) AS DOUBLE)) ctoken_balance_raw,try(CAST(bytearray_to_uint256(from_hex(substr(borrow_hex,3,64))) AS DOUBLE)) borrow_raw,try(CAST(bytearray_to_uint256(from_hex(substr(exchange_rate_hex,3,64))) AS DOUBLE)) exchange_rate_raw,try(CAST(bytearray_to_uint256(from_hex(substr(cash_hex,3,64))) AS DOUBLE)) cash_raw,try(CAST(bytearray_to_uint256(from_hex(substr(reserves_hex,3,64))) AS DOUBLE)) reserves_raw,try(CAST(bytearray_to_uint256(from_hex(substr(account_liquidity_hex,131,64))) AS DOUBLE))/1e18 account_shortfall_usdFROM rpc_calls),price AS (SELECT d.*, json_extract_scalar(json_parse(http_post(d.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', d.oracle, '","data":"', (SELECT price_sel FROM sel), lpad(substr(d.market,3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') price_hexFROM dec1 d