emferonsKPI - Stablecoin Borrow Volume copy
    Updated 2025-01-08
    -- forked from charliemarketplace / KPI - Stablecoin Borrow Volume @ https://flipsidecrypto.xyz/charliemarketplace/q/_OO24096-FQy/kpi---stablecoin-borrow-volume

    -- forked from KPI-stablecoin_borrow_volume @ https://flipsidecrypto.xyz/studio/queries/817ed49a-72c1-4372-9899-b20e06e9ab36

    WITH stablecoins AS (
    SELECT tokens
    FROM (VALUES
    ('a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near'),
    ('853d955acef822db058eb8505911ed77f175b99e.factory.bridge.near'), -- frax
    ('17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1'),
    ('dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near'),
    ('usdt.tether-token.near')
    ) AS s(tokens)
    )

    select
    date_trunc('month', block_timestamp) as month_,
    case
    when amount_usd >= 1000000 then '$1M+'
    when amount_usd >= 100000 then '$100-999k'
    when amount_usd >= 10000 then '$10-99k'
    when amount_usd >= 1000 then '$1-9.9k'
    else '$0-999' end as size_,
    case
    when amount_usd >= 1000000 then '5'
    when amount_usd >= 100000 then '4'
    when amount_usd >= 10000 then '3'
    when amount_usd >= 1000 then '2'
    else '1' end as size_order,
    sum(amount_usd) as borrow_usd_volume,
    avg(amount_usd) as avg_borrow_usd_amount,
    count(distinct sender_id) as n_borrowers
    from near.defi.ez_lending
    where block_timestamp >= '2024-01-01'
    and actions = 'borrow'
    and amount_usd IS NOT NULL
    QueryRunArchived: QueryRun has been archived