# Mint debt token

## Overview

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FYLfyJEi2Nv99n8CBOZwd%2Fimage.png?alt=media&#x26;token=9b5204d3-c965-4245-9cc5-f00f42155f62" alt=""><figcaption></figcaption></figure>

* [x] <mark style="color:orange;">cache</mark>
* [x] <mark style="color:orange;">updateState</mark>
* [x] getIsolationMode
* [x] validateBorrow
* [ ] mint debt token
* [ ] setBorrowing&#x20;
* [ ] update IsolationMode debt
* [ ] <mark style="color:orange;">updateInterestRates</mark>
* [ ] transfer underlying to user

<img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2Fq60YikDCVwHg2AhRLNYR%2Ffile.excalidraw.svg?alt=media&#x26;token=0552d7aa-0ff0-4b6f-804e-cf9a5350ed27" alt="" class="gitbook-drawing">

* If stable interest was selected -> mint stable debt tokens&#x20;
* Else -> mint variable debt tokens

## mint stableDebtTokens

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2F7dvLzRQqEvySJJ8BVt2t%2Fimage.png?alt=media&#x26;token=5526ee4d-e45c-499a-ac3b-9244f7f8acc3" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
rate: reserve.currentStableBorrowRate
{% endhint %}

### \_calculateBalanceIncrease

* For a user, calculates the increase in stable debt, due to interest accrued from timestamp of their last update, to current time.
* Serves to update user's prior stable debt

{% hint style="info" %}
`balanceIncrease` reflects interest accrued from lastUpdate till current block.timestamp.&#x20;

Does not relate to incoming borrow action.
{% endhint %}

### totalSupply

```solidity
    vars.previousSupply = totalSupply();
    vars.currentAvgStableRate = _avgStableRate;
    vars.nextSupply = _totalSupply = vars.previousSupply + amount;
```

#### vars.previousSupply = totalSupply();

`totalSupply` returns the updated supply value, by accounting for recently accrued interest, from `_totalSupplyTimestamp` till now, based on the `_avgStableRate`.

* `_avgStableRate`: internal storage variable on StableDebtToken.sol
* `previousSupply`: updated supply value

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2Fw0wtFTxPniy7GogKzTIA%2Fimage.png?alt=media&#x26;token=ac9de6e6-23e4-4f13-a9d7-dcedd9f4a334" alt=""><figcaption></figcaption></figure>

* `principalSupply` => `super.totalSupply` calls `totalSupply` on `IncentivizedERC20` which returns `_totalSupply`

{% hint style="info" %}
`previousSupply` is set to `totalSupply()` instead of `_totalSupply` directly, so that the updated supply value can be obtained.&#x20;
{% endhint %}

With recent interest accounted for, the storage variable `_totalSupply` is updated with the incoming stable debt, `amount`.&#x20;

```solidity
vars.nextSupply = _totalSupply = 
    vars.previousSupply + amount;
```

### Calculate new stable rate

* `nextStableRate` is new stable rate, specific to the user

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FEqKgqKpFxKNAAQ4d7v5P%2Fimage.png?alt=media&#x26;token=d6700526-c75e-4c79-8f5a-875e8a66c065" alt=""><figcaption></figcaption></figure>

{% code overflow="wrap" %}

```solidity
nextStableRate = 
(_avgStableRate * currentBalance) + (amount * rate / currentBalance + amount)
```

{% endcode %}

* `nextStableRate` stored in the user's `UserState.additionalData` struct. `userState` mapping collects all users' `UserState` structs.
* Store update time in `_timestamps`.`_timestamps` mapping collects the last update timestamp for all users.
* Update the timestamp of the last update of  `_totalSupply.`

{% hint style="info" %}
rate: `reserve.currentStableBorrowRate`
{% endhint %}

### Update average Stable rate

* `_avgStableRate`: internal uint128 on StableDebtToken.sol
* `vars.currentAvgStableRate` was set to `_avgStableRate` at the start of `mint`

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FYQtuOBHVmnX5h4cAguP1%2Fimage.png?alt=media&#x26;token=193c1420-ddb8-4adf-ae80-ac9d7f6f226a" alt=""><figcaption></figcaption></figure>

{% code overflow="wrap" %}

```solidity
newAvgStableRate = 
(_avgStableRate * previousSupply) + [(rate * amountInRay) / nextSupply]
```

{% endcode %}

{% hint style="info" %}

* previousSupply: updated`totalSupply`
  {% endhint %}

### mint tokens

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FSTqHkyCzvUIIwuRFl7tj%2Fimage.png?alt=media&#x26;token=7c37a997-f30c-4887-b4d6-7b36f51f86b4" alt=""><figcaption></figcaption></figure>

Notice that we do not just mint as per the incoming stable debt amount; but also mint for `balanceIncrease`. This serves to update user's prior stable debt based on interest accrued over time elapsed since last update.

* Since we going to mint, mint to update the user's entire position

`mint` calls the internal function `_mint`:

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FY67gpz4MofGaXh9VBAwU%2Fimage.png?alt=media&#x26;token=63085c42-acc9-4f42-8a2d-e7e51a10585b" alt=""><figcaption></figcaption></figure>

## mint variableDebtTokens

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2FaPSkoedk56626NcbU4Ft%2Fimage.png?alt=media&#x26;token=00729fb5-8416-45b0-80d0-3008cca8f07c" alt=""><figcaption></figcaption></figure>

* `amountScaled`: scale the borrow amount against variableBorrowIndex
* See variableDebtToken [mint](https://calnix.gitbook.io/aave-book/contracts/variabledebttoken#mint)
