👻
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
  • AToken::balanceOf returns:
  • AToken employs a unit increase model
  • On deposit/supply:
  • mint
  • AToken balance

Was this helpful?

Edit on GitHub
  1. Contracts
  2. AToken

Simple example: mint & balanceOf

PreviousATokenNextStableDebtToken

Last updated 1 year ago

Was this helpful?

AToken::balanceOf returns:

super.balanceOf(userAddress) * POOL.getReserveNormalisedIncome(_underlyingAsset)

AToken employs a unit increase model

  • deposit 1 DAI -> user receives 1 aDAI

  • interest is accumulated over time

  • user's aDAI balance increases, reflecting incrementing interest

    • 1 aDAI -> 1.5 aDAI

  • user redeems aTokens 1:1 with underlying

    • can redeem 1.5 DAI

On deposit/supply:

User's deposit is recorded as "scaledBalance":

scaledBalance=amountDepositedcurrentLiquidityIndexscaledBalance = \frac{amountDeposited}{currentLiquidityIndex}scaledBalance=currentLiquidityIndexamountDeposited​

scaledBalance is the actual value stored on-chain, within _userState[userAddress].balance.

All deposits are scaled against the liqudity index and stored as scaled values via _userState[userAddress].balance

Example

mint

The user is minted aTokens according to the scaledBalance, not the deposit value.

  • however, balanceOf is calculated by scaling up the scaledBalance against the incrementing liquidity index.

  • this is why, from the end-user's perspective it looks like they have minted aToken 1:1 to their deposits, and

  • their aToken balance continually increments over time - because the liquidity index increments

AToken balance

aTokenBalance = scaledBalance * currLiquidityIndex
  • LiquidityIndex only increments

  • An account's aTokenBalance can never go down, unless a withdrawal occurs

Simplified illustration:

super.balanceOf(userAddress) * POOL.getReserveNormalisedIncome(_underlyingAsset)

_userState[userAddress].balance * reserve.LiquidityIndex

scaledBalance * currLiquidityIndex

Links

https://medium.com/coinmonks/how-aave-hacked-the-erc-20-token-in-the-most-beautiful-way-3e04fb4410fb
https://mirror.xyz/freesuton.eth/EagGvVQZtLhsnnsTTwhHA6VwekMbImMmvniSWAm6SNg