👻
Aave Book
  • Introduction
  • TradFi vs DeFi: Lending
  • Market forces x Interest Rate Models
  • On Indexes
    • Why use indexes?
  • Scaling and ATokens
  • Deposit & Borrow Interest
  • Stable borrowing
    • 🚧Under construction
  • Liquidation
    • 🚧TODO: Full example
    • 🚧Under construction: oracles
  • Primer
    • Bitmap & Masks
      • 🚧padding and bytes
    • WadRayLibrary
      • Math Operations
      • 🚧WIP: Scaling different decimal representations
      • 🚧WIP: 2's complement
      • 🚧casting: to uint128
    • PercentageMath
    • Embedded vs Linked Libraries
  • Functions
    • General Execution flow
    • Architecture & Design choices
      • 🚧Upgradability and Proxies
    • Common Functions
      • getReserveFactor, getDecimals
      • .cache
      • .updateState
      • .updateInterestRates
      • SupplyCap, BorrowCap
      • getFlags
        • 🚧more on flags
      • calculateUserAccountData
    • supply
      • validateSupply
      • transfer & mint
      • isFirstSupply
        • isUsingAsCollateralOne, isUsingAsCollateralAny
      • On check-effects-interactions pattern
    • withdraw
      • get user balance & withdraw amount
      • validateWithdraw
      • collateral check
      • burn ATokens
      • Ensure existing loans are collateralized
    • borrow
      • getIsolationModeState
      • .validateBorrow
      • Mint debt token
      • setBorrowing
      • update IsolationMode debt
      • transfer underlying to user
    • repay
      • get current debt
      • validateRepay, paybackAmount
      • burn debt tokens
      • Cleanup + Collect repay
    • liquidate
      • _calculateDebt
      • validateLiquidationCall
      • getConfigurationData
      • calculateAvailableCollateralToLiquidate
      • 🚧_burnDebtTokens
      • liquidate/burn collateral
      • liquidation Fee
      • Wrap-up
    • 🚧swapBorrowRateMode
    • 🚧setUserUseReserveAsCollateral
  • Contracts
    • AToken
      • Simple example: mint & balanceOf
    • StableDebtToken
      • Implementation explained
    • VariableDebtToken
    • DefaultReserveInterestRateStrategy
    • L2
      • 🚧PriceOracleSentinel
  • Audit findings
    • 🚧Under construction
  • Appendix
    • Simple, Compound, APR, APY
  • Aave Features
    • Risk Management
      • Supply & Borrow Caps
      • Isolation Mode
      • Siloed Borrowing
    • Other features
      • Repay with ATokens
      • eMode: High efficiency Mode
      • 🚧Aave Vault
      • 🚧Portal
Powered by GitBook
On this page
  • Global currentStableBorrowRate and users' local borrow rate
  • Global stable rate
  • User's stable rate
  • _avgStableRate
  • borrow & mint
  • updateInterestRates::calculateInterestRates
  • Visual Aid

Was this helpful?

Edit on GitHub
  1. Contracts
  2. StableDebtToken

Implementation explained

PreviousStableDebtTokenNextVariableDebtToken

Last updated 1 year ago

Was this helpful?

Global currentStableBorrowRate and users' local borrow rate

There is a global stable rate determined by a model; each user accrues interest based on the stable rate they locked-in. How does this work?

Global stable rate

Each asset in Aave has its configuration data contained within a struct ReserveData. This can be accessed via an internal mapping _reserves.

Held in storage, each ReserveData, contains a uint128 variable currentStableBorrowRate.

  • This is the stable rate enjoyed by incoming stable borrows, regardless of user.

  • This global value is determined by the Utilization model we described above.

Held in storage, the ReserveData struct contains variable: currentStableBorrowRate

User's stable rate

Upon taking up a stable loan, the rate enjoyed by the user is stored in _userState[address].additionalData, as defined on the stableDebtToken contract.

  • .additionalData is his weighted average stable rate, based on all his previous borrows.

  • This is the rate at which interest compounds for the user.

  • Each time a new stable borrow is taken, this is incremented:

    • (oldAmount * oldRate) + (newAmount * newRate) / totalAmount

_userState[address].additionalData is updated within the mint function of stableDebtToken.

_avgStableRate

An internal storage variable on the stable debt token contract, it is the weighted average rate across all the stable borrows taken to date, system-wide.

Incremented like so:

_avgStableRate = (currrentAvgStableRate * previousSupply) + (newRate * newAmount) / (_totalSupply() + newAmount)

Difference between _avgStableRate and currentStableBorrowRate

  • _avgStableRate reflects the average rate at which interest is being accrued by stable borrowers

  • currentStableBorrowRate is the rate for the next incoming stable borrow as determined by the Utilization model.

borrow & mint

mint returns the following two values which are stored in reserveCache:

  • reserveCache.nextAvgStableBorrowRate = vars.currentAvgStableRate

  • reserveCache.nextTotalStableDebt = vars.nextSupply

They are used later in calculateInterestRates to update system-wide rates.

updateInterestRates::calculateInterestRates

After a loan is taken, calculateInterestRates is ran to update the system-wide rates:

  • Liquidity rate

  • Stable borrow rate

  • Variable borrow rate

totalStableDebt used to calculate totalDebt and averageStableBorrowRate used to calculate overallBorrowRate, and consequently, currentLiquidityRate.

Stable borrow rate is updated, accounting for increase in stable loans taken; as are the other rates.

The updated stable rate is stored in reserve.currentStableBorrowRate - in storage.

Visual Aid

StableDebtToken is IncentivizedERC20