CryptoIcicleAAVE-30. [Hard] Means of Repayment - Token
    Updated 2021-11-06
    -- When people repay their loans, do they liquidate themselves and use collateral growth to do so,
    -- or are they paying off their loan with external (to Aave) funding? How does this relate to the collateral's price?
    with liquidations as (
    select
    *
    from
    aave.liquidations
    ), repayments_after_liquidation as (
    select
    *
    from
    aave.repayments r
    join liquidations l on l.debt_token_symbol = r.symbol
    and l.block_timestamp < r.block_timestamp
    and l.borrower = r.borrower
    and l.liquidated_amount_usd > r.repayed_usd
    )
    (
    select
    count(*) as number,
    symbol,
    'total repayments' as type
    from
    aave.repayments
    group by type, symbol
    )
    union
    (
    select
    count(*) as number,
    symbol,
    'total repayments after liquidations' as type
    from
    repayments_after_liquidation
    group by type, symbol
    Run a query to Download Data