messariBorrowed Amount copy
    Updated 2025-04-08
    -- forked from pietrekt / Borrowed Amount @ https://flipsidecrypto.xyz/pietrekt/q/c66vl1MnKTnY/borrowed-amount

    with base as (select block_timestamp, dim_block_id,
    owner,
    round(collateralization_ratio/10000, 2) as CR,
    round((1/CR) * 100, 2) as LTV,
    collateral_asset,
    target_asset as borrow_asset
    from thorchain.defi.fact_loan_open_events),

    base2 as (select a.block_timestamp, a.dim_block_id, owner, cr, LTV,
    collateral_asset,
    from_amount,
    from_amount_USD,
    borrow_asset,
    case
    when affiliate_address = 'thor160yye65pf9rzwrgqmtgav69n6zlsyfpgm9a7xk' then 'THORSwap'
    when affiliate_address = 'wr' then 'THORWallet'
    when affiliate_address = 'tl' then 'TS Ledger'
    when affiliate_address = 'cb' then 'Team CoinBot'
    when affiliate_address is null then 'No Affiliate'
    else affiliate_address
    end as affiliates,
    tx_id
    from base a
    left join thorchain.defi.fact_swaps b
    on a.block_timestamp = b.block_timestamp and a.owner = b.from_address
    where tx_id in (select tx_id
    from thorchain.defi.fact_swaps_events
    where memo like 'loan%')
    and from_asset in ('BTC.BTC', 'ETH.ETH')),

    --not borrowing rune
    non_rune_borrows as (select a.block_timestamp, owner, cr, LTV,
    collateral_asset,
    a.from_amount,
    Last run: 15 days ago
    DAY
    BORROW_ASSET_LABEL
    BORROW_AMOUNT_USD
    CUMULATIVE_BY_ASSET_BORROW_AMOUNT_USD
    CUMULATIVE_BORROW_AMOUNT_USD
    1
    2024-09-05 00:00:00.000THOR.RUNE10.97392648912435752.9444099111119121.710554
    2
    2024-05-29 00:00:00.000Stablecoin851.67099588658805536.1192605111119110.736628
    3
    2024-05-29 00:00:00.000BTC.BTC13469.75070427919193192.7754763111119110.736628
    4
    2024-05-28 00:00:00.000Stablecoin16846.30103026758804684.4482647111104789.314928
    5
    2024-05-28 00:00:00.000BTC.BTC582.8575691119179723.0247721111104789.314928
    6
    2024-05-27 00:00:00.000ETH.ETH9.99097294219235223.8896326111087360.156328
    7
    2024-05-27 00:00:00.000BTC.BTC53058.84223643219179140.1672029111087360.156328
    8
    2024-05-27 00:00:00.000Stablecoin217550.36180860258787838.1472344111087360.156328
    9
    2024-05-27 00:00:00.000THOR.RUNE37732.03063941412435741.9704834111087360.156328
    10
    2024-05-27 00:00:00.000Other129971.0397076911449415.98177478111087360.156328
    11
    2024-05-20 00:00:00.000Stablecoin151859.0313929358570287.7854258110649037.890963
    12
    2024-05-20 00:00:00.000BTC.BTC1554.69994079919126081.3249665110649037.890963
    13
    2024-05-20 00:00:00.000Other23260.3148907851319444.94206709110649037.890963
    14
    2024-05-19 00:00:00.000THOR.RUNE42111.87802225712398009.939844110472363.844739
    15
    2024-05-19 00:00:00.000Stablecoin278571.12797528158418428.7540329110472363.844739
    16
    2024-05-19 00:00:00.000Other65452.4531640731296184.62717631110472363.844739
    17
    2024-05-18 00:00:00.000Other330.5565212651230732.17401223110086228.385577
    18
    2024-05-18 00:00:00.000Stablecoin687242.42751588158139857.6260576110086228.385577
    19
    2024-05-18 00:00:00.000ETH.ETH3499.78997844219235213.8986597110086228.385577
    20
    2024-05-17 00:00:00.000Stablecoin1397025.5523358857452615.1985417109395155.611561
    ...
    743
    64KB
    9s