angealTotal DAI Discount
    Updated 2023-03-13
    -- forked from 28074afe-52a2-4a33-b191-0d905fc174f4

    -- assuming people bought USDC at a discount right before payment,
    -- how much $ did they save?
    WITH
    prices AS (
    SELECT
    recorded_hour,
    'DAI' AS token_symbol,
    avg(close) AS price
    FROM
    crosschain.core.fact_hourly_prices
    WHERE
    recorded_hour > current_date - 3
    AND id IN ('dai', '4943')
    GROUP BY
    recorded_hour,
    token_symbol
    ),
    hourly_repayments AS (
    SELECT
    'aave' AS protocol,
    symbol AS token_symbol,
    date_trunc('hour', block_timestamp) AS hour,
    sum(repayed_tokens) AS tokens_repaid,
    count(tx_hash) AS n_repayments
    FROM
    ethereum.aave.ez_repayments
    WHERE
    block_timestamp > current_date - 3
    AND symbol IN ('USDC', 'DAI')
    GROUP BY
    protocol,
    token_symbol,
    hour
    UNION
    Run a query to Download Data