# get user balance & withdraw amount

## Overview

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FFiSjuFHG83qd3tYDHv2p%2Fimage.png?alt=media&#x26;token=f4b2b130-41e7-44c0-ac24-509ed1c9d54e" alt=""><figcaption></figcaption></figure>

* [x] <mark style="color:orange;">cache</mark>
* [x] <mark style="color:orange;">updateState</mark>
* [ ] get user's AToken balance + amount to withdraw
* [ ] validateWithdraw: confirm user has sufficient balance and asset isActive
* [ ] <mark style="color:orange;">updateInterestRates</mark>
* [ ] collateral check
* [ ] burn ATokens
* [ ] Ensure existing loans are collateralized

## 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.

{% code overflow="wrap" fullWidth="true" %}
```solidity
uint256 userBalance = 
IAToken(reserveCache.aTokenAddress).scaledBalanceOf(msg.sender).rayMul(reserveCache.nextLiquidityIndex);
```
{% endcode %}

* `scaledBalanceOf` returns the user's aToken balance scaled against the liquidity index at the time of supply.
* $$scaledBalanceOf = Deposit/liquidtyIndex$$

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

{% hint style="success" %}
For in-depth explanation of this behaviour, see: [AToken-balanceOf](../../../scaling-and-atokens#atoken-balanceof)
{% endhint %}

{% hint style="info" %}
Supplied assets accrue supply interest, as accounted for by the scaling against current liquidity index
{% endhint %}

## get withdraw amount

```solidity
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.
