Query editor
/*-- @title: Badger eBTC - Accounting-- @author: Steakhouse Financial-- @description: Generates double-entry accounting for Badger's eBTC, which serves as the basis for calculating the profit & loss and balance sheet-- @notes:@todo: no events yet on: 1) stETH flashloans 2)redemptions@dev: using <cast(null as varchar) as customer> to be able to create a materialized view (can't infer type from an always null value)-- @version:1.0 - 2024-09-04 - Initial version*/withdebt as (/******************************************************************************************************************************************** D E B T *********************************************************************************************************************************************/select'DEBT' as ledger,u.evt_block_time as ts,u.evt_tx_hash as txn_hash,u.evt_index,u._borrower as wallet_addr,0x661c70333AA1850CcDBAe82776Bb436A0fCfeEfB as token_addr, -- eBTCconcat('cdp:', cast(varbinary_substring(u._cdpId, 29, 4) as varchar), ', op:', cast(u._operation as varchar)) as metadata,array[1, 1] as sign,array['1020101', -- Inv. Crypto-Backed Loans'20101' -- Liab. Non-yielding] as account_id,coalesce((u._debt / 1e18 - u._oldDebt / 1e18), 0) as amount_txn,coalesce((u._debt / 1e18 - u._oldDebt / 1e18), 0) as amount_base,coalesce((u._debt / 1e18 - u._oldDebt / 1e18), 0) * c.price_btc as value_usdfrom badgerdao_ethereum.CdpManager_evt_CdpUpdated uleft join query_4042988 c on date(u.evt_block_time) = c.dt -- Conversionswhere u._operation in (0, 1, 2, 4) -- 0: open CDP, 1: close CDP, 2: adjust CDP, 4: Normal liquidationand u._debt != u._oldDebt -- exclude collateral updates not affecting debtorder by 1, 2, 3),rev_flash_loans as (/*********************************************************************************************************************** R E V E N U E S : F L A S H L O A N F E E S ************************************************************************************************************************/-- @dev: flash loans can be done with eBTC or stETHselect'REV' as ledger,ts,txn_hash,evt_index,wallet_addr,token_addr,'FLASH-LOANS' as metadata, -- naming convention TBCarray[1, 1] as sign,array[if(token_addr = 0x661c70333AA1850CcDBAe82776Bb436A0fCfeEfB, -- eBTC'1010101', -- Liq. Non-yielding [BC] -> eBTC'1010202' -- Liq. Yielding [Non-BC] -> stETH),'3010101' -- Rev. Platform Fees] as account_id,