# Cleanup + Collect repay

## Overview

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FqTGBPdLfAjlG766dn75y%2Fimage.png?alt=media&#x26;token=8e5feaab-a128-48f8-bb29-5f7323ea032c" alt=""><figcaption></figcaption></figure>

* [x] <mark style="color:orange;">cache + updateState</mark>
* [x] get current debt&#x20;
* [x] validateRepay
* [x] get paybackAmount
* [x] burn debt tokens
* [x] <mark style="color:orange;">updateInterestRates</mark>
* [ ] **setBorrowing**
* [ ] **updateIsolatedDebtIfIsolated**
* [ ] **handle repayment**

We will cover the last three components in this section.

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FYlWA6VZJy2di2z3x0neA%2Fimage.png?alt=media&#x26;token=efaf87f5-acc0-498b-9608-7eaf69a822f7" alt=""><figcaption></figcaption></figure>

### setBorrowing

If there is no outstanding debt left, within userConfig, set the borrowing flag for the asset to `0`.

* For function's inner workings, see: [setBorrowing](https://calnix.gitbook.io/aave-book/borrow/setborrowing#setborrowing) under the borrow section.

### **updateIsolatedDebtIfIsolated**

In the event that the debt position was collateralized by an isolated asset, and a repay or liquidation occurred

* total debt of the isolated asset must be decremented (`isolationModeTotalDebt`)
* [getisolationModeState](https://calnix.gitbook.io/aave-book/borrow/getisolationmodestate#getisolationmodestate) was previously explained in the borrow section

<img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FnBTaBDu9wjxTFthed8qk%2Ffile.excalidraw.svg?alt=media&#x26;token=f84521f4-47e8-42b6-9b70-feb5a7b8e326" alt="" class="gitbook-drawing">

**If user is in isolation mode:**

* get total isolated debt from `reservesData` (`isolationModeTotalDebt`)
* rebase the decimals of `paybackAmount`

**`if isolationModeTotalDebt < repayAmount`**

* reset total isolated debt on asset to `0`
* Else: decrement total isolated debt by `repayAmount`

## Collect repay

Finally, the repayment is collected from the function caller.&#x20;

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FaA0XLW6nLCmx1Osx8kF1%2Fimage.png?alt=media&#x26;token=e9343289-e15d-41db-a7de-f4abcb9a7e36" alt=""><figcaption></figcaption></figure>

**If `useATokens` was set to `true`, like in `repayWithATokens`, Atokens would be burnt.**&#x20;

Exampl&#x65;*:* User has DAI debt and also holds aDAI token

* can use aDAI to repay DAI debt in single transaction without any approvals or having to withdraw their supplied liquidity to the pool using `repayWithATokens` feature
* aDAI tokens would be burnt, reducing his claim on supplied assets.
* See withdraw section for [AToken.burn](https://calnix.gitbook.io/aave-book/functions/withdraw/burn-atokens)

**Else, `useATokens` is set to `false`, in the case of `repay`;**&#x20;

* asset is pulled from user via `safeTransferFrom`
* example: borrowed USDC, repaying USDC: principal + plus interest
