Query editor
WITH positions AS (SELECT * FROM query_7411482),debts AS (SELECT project, chain, borrower_label, borrower, account_shortfall_usd, market AS debt_market, symbol AS debt_symbol, borrow_underlying, borrow_usdFROM positionsWHERE borrow_underlying > 0),collats AS (SELECT project, chain, borrower_label, borrower, market AS collateral_market, symbol AS collateral_symbol,ctoken_balance, collateral_underlying_est, collateral_usd_raw, market_cash_underlying, market_reserves_underlyingFROM positionsWHERE ctoken_balance > 0),pairs AS (SELECT d.*, c.collateral_market, c.collateral_symbol, c.ctoken_balance, c.collateral_underlying_est, c.collateral_usd_raw, c.market_cash_underlying, c.market_reserves_underlying,CASE d.chain WHEN 'ethereum' THEN 'https://ethereum-rpc.publicnode.com' WHEN 'optimism' THEN 'https://optimism-rpc.publicnode.com' END AS rpc,CASE d.project WHEN 'unfederalreserve_v1' THEN '0x3105d328c66d8d55092358cf595d54608178e9b5' WHEN 'hundred_finance' THEN '0x0f390559f258eb8591c8e31cf0905e97cf36ace2' END AS comptrollerFROM debts d JOIN collats c ON d.project=c.project AND d.chain=c.chain AND lower(d.borrower)=lower(c.borrower)),-- Also keep debt-only accounts so final output explicitly shows no collateral candidate.debt_only AS (SELECT d.*, CAST(NULL AS varchar) collateral_market, CAST(NULL AS varchar) collateral_symbol,CAST(NULL AS double) ctoken_balance, CAST(NULL AS double) collateral_underlying_est, CAST(NULL AS double) collateral_usd_raw,CAST(NULL AS double) market_cash_underlying, CAST(NULL AS double) market_reserves_underlying,CASE d.chain WHEN 'ethereum' THEN 'https://ethereum-rpc.publicnode.com' WHEN 'optimism' THEN 'https://optimism-rpc.publicnode.com' END AS rpc,CASE d.project WHEN 'unfederalreserve_v1' THEN '0x3105d328c66d8d55092358cf595d54608178e9b5' WHEN 'hundred_finance' THEN '0x0f390559f258eb8591c8e31cf0905e97cf36ace2' END AS comptrollerFROM debts dWHERE NOT EXISTS (SELECT 1 FROM collats c WHERE d.project=c.project AND d.chain=c.chain AND lower(d.borrower)=lower(c.borrower))),all_rows AS (SELECT * FROM pairsUNION ALL SELECT * FROM debt_only),sel AS (SELECTconcat('0x', substr(to_hex(keccak(to_utf8('liquidateBorrowAllowed(address,address,address,address,uint256)'))),1,8)) AS liquidate_allowed_sel,concat('0x', substr(to_hex(keccak(to_utf8('seizeAllowed(address,address,address,address,uint256)'))),1,8)) AS seize_allowed_sel,concat('0x', substr(to_hex(keccak(to_utf8('borrowGuardianPaused(address)'))),1,8)) AS borrow_paused_sel,concat('0x', substr(to_hex(keccak(to_utf8('seizeGuardianPaused()'))),1,8)) AS seize_paused_global_sel,concat('0x', substr(to_hex(keccak(to_utf8('seizeGuardianPaused(address)'))),1,8)) AS seize_paused_market_sel),calls AS (SELECT r.*,CASE WHEN collateral_market IS NOT NULL THEN json_extract_scalar(json_parse(http_post(r.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"from":"0x000000000000000000000000000000000000dEaD","to":"', r.comptroller, '","data":"', (SELECT liquidate_allowed_sel FROM sel), lpad(substr(r.debt_market,3),64,'0'), lpad(substr(r.collateral_market,3),64,'0'), lpad(substr('0x000000000000000000000000000000000000dEaD',3),64,'0'), lpad(substr(r.borrower,3),64,'0'), lpad('1',64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') END AS liquidate_allowed_hex,CASE WHEN collateral_market IS NOT NULL THEN json_extract_scalar(json_parse(http_post(r.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"from":"0x000000000000000000000000000000000000dEaD","to":"', r.comptroller, '","data":"', (SELECT seize_allowed_sel FROM sel), lpad(substr(r.collateral_market,3),64,'0'), lpad(substr(r.debt_market,3),64,'0'), lpad(substr('0x000000000000000000000000000000000000dEaD',3),64,'0'), lpad(substr(r.borrower,3),64,'0'), lpad('1',64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') END AS seize_allowed_hex,json_extract_scalar(json_parse(http_post(r.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', r.comptroller, '","data":"', (SELECT borrow_paused_sel FROM sel), lpad(substr(r.debt_market,3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') AS debt_borrow_paused_hex,CASE WHEN collateral_market IS NOT NULL THEN json_extract_scalar(json_parse(http_post(r.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', r.comptroller, '","data":"', (SELECT seize_paused_market_sel FROM sel), lpad(substr(r.collateral_market,3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') END AS seize_paused_market_hex,json_extract_scalar(json_parse(http_post(r.rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', r.comptroller, '","data":"', (SELECT seize_paused_global_sel FROM sel), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') AS seize_paused_global_hexFROM all_rows r),decoded AS (SELECT *,try(bytearray_to_uint256(from_hex(substr(liquidate_allowed_hex,3,64)))) AS liquidate_allowed_error,try(bytearray_to_uint256(from_hex(substr(seize_allowed_hex,3,64)))) AS seize_allowed_error,try(bytearray_to_uint256(from_hex(substr(debt_borrow_paused_hex,3,64)))) AS debt_borrow_paused,try(bytearray_to_uint256(from_hex(substr(seize_paused_market_hex,3,64)))) AS seize_paused_market,try(bytearray_to_uint256(from_hex(substr(seize_paused_global_hex,3,64)))) AS seize_paused_globalFROM calls)