> For the complete documentation index, see [llms.txt](https://docs.nondollar.life/autonomint/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nondollar.life/autonomint/blockchain-docs/core-logics/treasury-logics.md).

# Treasury Logics

### Cumulative rate calculation

{% code title="Treasury.sol" %}

```solidity
function _calculateCumulativeRate(uint256 balanceBeforeEvent, Protocol _protocol)
```

{% endcode %}

| Param Name         | Type          | Description                                                     |
| ------------------ | ------------- | --------------------------------------------------------------- |
| balanceBeforeEvent | uint256       | Balance of external protocol tokens before deposit or withdraw. |
| \_protocol         | enum Protocol | Which external protocol cumulative rate needs to be calculated. |

```solidity
enum Protocol{Aave,Compound}
```

For the first time deposit, the cumulative rate will be 1.

```solidity
change = (balanceBeforeEvent - protocolDeposit[_protocol].totalCreditedTokens) / protocolDeposit[_protocol].totalCreditedTokens;
currentCumulativeRate = ((CUMULATIVE_PRECISION + change) * protocolDeposit[_protocol].cumulativeRate);
```

For further events, calculate the percentage change by dividing the tokens credited between last event and balance before event by total credited tokens till last event.

Then current cumulative rate will be previous cumulative rate multiplied with percentage change plus one.

{% hint style="info" %}
Reason for adding 1 to percentage change is, converting that into rate.
{% endhint %}

Functions which are using the above logic

* [Deposit to Aave.](/autonomint/blockchain-docs/core-contracts/treasury.md#deposittoaavebyuser)
* [Deposit to Compound.](/autonomint/blockchain-docs/core-contracts/treasury.md#deposittocompoundbyuser)
* [Withdraw from Aave.](/autonomint/blockchain-docs/core-contracts/treasury.md#withdrawfromaavebyuser)
* [Withdraw from Compound.](/autonomint/blockchain-docs/core-contracts/treasury.md#withdrawfromcompoundbyuser)
