# 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)


---

# Agent Instructions: 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://docs.nondollar.life/autonomint/blockchain-docs/core-logics/treasury-logics.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.
