Query editor
WITH positions AS (SELECT * FROM query_7411482),debts AS (SELECT * FROM positions WHERE project='unfederalreserve_v1' AND borrow_underlying > 0),collats AS (SELECT * FROM positions WHERE project='unfederalreserve_v1' AND ctoken_balance > 0),pairs AS (SELECT d.project, d.chain, d.borrower_label, d.borrower, d.account_shortfall_usd,d.market AS debt_market, d.symbol AS debt_symbol, d.borrow_underlying, d.borrow_usd,c.market AS collateral_market, c.symbol AS collateral_symbol, c.ctoken_balance, c.collateral_underlying_est, c.collateral_usd_raw, c.market_cash_underlying, c.market_reserves_underlying,'https://ethereum-rpc.publicnode.com' AS rpc, '0x3105d328c66d8d55092358cf595d54608178e9b5' AS comptrollerFROM debts d JOIN collats c ON lower(d.borrower)=lower(c.borrower)),sel AS (SELECTconcat('0x', substr(to_hex(keccak(to_utf8('liquidateBorrowAllowed(address,address,address,address,uint256)'))),1,8)) liquidate_allowed_sel,concat('0x', substr(to_hex(keccak(to_utf8('seizeAllowed(address,address,address,address,uint256)'))),1,8)) seize_allowed_sel,concat('0x', substr(to_hex(keccak(to_utf8('borrowGuardianPaused(address)'))),1,8)) borrow_paused_sel,concat('0x', substr(to_hex(keccak(to_utf8('seizeGuardianPaused()'))),1,8)) seize_paused_global_sel),calls AS (SELECT p.*,json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"from":"0x000000000000000000000000000000000000dEaD","to":"', comptroller, '","data":"', (SELECT liquidate_allowed_sel FROM sel), lpad(substr(debt_market,3),64,'0'), lpad(substr(collateral_market,3),64,'0'), lpad(substr('0x000000000000000000000000000000000000dEaD',3),64,'0'), lpad(substr(borrower,3),64,'0'), lpad('1',64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') liquidate_allowed_hex,json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"from":"0x000000000000000000000000000000000000dEaD","to":"', comptroller, '","data":"', (SELECT seize_allowed_sel FROM sel), lpad(substr(collateral_market,3),64,'0'), lpad(substr(debt_market,3),64,'0'), lpad(substr('0x000000000000000000000000000000000000dEaD',3),64,'0'), lpad(substr(borrower,3),64,'0'), lpad('1',64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') seize_allowed_hex,json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', comptroller, '","data":"', (SELECT borrow_paused_sel FROM sel), lpad(substr(debt_market,3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') debt_borrow_paused_hex,json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', comptroller, '","data":"', (SELECT seize_paused_global_sel FROM sel), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') seize_paused_global_hexFROM pairs p),d AS (SELECT *,try(bytearray_to_uint256(from_hex(substr(liquidate_allowed_hex,3,64)))) liquidate_allowed_error,try(bytearray_to_uint256(from_hex(substr(seize_allowed_hex,3,64)))) seize_allowed_error,try(bytearray_to_uint256(from_hex(substr(debt_borrow_paused_hex,3,64)))) debt_borrow_paused,try(bytearray_to_uint256(from_hex(substr(seize_paused_global_hex,3,64)))) seize_paused_globalFROM calls)SELECT project, chain, borrower_label, borrower, account_shortfall_usd,debt_market, debt_symbol, borrow_underlying, borrow_usd,collateral_market, collateral_symbol, ctoken_balance, collateral_underlying_est, collateral_usd_raw, market_cash_underlying, market_reserves_underlying,liquidate_allowed_error, seize_allowed_error, debt_borrow_paused, seize_paused_global,CASEWHEN liquidate_allowed_error = UINT256 '0' AND (seize_allowed_error = UINT256 '0' OR seize_allowed_error IS NULL) THEN 'LIQUIDATION_ALLOWED_BY_COMPTROLLER'ELSE concat('BLOCKED_OR_ERROR liquidate=', coalesce(CAST(liquidate_allowed_error AS varchar),'null'), ' seize=', coalesce(CAST(seize_allowed_error AS varchar),'null'))END AS liquidation_path_status,CASE WHEN coalesce(market_cash_underlying,0)=0 THEN 'collateral cash zero; seized cTokens may not be redeemable now' ELSE 'collateral market has cash' END AS collateral_cash_noteFROM dORDER BY account_shortfall_usd DESC, borrower_label, collateral_usd_raw DESC