Query editor
/*-- @title: Badger eBTC - CDP Details-- @author: Steakhouse Financial-- @description: Provides debt, collateral, protocol yield share and different collateral ratios per CDP position-- @notes:-- - field <is_active> to be validated depending on non-tested statuses -> currently using operations 0, 2, 3 & 7-- - ignore CDPs with 0 debt & coll?-- @version:- 1.0 - 2024-09-03 - Initial version*/with-- retrieves debt and collateral from all cdp operations-- using rn to get only the latest operation x daycdp_ops as (selectdate(u.evt_block_time) as dt,varbinary_to_uint256(varbinary_substring(_cdpId, 29, 4)) AS cdp_number,_operation as op,casewhen _operation in (0, 2, 3, 7) then true -- @todo: TBCelse falseend as is_active,conv.factor,u._borrower as borrower,coalesce(u._debt / 1e18, 0) as debt,casewhen i1._newIndex is not null then coalesce((u._collShares / 1e18) * (i1._newIndex / 1e18), 0)when conv.index is not null then coalesce((u._collShares / 1e18) * conv.index, 0)else -1end as coll,row_number() over (partition by date(u.evt_block_time), _cdpId order by u.evt_block_time desc) as rnfrom badgerdao_ethereum.CdpManager_evt_CdpUpdated uleft join badgerdao_ethereum.CdpManager_evt_StEthIndexUpdated i1 using(evt_tx_hash)left join query_4042988 conv on date(u.evt_block_time) = conv.dt -- Conversionswhere u._operation in (0, 1, 2, 4) -- 0: open CDP, 1: close CDP, 2: adjust CDP, 4: Normal liquidationorder by 1 asc),-- gets the date of the first operation (ie: open) per cdpcdp_mindate as (selectcdp_number,min(dt) as min_dtfrom cdp_opsgroup by 1),-- date filling for every cdp from its cdp open date until the present day,-- carrying forward the debt, collateral and factorcdp_filling as (selectdates.dt as dt,md.cdp_number,cdp.op,last_value(is_active) ignore nulls over (partition by md.cdp_number order by dates.dt asc) as is_active,last_value(cdp.borrower) ignore nulls over (partition by md.cdp_number order by dates.dt asc) as borrower,cdp.coll as new_coll,last_value(factor) ignore nulls over (partition by md.cdp_number order by dates.dt asc) as start_factor,last_value(cdp.debt) ignore nulls over (partition by md.cdp_number order by dates.dt asc) as debt,last_value(cdp.coll) ignore nulls over (partition by md.cdp_number order by dates.dt asc) as collfrom cdp_mindate md