# isFirstSupply

## Overview

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FaOrJL71t9Dp7mhKiWiV6%2Fimage.png?alt=media&#x26;token=f8396415-b51c-46d1-bbc9-ae3835d30136" alt=""><figcaption></figcaption></figure>

* [x] <mark style="color:orange;">cache</mark>
* [x] <mark style="color:orange;">updateState</mark>
* [x] validateSupply
* [x] <mark style="color:orange;">updateInterestRates</mark>
* [x] transfer & mint
* [ ] If isFirstSupply

This is the last execution block for a supply action. This if block only executes in the event of a user's very first supply action for a specific asset.

If on minting ATokens, `isFirstSupply` is returned as `TRUE`, and the nested `validateUseAsCollateral` returns `TRUE` as well,&#x20;

* `isFirstSupply` : `TRUE` => user's first time supplying this specific asset
* `validateUseAsCollateral` : `TRUE` => either:
  * `isUsingAnyCollateralAny`: `TRUE` => user never supplied any asset ever,
  * OR,
  * `!isolationMode && getDebtCeiling == 0`: TRUE => there is no debt ceiling on incoming supplied asset AND the user is not in isolation mode.

<img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2F5W1CydykJvJoVnaVYxja%2Ffile.excalidraw.svg?alt=media&#x26;token=fa8d52db-5267-48ae-96f9-f7a67042ad6b" alt="" class="gitbook-drawing">

## isFirstSupply

* captures the return value from `mint`
* `TRUE` if user has no prior supply actions, and therefore a pre-dating zero supply balance

If `TRUE`, execution moves into the nested if block, with the return value of `validateUseAsCollateral` as the condition.

### validateUseAsCollateral

This function checks if the incoming asset can be used as collateral. If the user is previously in [isolated mode](https://calnix.gitbook.io/aave-book/aave-features/risk-management/isolation-mode), other assets cannot be supplied as collateral at the same time.&#x20;

{% hint style="info" %}
Supplied assets are automatically treated as collateral, if possible.
{% endhint %}

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FrCkPGjccldxhQvZIhFHz%2Fimage.png?alt=media&#x26;token=4872553e-7f26-464f-9a58-afaa33dcd3db" alt=""><figcaption></figcaption></figure>

#### isUsingAsCollateralAny

* checks if user has been supplying ANY asset as collateral
* returns `FALSE` if user has NOT supplied ANY asset so far

{% hint style="info" %}
[`isUsingAsCollateralAny` breakdown](https://calnix.gitbook.io/aave-book/functions/supply/isfirstsupply/isusingascollateralone-isusingascollateralany)
{% endhint %}

If the user has never supplied any assets to date, (`isUsingAsCollateralAny` returns `FALSE`) `validateUseAsCollateral` will exit early and return `TRUE`.&#x20;

* Execution will then proceed to [`setUsingAsCollateral`](#setusingascollateral); this will engage in first-time user configuration setup.

{% hint style="success" %}
User cannot be in isolation mode if he has no supplied action to date.&#x20;
{% endhint %}

However, if `isUsingAsCollateralAny` is true; user has executed supply action sometime before.  This means, we must check if the user is currently in **isolation mode;** achieved by `getIsolationMode`.

* If he is, it could cause conflict with the new supply action.
* Additionally, there would be no need to execute `setUsingAsCollateral`, as this is not a first-time user.

### `getIsolationMode`

* if user is supplying only one type of collateral, get the debt ceiling of the collateral to check if its an isolated asset.
  * returns `TRUE`, along with value of debt ceiling
* If the user is supplying multiple types of collateral, clearly he is not in isolation mode, returns `FALSE`.

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FGMHnNMUHbonPwJFL30c1%2Fimage.png?alt=media&#x26;token=9f15e8e8-81ee-4d77-98c0-a3e5c03462c0" alt=""><figcaption></figcaption></figure>

#### Visual aid

<img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2F3co29b2D5z44Y1a8vkoV%2Ffile.excalidraw.svg?alt=media&#x26;token=12555288-6adc-45a0-82cb-623c5c7e0655" alt="" class="gitbook-drawing">

* isUsingCollateralOne: checks if the user has been supplying only 1 asset as collateral

#### return (!isolationMode && getDebtCeiling == 0)

`!isolationMode && getDebtCeiling == 0`:

* TRUE => there is no debt ceiling on incoming supplied asset AND the user is not in isolation mode.

## setUsingAsCollateral

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FHHlizYwTShnD0Ts67PBQ%2Fimage.png?alt=media&#x26;token=4af62707-4f6b-4e2d-aa62-3e6aeeac0641" alt=""><figcaption></figcaption></figure>

#### `(reserveIndex << 1) + 1`

* This bitwise operation doubles the index and increments it by 1
* Creates the necessary offset to manipulate the relevant bits to the asset of specified index
* For more in-depth explanation see: [isUsingCollateral](https://calnix.gitbook.io/aave-book/withdraw/collateral-check#example-check-if-the-user-has-been-using-the-reserve-at-index-2-as-collateral)

#### **`bit = 1 << offset`**

<img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FfviMzw0rnXmIT9M3onzT%2Ffile.excalidraw.svg?alt=media&#x26;token=f21abaf1-1fc8-4e77-9407-132030c5b9ad" alt="" class="gitbook-drawing">

If `usingAsCollateral` is **true**, indicating that the reserve should be marked as used for collateral,&#x20;

* bitwise OR operation between `data` and the `bit` value (`self.data |= bit`).&#x20;
* sets the corresponding bit in the `self.data` variable to `1`

If `usingAsCollateral` is **false**, indicating that the reserve should NOT be marked as collateral

* bitwise AND operation between `data` and the `bit` value (`self.data &= bit`).&#x20;
* sets the corresponding bit in the `self.data` variable to `0`
