Query editor
WITH markets(project, chain, rpc, comptroller, label, market, cf) AS (VALUES('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','hWBTC_empty','0x090a00a2de0ea83def700b5e216f87a5d4f394fe',0.75),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','hETH_low_supply','0xe8f12f5492ec28609d2932519456b7436d6c93bd',0.75),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','hDAI_low_supply','0x607312a5c671d0c511998171e634de32156e69d0',0.85),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','hUSDT_low_supply','0xfcd8570ad81e6c77b8d252bebeba62ed980bd64d',0.85),('hundred_finance','optimism','https://optimism-rpc.publicnode.com','0x0f390559f258eb8591c8e31cf0905e97cf36ace2','hSUSD_low_supply_price_missing','0x9a9c7c065efcd4a8ffbf3d97882bbcaed4eb2910',0.85),('unfederalreserve_v1','ethereum','https://ethereum-rpc.publicnode.com','0x3105d328c66d8d55092358cf595d54608178e9b5','unFederal_cETH_low_supply','0xfacece87e14b50eafc85c44c01702f5f485ca460',0.80)),amounts(label, unit_amount) AS (VALUES('hWBTC_empty', 0.001), ('hWBTC_empty', 0.01), ('hWBTC_empty', 0.1),('hETH_low_supply', 0.1), ('hETH_low_supply', 1.0), ('hETH_low_supply', 10.0),('hDAI_low_supply', 100.0), ('hDAI_low_supply', 1000.0), ('hDAI_low_supply', 10000.0),('hUSDT_low_supply', 100.0), ('hUSDT_low_supply', 1000.0), ('hUSDT_low_supply', 10000.0),('hSUSD_low_supply_price_missing', 100.0), ('hSUSD_low_supply_price_missing', 1000.0), ('hSUSD_low_supply_price_missing', 10000.0),('unFederal_cETH_low_supply', 0.1), ('unFederal_cETH_low_supply', 1.0), ('unFederal_cETH_low_supply', 10.0)),sel AS (SELECTconcat('0x', lower(substr(to_hex(keccak(to_utf8('oracle()'))),1,8))) AS oracle_sel,concat('0x', lower(substr(to_hex(keccak(to_utf8('getUnderlyingPrice(address)'))),1,8))) AS price_sel,concat('0x', lower(substr(to_hex(keccak(to_utf8('mintAllowed(address,address,uint256)'))),1,8))) AS mint_allowed_sel),base AS (SELECT m.*,concat('0x', substr(json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', comptroller, '","data":"', (SELECT oracle_sel FROM sel), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result'),27,40)) AS oracle,json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', market, '","data":"0x6f307dc3"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result') AS underlying_hex,bytearray_to_uint256(from_hex(substr(json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', market, '","data":"0x18160ddd"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result'),3,64))) AS total_supply_raw,bytearray_to_uint256(from_hex(substr(json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', market, '","data":"0x3b1d21a2"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result'),3,64))) AS cash_raw,bytearray_to_uint256(from_hex(substr(json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', market, '","data":"0x47bd3718"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result'),3,64))) AS borrows_raw,bytearray_to_uint256(from_hex(substr(json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', market, '","data":"0x8f840ddd"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result'),3,64))) AS reserves_raw,bytearray_to_uint256(from_hex(substr(json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', market, '","data":"0x182df0f5"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result'),3,64))) AS exchange_rate_rawFROM markets m),ud AS (SELECT b.*,CASE WHEN underlying_hex IS NOT NULL AND length(underlying_hex) >= 66 THEN concat('0x', substr(underlying_hex,27,40)) ELSE '0x0000000000000000000000000000000000000000' END AS underlyingFROM base b),dec AS (SELECT u.*,CASE WHEN underlying = '0x0000000000000000000000000000000000000000' THEN UINT256 '18'ELSE try(bytearray_to_uint256(from_hex(substr(json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', underlying, '","data":"0x313ce567"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result'),3,64)))) END AS underlying_decimals,try(bytearray_to_uint256(from_hex(substr(json_extract_scalar(json_parse(http_post(rpc, concat('{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"to":"', oracle, '","data":"', (SELECT price_sel FROM sel), lpad(substr(market,3),64,'0'), '"},"latest"]}'), ARRAY['Content-Type: application/json'])), '$.result'),3,64)))) AS price_rawFROM ud u),scenarios AS (SELECT d.*, ma.unit_amount AS mint_units, da.unit_amount AS donation_units,ma.unit_amount * pow(10, CAST(d.underlying_decimals AS DOUBLE)) AS M_raw,da.unit_amount * pow(10, CAST(d.underlying_decimals AS DOUBLE)) AS D_rawFROM dec dJOIN amounts ma ON d.label = ma.labelJOIN amounts da ON d.label = da.label),calc AS (SELECT *,M_raw * 1e18 / CAST(exchange_rate_raw AS DOUBLE) AS q_ctoken_raw,M_raw * 1e18 / CAST(exchange_rate_raw AS DOUBLE) / 1e8 AS q_ctokens,CAST(total_supply_raw AS DOUBLE) AS S_raw,