👻
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
  • get user balance
  • get withdraw amount

Was this helpful?

Edit on GitHub
  1. Functions
  2. withdraw

get user balance & withdraw amount

PreviouswithdrawNextvalidateWithdraw

Last updated 1 year ago

Was this helpful?

Overview

get user balance

Check user's balance of ATokens corresponding to the asset their wish to withdraw. His aToken balance will dictate how much of the underlying is made available for withdrawal.

uint256 userBalance = 
IAToken(reserveCache.aTokenAddress).scaledBalanceOf(msg.sender).rayMul(reserveCache.nextLiquidityIndex);
  • scaledBalanceOf returns the user's aToken balance scaled against the liquidity index at the time of supply.

  • scaledBalanceOf=Deposit/liquidtyIndex scaledBalanceOf = Deposit/liquidtyIndexscaledBalanceOf=Deposit/liquidtyIndex

This scaled balance is multiplied against the current liquidity index, which was updated in updateState.

Supplied assets accrue supply interest, as accounted for by the scaling against current liquidity index

get withdraw amount

uint256 amountToWithdraw = params.amount;

if (params.amount == type(uint256).max) {
    amountToWithdraw = userBalance;
}
  • If the supplied input is the maximum uint256 value, the withdrawal amount is set to the user's balance of ATokens => withdraw everything

  • Otherwise, withdraw the amount as dictated by user input.

For in-depth explanation of this behaviour, see:

AToken-balanceOf