# collateral check

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

{% hint style="info" %}
Interest rates are updated to account for the withdrawal of deposits, and therefore the reduction of supply. See [.updateInterestRates](https://calnix.gitbook.io/aave-book/functions/common-functions/.updateinterestrates)
{% endhint %}

<img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FV6PIcSCnOOiV97Trshyc%2Ffile.excalidraw.svg?alt=media&#x26;token=fa5ec747-0e47-485c-b840-7efa14d7ea75" alt="" class="gitbook-drawing">

## isUsingCollateral

* Transaction reverts on invalid `reserveIndex`

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FGExYveKJZC1We2dJY5Av%2Fimage.png?alt=media&#x26;token=fa74edee-82de-4899-8869-3344aac66776" alt=""><figcaption></figcaption></figure>

Let's examine the bitwise manipulations with an example. In this example, we assume six bits for simplicity, w\.r.t to `self.data`

#### **Example: Check if the user has been using the reserve at index 2 as collateral:**

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FTeCyAWqKghxuTn54GIhc%2Fimage.png?alt=media&#x26;token=c00b8d1d-02f9-4710-b757-36721f1e81f7" alt=""><figcaption></figcaption></figure>

It the result of the bitwise operations is not equal to 0, the function would return `true`. This indicates that the user has been using the reserve at `reserveIndex = 2` as collateral.

## setUsingAsCollateral

Set the collateral bit for a specific asset to either `0` or `1`, depending on bool `usingAsCollateral`.

<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](#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`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnix.gitbook.io/aave-book/functions/withdraw/collateral-check.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
