aave / aave-protocol

Aave Protocol Version 1.0 - Decentralized Lending Pools

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

the aToken balance will never reach zero

vseae opened this issue · comments

Regarding the question about aave aToken:

In the provided code, if you withdraw all of your collateral assets, which is represented by userBalance, the scaled balance used to calculate the liquidity index is NI_t_old. After the reserve.updateState() function is called, the liquidity index is updated to NI_t_new. This means that when burning the aToken, another interest accrual occurs, and the burned scaled amount will always be less than userBalance. As a result, the aToken balance will never reach zero.

  function withdraw(
    address asset,
    uint256 amount,
    address to
  ) external override whenNotPaused returns (uint256) {
    DataTypes.ReserveData storage reserve = _reserves[asset];

    address aToken = reserve.aTokenAddress;

    uint256 userBalance = IAToken(aToken).balanceOf(msg.sender);

    uint256 amountToWithdraw = amount;

    if (amount == type(uint256).max) {
      amountToWithdraw = userBalance;
    }

    ValidationLogic.validateWithdraw(
      asset,
      amountToWithdraw,
      userBalance,
      _reserves,
      _usersConfig[msg.sender],
      _reservesList,
      _reservesCount,
      _addressesProvider.getPriceOracle()
    );

    reserve.updateState();

    reserve.updateInterestRates(asset, aToken, 0, amountToWithdraw);


    if (amountToWithdraw == userBalance) {
      _usersConfig[msg.sender].setUsingAsCollateral(reserve.id, false);
      emit ReserveUsedAsCollateralDisabled(asset, msg.sender);
    }

    IAToken(aToken).burn(msg.sender, to, amountToWithdraw, reserve.liquidityIndex);

    emit Withdraw(asset, msg.sender, to, amountToWithdraw);

    return amountToWithdraw;
  }