antranGetting Started
    Updated 2025-02-15
    WITH weekly AS (
    -- Aggregate weekly totals from both tables within the last 3 years
    SELECT
    COALESCE(d.week_start, b.week_start) AS week_start,
    d.total_supply,
    b.total_borrow
    FROM (
    SELECT
    DATE_TRUNC('WEEK', block_timestamp) AS week_start,
    SUM(amount) AS total_supply
    FROM ethereum.defi.ez_lending_deposits
    WHERE block_timestamp >= DATEADD(YEAR, -3, CURRENT_DATE)
    GROUP BY DATE_TRUNC('WEEK', block_timestamp)
    ) d
    FULL JOIN (
    SELECT
    DATE_TRUNC('WEEK', block_timestamp) AS week_start,
    SUM(amount) AS total_borrow
    FROM ethereum.defi.ez_lending_borrows
    WHERE block_timestamp >= DATEADD(YEAR, -3, CURRENT_DATE)
    GROUP BY DATE_TRUNC('WEEK', block_timestamp)
    ) b
    ON d.week_start = b.week_start
    ),
    constants AS (
    -- Define constants (adjust as needed)
    SELECT
    31536000.0 AS seconds_per_year, -- Number of seconds in a year
    0.02 AS base_borrow_rate, -- Base borrow rate (e.g., 2%)
    0.1 AS slope_low, -- Slope when utilization is below target
    0.3 AS slope_high, -- Slope when utilization is above target
    0.8 AS target_utilization, -- Target utilization ratio (e.g., 80%)
    0.1 AS reserve_factor -- Reserve factor (e.g., 10%)
    ),
    calc AS (
    -- Compute utilization and carry constants
    Last run: 2 months ago
    WEEK
    TOTAL_SUPPLY
    TOTAL_BORROW
    UTILIZATION
    BORROW_RATE
    BORROW_APY
    SUPPLY_RATE
    SUPPLY_APY
    MARGIN
    NET_APY
    1
    2022-02-14 00:00:00.000672051650.140018258514872.2787630.38466518490.068083148120.070454308460.023570295080.0238502728-2184871.35830288-0.003251046788
    2
    2022-02-21 00:00:00.0001998473779.07478848455675.7562290.42455181780.073068977230.075804742920.02791941040.02831281011-7734555.75526816-0.003870231292
    3
    2022-02-28 00:00:00.0001251451460.33743999282985.6094240.79849919660.11981239960.12728535220.086103094310.08991869026-14665210.5833499-0.01171856125
    4
    2022-03-07 00:00:00.000466427557.229217205493830.5822310.44056966060.075071207570.077960910220.02976668680.03021414203-1927777.61487446-0.004133069723
    5
    2022-03-14 00:00:00.000992617403.697181668882049.991160.67385686320.10423210790.10985802790.063213769140.06525453299-8709277.8141338-0.008774053106
    6
    2022-03-21 00:00:00.0001248394663.46352845659089.4248760.6773972320.1046746540.11034930610.063815688790.06589592314-11053774.89288-0.008854391337
    7
    2022-03-28 00:00:00.0001665359561.50825931495409.2009090.5593359120.0899169890.094083458750.045264420950.0463044895-10524685.5877187-0.006319767713
    8
    2022-04-04 00:00:00.0001422904615.79051636043435.030560.44700356440.075875445550.07882819010.030524935150.0309955958-6034376.47325391-0.004240886147
    9
    2022-04-11 00:00:00.000667469144.085925314005828.1857020.47044246310.078805307890.081993647720.033366026830.03392891223-3099981.2522041-0.004644381362
    10
    2022-04-18 00:00:00.000984761512.133288364554655.8237090.37019588130.066274485160.068519966580.02208108730.02232667528-2992822.3275448-0.003039134136
    11
    2022-04-25 00:00:00.0001067172854.2605335307669.3993120.31420183530.059275229410.061067235530.016761947280.01690321947-2437655.45224216-0.002284218009
    12
    2022-05-02 00:00:00.0001556348818.1175464553986.6584050.29848963240.057311204050.058985317270.015396120210.01551524874-3254725.26099944-0.002091256936
    13
    2022-05-09 00:00:00.0003039465122.92732051920878.859230.67509275350.10438659420.11002949970.063423569960.06547804563-26753591.7654681-0.0088020723
    14
    2022-05-16 00:00:00.0001932247546.69964719311059.1853980.37226651440.06653331430.068796567030.022291312510.02254161753-5930146.33325071-0.003069040684
    15
    2022-05-23 00:00:00.0001377125321.90772350538872.9203990.25454391650.051817989560.053184033070.011870958610.01194169582-2197859.31541097-0.001595976256
    16
    2022-05-30 00:00:00.000430695686.329408328999136.6726170.76387841140.11548480140.12241745130.079394711990.08263156156-4686178.69000679-0.01088048671
    17
    2022-06-06 00:00:00.000657786034.566604290767967.2238740.44204034740.075255043420.078159094270.029939188980.03039187352-2734810.99849007-0.004157599667
    18
    2022-06-13 00:00:00.0002086227362.131621285628119.50870.616245450.097030681250.10189418310.053815244240.05528961652-15651316.2325296-0.007502210218
    19
    2022-06-20 00:00:00.0001623837701.62925451674604.0501850.2781525540.054769069250.056296658050.013710740850.01380516181-3010428.50978307-0.00185389741
    20
    2022-06-27 00:00:00.000857238255.233337423475627.0990850.49399991720.081749989650.085184464850.036346039310.03701463426-4343184.17937072-0.005066484321
    ...
    154
    25KB
    4s