Query editor
WITH markets AS (SELECT DISTINCT from_hex(substr(market,3)) AS marketFROM query_7410547WHERE project='hundred_finance'),borrow_logs AS (SELECT varbinary_substring(data, 13, 20) AS borrower, max(block_time) AS last_seenFROM optimism.logsWHERE block_date >= DATE '2022-01-01'AND contract_address IN (SELECT market FROM markets)AND topic0 = keccak(to_utf8('Borrow(address,uint256,uint256,uint256)'))GROUP BY 1),ranked AS (SELECT borrower, last_seen, row_number() OVER (ORDER BY last_seen DESC) AS rnFROM borrow_logs),limited AS (SELECT * FROM ranked WHERE rn <= 40),sel AS (SELECT concat('0x', substr(to_hex(keccak(to_utf8('getAccountLiquidity(address)'))),1,8)) AS liquidity_sel),calls AS (SELECT l.borrower, l.last_seen,json_extract_scalar(json_parse(http_post('https://optimism-rpc.publicnode.com', concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"0x0f390559f258eb8591c8e31cf0905e97cf36ace2","data":"', (SELECT liquidity_sel FROM sel), lpad(to_hex(l.borrower),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') AS res_hexFROM limited l),decoded AS (SELECT 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_1e18FROM calls)SELECT *, CASE WHEN shortfall_usd_assuming_1e18 > 0 THEN 'HAS_SHORTFALL' ELSE 'NO_SHORTFALL_IN_SAMPLE' END AS statusFROM decodedORDER BY shortfall_usd_assuming_1e18 DESC, last_seen DESC