get user balance & withdraw amount

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/liquidtyIndex

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

For in-depth explanation of this behaviour, see: AToken-balanceOf

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.

Last updated