Query editor
--- METASTREET V2 LOANSwith pools as ( -- find the poolsselect *, coalesce(t.name, 'Awesome NFT') as pool_namefrom (select l.tx_hash, l.block_time, substr(topic1, 13) as pool, substr(e.data, 113, 20) as collectionContract,casewhen length(e.data) = 484 then substr(e.data, 145, 20)else substr(e.data, 209, 20)end as currencyfrom ethereum.logs l left join ethereum.transactions e on l.tx_hash=e.hashwhere l.block_time > date '2023-06-01'and l.topic0=0x4f2ce4e40f623ca765fc0167a25cb7842ceaafb8d82d3dec26ca0d0e0d2d4896and l.contract_address=0x1c91c822F6C5e117A2abe2B33B0E64b850e67095) l left join tokens.nft t on l.collectionContract=t.contract_address),-- get all loans issued by the poolsloans as (select *, pool as lender,ceiling(durationSecs * 1.0 / 86400) as duration,365.0 / (durationSecs / 86400) * (repayment * 1.0 / principal - 1) * 100 as aprfrom (selectl.tx_hash as evt_tx_hash,l.block_time as evt_block_time,bytearray_to_decimal(bytearray_substring(l.data, 65, 1)) as version,bytearray_to_uint256(bytearray_substring(l.data, 66, 32)) as principal,bytearray_to_uint256(bytearray_substring(l.data, 98, 32)) as repayment,bytearray_substring(l.data, 130, 20) as borrower,from_unixtime(bytearray_to_bigint(bytearray_substring(l.data, 150, 8))) as end_time, --maturity,bytearray_to_bigint(bytearray_substring(l.data, 158, 8)) as durationSecs,bytearray_substring(l.data, 166, 20) as noteTokenContract,bytearray_to_uint256(bytearray_substring(l.data, 186, 32)) as tokenId,l.topic1 as loanId,p.pool_name,p.collectionContract,p.currency,p.poolfrom ethereum.logs l inner join pools p on l.contract_address=p.poolwhere l.block_time > date '2023-06-01' and l.topic0 = 0x75cb20cd5ea276edc48a5c9c85e53729cc90bdd6776029b284aa2eb60a334ce2)),loans_with_repayments as (select l.*, r.block_time as repay_time, r.tx_hash as repay_tx_hashfrom loans l left join (select *from ethereum.logswhere topic0=0x1d7f9b884cc43b9a5c67bd7c6f6fb5ddb8cb3eb3994e934e8448d7bf4a4a25c9 -- repayment) r on l.loanId=r.topic1),loans_with_status as (select *, casewhen not repay_time is null then 'REPAID'when (repay_time is null and end_time < current_timestamp) then 'DEFAULTED'else 'ACTIVE'end as statusfrom loans_with_repayments),