> 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/common-functions/.updatestate.md).

# .updateState

## TLDR

The updateState function serves to update the interest and indexes within the system for a specific asset.

### Code

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

If state has been updated within the same block in a previous transaction, do nothing. \
Else this function does two things:

1. **\_updateIndexes** => updates Indexes: `nextLiquidityIndex`, `nextVariableBorrowIndex`
2. **\_accrueToTreasury** => updates Treasury accrual&#x20;
3. updates **`reserve.lastUpdateTimestamp`** to current block.timestamp

{% hint style="info" %}
On time

* `.cache` assigns **`reserveCache.reserveLastUpdateTimestamp`** = `reserve.lastUpdateTimestamp`
* `.updateState` updates **`reserve.lastUpdateTimestamp`** to latest
  {% endhint %}

## &#x20;`_updateIndexes`&#x20;

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

Updates `nextliquidityIndex` by the way of `calculateLinearInterest`, and updates `nextVariableBorrowIndex` by the way of `calculateCompoundInterest`.

* supply interest is accrued via linear interest (simple interest)
* borrow interest is accrued via compound interest&#x20;

### `calculateLinearInterest`

Calculates linear interest accrued from `lastUpdateTimeStamp` till current `block.timestamp`.

<figure><img src="/files/0Uf0MjgfT6ekmvl9Kgrc" alt=""><figcaption></figcaption></figure>

* serves to update deposit interest and liquidity index
* explanation: [deposit-interest](/aave-book/deposit-and-borrow-interest.md#deposit-interest)

### `calculateCompoundInterest`

Calculates interest accrued from `lastUpdateTimeStamp` till current `block.timestamp`.&#x20;

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

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

* interest is compounded every second&#x20;
* serves to update variable borrow interest and variable borrow index
* explanation: [borrow-interest](/aave-book/deposit-and-borrow-interest.md#borrow-interest)

## **\_accrueToTreasury**&#x20;

A portion of repaid borrow interest goes to Aave's treasury; the rest is paid out to suppliers. This portion is determined by the reserve factor: : % of borrow rate that goes to Treasury.

* `reserve.accruedToTreasury` represents the treasury's cut of repaid interest to date.
* `_accrueToTreasury` serves to increment this value, by accounting for unbooked interest.
* Unbooked interest: interest accrued from the last update till now.
* Last update: `reserveCache.reserveLastUpdateTimestamp`&#x20;

{% hint style="info" %}

* Treasury accrues funds in aTokens.
* While `accruedToTreasury` is incremented, the tokens are not minted; that happens when `mintToTreasury` is called.
  {% endhint %}

<figure><img src="/files/4TeGhzdZhZib7efg1QmT" alt=""><figcaption></figcaption></figure>

In a nutshell, the function calculates the "new debt" accumulated from `lastUpdateTimestamp`  till now, and applies the reserve factor upon - accounting for the treasury's portion.&#x20;

* "new debt" comprises of both variable and stable components&#x20;

### Unbooked variable debt&#x20;

For variable debt, the increase is accounted for by the delta between curr and next indexes:

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

* `nextVariableBorrowIndex` will account for interest accrued up till now
* `currVariableBorrowIndex` will account for interest accrued up till `lastUpdateTimestamp` &#x20;

Essentially,

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

**The difference gives us the delta of variable debt since `lastUpdateTimestamp`.**

### Unbooked stable debt&#x20;

The approach here has to be different simply because there is no index for stable debt.

First, let's isolate the sections of code for calculating unbooked stable debt.&#x20;

{% code title="calculated in .cache:" %}

```solidity
// from getSupplyData():
reserveCache.currTotalStableDebt = _calcTotalSupply(avgRate)
reserveCache.stableDebtLastUpdateTimestamp = _totalSupplyTimestamp
```

{% endcode %}

{% code title="from \_accrueToTreasury:" %}

```solidity
//calculate the stable debt from stableDebtLastUpdateTimestamp 
//until reserveLastUpdateTimestamp
vars.cumulatedStableInterest = MathUtils.calculateCompoundedInterest(
  reserveCache.currAvgStableBorrowRate,
  reserveCache.stableDebtLastUpdateTimestamp,
  reserveCache.reserveLastUpdateTimestamp
);

//prevTotalStableDebt = currPrincipalStableDebt * cumulatedStableInterest
vars.prevTotalStableDebt = 
reserveCache.currPrincipalStableDebt.rayMul(vars.cumulatedStableInterest);

// to find unbooked component
currTotalStableDebt - prevTotalStableDebt
```

{% endcode %}

**`currTotalStableDebt`** - **`prevTotalStableDebt`** gives us interest accrued from `reserveLastUpdateTimestamp` till now.

* **`currTotalStableDebt:`** accounts for interest from `_totalSupplyTimestamp` till now
* **`prevTotalStableDebt:`** accounts for interest from `stableDebtLastUpdateTimestamp` to `reserveLastUpdateTimestamp`

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

{% hint style="info" %}
`stableDebtLastUpdateTimestamp == _totalSupplyTimestamp`

* assigned via [`getSupplyData`](/aave-book/contracts/stabledebttoken.md#getsupplydata) in [`.cache`](/aave-book/functions/common-functions/.cache.md)
* `_totalSupplyTimestamp is updated during` [`mint`](/aave-book/contracts/stabledebttoken.md#mint)`/burn`
  {% endhint %}

**Why can't we simply calculate interest from `reserveLastUpdateTimestamp`, similar to variable debt?**

* `reserveLastUpdateTimestamp` **>=** `_totalSupplyTimestamp`
* **`_totalSupplyTimestamp`** is only updated when mint/burn of the stableDebtToken is called.&#x20;
* **`reserveLastUpdateTimestamp`** is updated in `.updateState`, which is called in every state-changing function.

Given how often that that happens,**`reserveLastUpdateTimestamp`** is updated far more frequently, while **`_totalSupplyTimestamp`** is likely to be 'stale'.&#x20;

{% hint style="warning" %}
Why ignore stable debt accrued from **`_totalSupplyTimestamp`**` ``to`` `**`reserveLastUpdateTimestamp` ?**
{% endhint %}

### **Putting it together**&#x20;

* Paid to treasury = `amountToMint` =`totalDebtAccrued * reserveFactor`

If `amountToMint` is a non-zero value, `reserve.accruedToTreasury` is incremented by: `amountToMint / nextLiquidityIndex`&#x20;

This scales against the most recent index; similar to how deposits are scaled.

## Visual Aid

<img src="/files/osS435fQzQ6HnfQTcknU" alt="" class="gitbook-drawing">
