> 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/contracts/stabledebttoken/implementation-explained.md).

# Implementation explained

## Global currentStableBorrowRate and users' local borrow rate

There is a global stable rate determined by a model; each user accrues interest based on the stable rate they locked-in. How does this work?

### **Global stable rate**

Each asset in Aave has its configuration data contained within a struct **`ReserveData`**. This can be accessed via an internal mapping **`_reserves`**.

Held in storage, each `ReserveData`, contains a uint128 variable `currentStableBorrowRate`.&#x20;

* This is the stable rate enjoyed by incoming stable borrows, regardless of user.
* This global value is determined by the Utilization model we described above.

{% hint style="info" %}
**Held in storage, the ReserveData struct contains variable: `currentStableBorrowRate`**
{% endhint %}

### **User's stable rate**

Upon taking up a stable loan, the rate enjoyed by the user is stored in `_userState[address].additionalData`, as defined on the stableDebtToken contract.

<figure><img src="/files/u1gYBH9bztQem2YVKjfb" alt=""><figcaption><p>StableDebtToken is IncentivizedERC20</p></figcaption></figure>

* `.additionalData` is his weighted average stable rate, based on all his previous borrows.
* This is the rate at which interest compounds for the user.
* Each time a new stable borrow is taken, this is incremented:&#x20;
  * `(oldAmount * oldRate) + (newAmount * newRate) / totalAmount`

{% hint style="info" %}
**`_userState[address].additionalData`**` ``is updated within the`` `**`mint`**` ``function of stableDebtToken.`
{% endhint %}

### **\_avgStableRate**

An internal storage variable on the stable debt token contract, it is the weighted average rate across all the stable borrows taken to date, system-wide.&#x20;

Incremented like so:

**`_avgStableRate`**` ``= (currrentAvgStableRate * previousSupply) + (newRate * newAmount) / (_totalSupply() + newAmount)`

{% hint style="info" %}
Difference between **`_avgStableRate`**` ``and`` `**`currentStableBorrowRate`**

* **`_avgStableRate`**` ``reflects the average rate at which interest is being accrued by stable borrowers`
* **`currentStableBorrowRate`**` ``is the rate for the next incoming stable borrow as determined by the Utilization model.`
  {% endhint %}

### **`borrow & mint`**

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

`mint` returns the following two values which are stored in `reserveCache`:&#x20;

* `reserveCache.nextAvgStableBorrowRate` = `vars.currentAvgStableRate`&#x20;
* `reserveCache.nextTotalStableDebt` = `vars.nextSupply`

They are used later in **`calculateInterestRates`** to update system-wide rates.

### **updateInterestRates::calculateInterestRates**

After a loan is taken, **`calculateInterestRates`** is ran to update the system-wide rates:

* Liquidity rate
* Stable borrow rate
* Variable borrow rate

**`totalStableDebt`** used to calculate `totalDebt` and **`averageStableBorrowRate`** used to calculate `overallBorrowRate`, and consequently, `currentLiquidityRate`.

Stable borrow rate is updated, accounting for increase in stable loans taken; as are the other rates.&#x20;

{% hint style="success" %}
The updated stable rate is stored in **`reserve.currentStableBorrowRate - in storage.`**
{% endhint %}

### Visual Aid

<figure><img src="/files/44je4zlmizXkObPSJV2H" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/contracts/stabledebttoken/implementation-explained.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.
