> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/aave-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/aave-book/functions/borrow/mint-debt-token.md).

# Mint debt token

## Overview

<figure><img src="/files/bOM7Wa0B8H5OSNJ1WaMd" 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="/files/eOPuJQL0SCm6nXrBtWbU" alt="" class="gitbook-drawing">

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

## mint stableDebtTokens

<figure><img src="/files/2kpw4UI5oHcDc1MZusTJ" 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="/files/aH5xOi0hc2eupG9730dk" 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="/files/MxSPbrovmWry6kKkeXjh" 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="/files/4ScYUjoTJobw7n9J3qyT" 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="/files/vLaOTQyW47kRxBrtUism" 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="/files/z5hPAUmEDkpiVPzaB5Gr" alt=""><figcaption></figcaption></figure>

## mint variableDebtTokens

<figure><img src="/files/lMZx92SfVUqj89Qq7f2g" alt=""><figcaption></figcaption></figure>

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