👻
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
  • Overview
  • mint stableDebtTokens
  • _calculateBalanceIncrease
  • totalSupply
  • Calculate new stable rate
  • Update average Stable rate
  • mint tokens
  • mint variableDebtTokens

Was this helpful?

Edit on GitHub
  1. Functions
  2. borrow

Mint debt token

Previous.validateBorrowNextsetBorrowing

Last updated 1 year ago

Was this helpful?

Overview

  • If stable interest was selected -> mint stable debt tokens

  • Else -> mint variable debt tokens

mint stableDebtTokens

rate: reserve.currentStableBorrowRate

_calculateBalanceIncrease

  • For a user, calculates the increase in stable debt, due to interest accrued from timestamp of their last update, to current time.

  • Serves to update user's prior stable debt

balanceIncrease reflects interest accrued from lastUpdate till current block.timestamp.

Does not relate to incoming borrow action.

totalSupply

    vars.previousSupply = totalSupply();
    vars.currentAvgStableRate = _avgStableRate;
    vars.nextSupply = _totalSupply = vars.previousSupply + amount;

vars.previousSupply = totalSupply();

totalSupply returns the updated supply value, by accounting for recently accrued interest, from _totalSupplyTimestamp till now, based on the _avgStableRate.

  • _avgStableRate: internal storage variable on StableDebtToken.sol

  • previousSupply: updated supply value

  • principalSupply => super.totalSupply calls totalSupply on IncentivizedERC20 which returns _totalSupply

previousSupply is set to totalSupply() instead of _totalSupply directly, so that the updated supply value can be obtained.

With recent interest accounted for, the storage variable _totalSupply is updated with the incoming stable debt, amount.

vars.nextSupply = _totalSupply = 
    vars.previousSupply + amount;

Calculate new stable rate

  • nextStableRate is new stable rate, specific to the user

nextStableRate = 
(_avgStableRate * currentBalance) + (amount * rate / currentBalance + amount)
  • nextStableRate stored in the user's UserState.additionalData struct. userState mapping collects all users' UserState structs.

  • Store update time in _timestamps._timestamps mapping collects the last update timestamp for all users.

  • Update the timestamp of the last update of _totalSupply.

rate: reserve.currentStableBorrowRate

Update average Stable rate

  • _avgStableRate: internal uint128 on StableDebtToken.sol

  • vars.currentAvgStableRate was set to _avgStableRate at the start of mint

newAvgStableRate = 
(_avgStableRate * previousSupply) + [(rate * amountInRay) / nextSupply]
  • previousSupply: updatedtotalSupply

mint tokens

Notice that we do not just mint as per the incoming stable debt amount; but also mint for balanceIncrease. This serves to update user's prior stable debt based on interest accrued over time elapsed since last update.

  • Since we going to mint, mint to update the user's entire position

mint calls the internal function _mint:

mint variableDebtTokens

  • amountScaled: scale the borrow amount against variableBorrowIndex

See variableDebtToken

Drawing
mint