# Treasury

## Write Functions

### deposit

```solidity
function deposit(
    address user,
    uint128 ethPrice,
    uint64 depositTime,
    IBorrowing.AssetName assetName,
    uint256 depositingAmount
) payable 
```

| Param Name       | Type                                                                                 | Description                      |
| ---------------- | ------------------------------------------------------------------------------------ | -------------------------------- |
| user             | address                                                                              | Address of the borrower.         |
| ethPrice         | uint128                                                                              | Current ETH price.               |
| depositTime      | uint64                                                                               | Depositing time.                 |
| depositingAmount | uint256                                                                              | Depositing amount by borrower.   |
| assetName        | [AssetName](/autonomint/blockchain-docs/core-contracts/borrowing.md#write-functions) | User depositing collateral type. |

In this function, the borrower's position data are stored and half of the deposited collateral is deposited in External protocol.

### withdraw

```solidity
function withdraw(
        address borrower,
        address toAddress,
        uint256 amount,
        uint64 index ) payable
```

| Param Name | Type    | Description                     |
| ---------- | ------- | ------------------------------- |
| borrower   | address | Borrower's address.             |
| toAddress  | address | Receiver's address.             |
| amount     | uint256 | Withdraw amount.                |
| index      | uint64  | Index pf the withdrew position. |

Transfers 50% of the deposited amount with call option gains.

### withdrawFromExternalProtocol

```solidity
function withdrawFromExternalProtocolDuringLiq(
    address user,
    uint64 index
)
```

| Param Name | Type    | Description                            |
| ---------- | ------- | -------------------------------------- |
| user       | address | User's address who is redeeming ABOND. |
| index      | uint64  | Index of the user.                     |

Withdraws the ETH backed with interest gained for the redeemable ABOND amount. Then the ABOND is burned from user.

### withdrawFromExternalProtocolDuringLiq

```solidity
function withdrawFromExternalProtocolDuringLiq(address user, uint128 aBondAmount)
```

| Param Name  | Type    | Description                            |
| ----------- | ------- | -------------------------------------- |
| user        | address | User's address who is redeeming ABOND. |
| aBondAmount | uint128 | ABOND amount to redeem.                |

Withdraws the ETH from External protocol i.e, the 50% of the deposited amount.

### approveTokens

```solidity
function approveTokens(
    IBorrowing.AssetName assetName,
    address spender,
    uint amount
)
```

| Param Name | Type                                                                               | Description                                        |
| ---------- | ---------------------------------------------------------------------------------- | -------------------------------------------------- |
| assetName  | [AssetName](/autonomint/blockchain-docs/core-contracts/borrowing.md#deposittokens) | Asset type, the treasury contract needs to approve |
| spender    | address                                                                            | Address of the spender.                            |
| amount     | uint256                                                                            | Amount of tokens to approve.                       |

Approves the asset to be spend by spender (mostly core contracts).

### \_calculateCumulativeRate

```solidity
function _calculateCumulativeRate(
    uint256 _currentExchangeRate,
    Protocol _protocol
) internal returns (uint256)
```

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

| Param Name            | Type     | Description                            |
| --------------------- | -------- | -------------------------------------- |
| \_currentExchangeRate | uint256  | Current ionWETH to WETH exchange rate. |
| \_protocol            | Protocol | External protocol name.                |

Calculates the cumulative rate for that protocol and store that.

### depositToIonicByUser

```solidity
function depositToIonicByUser(uint256 depositAmount) internal
```

| Param Name    | Type    | Description                            |
| ------------- | ------- | -------------------------------------- |
| depositAmount | uint256 | Collateral amount to deposit in Ionic. |

Deposits the collateral amount to Ionicprotocol. Updates the cumulative rate(to calculate interest) for Ionic protocol.

### depositToAaveByUser

```solidity
function depositToAaveByUser(uint256 depositAmount) internal
```

| Param Name    | Type    | Description                           |
| ------------- | ------- | ------------------------------------- |
| depositAmount | uint256 | Collateral amount to deposit in Aave. |

Deposits the collateral amount to Aave protocol. Updates the cumulative rate(to calculate interest) for Aave protocol.

### depositToCompoundByUser

```solidity
function depositToCompoundByUser(uint256 depositAmount) internal
```

| Param Name    | Type    | Description                               |
| ------------- | ------- | ----------------------------------------- |
| depositAmount | uint256 | Collateral amount to deposit in Compound. |

Deposits the collateral amount to Compound protocol. Updates the cumulative rate(to calculate interest) for Compound protocol.

### withdrawFromIonicByuser

```solidity
function withdrawFromIonicByUser(address user, uint128 aBondAmount)
```

| Param Name  | Type    | Description                         |
| ----------- | ------- | ----------------------------------- |
| user        | address | Address of the user.                |
| aBondAmount | uint128 | ABOND amount the user is redeeming. |

Withdraws the collateral with interest from Ionic and transfer it to user. The initial deposited amount and interest gained is calculated by ABOND amount, collateral backed, cumulative rate for that ABOND and current cumulative rate.&#x20;

### withdrawFromAaveByuser

```solidity
function withdrawFromAaveByUser(address user, uint128 aBondAmount)
```

| Param Name  | Type    | Description                         |
| ----------- | ------- | ----------------------------------- |
| user        | address | Address of the user.                |
| aBondAmount | uint128 | ABOND amount the user is redeeming. |

Withdraws the collateral with interest from Aave and transfer it to user. The initial deposited amount and interest gained is calculated by ABOND amount, collateral backed, cumulative rate for that ABOND and current cumulative rate.&#x20;

### withdrawFromCompoundByuser

```solidity
function withdrawFromCompoundByUser(address user, uint128 aBondAmount)
```

| Param Name  | Type    | Description                         |
| ----------- | ------- | ----------------------------------- |
| user        | address | Address of the user.                |
| aBondAmount | uint128 | ABOND amount the user is redeeming. |

Withdraws the collateral with interest from Compound and transfer it to user. The initial deposited amount and interest gained is calculated by ABOND amount, collateral backed, cumulative rate for that ABOND and current cumulative rate.

### withdrawFromIonicDuringLiq

```solidity
function withdrawFromIonicDuringLiq(address user, uint64 index)
```

| Param Name | Type    | Description                                                     |
| ---------- | ------- | --------------------------------------------------------------- |
| user       | address | Address of the user.                                            |
| index      | uint64  | Index of the borrower position which is going to be liquidated. |

Withdraws the collateral with interest from Ionic and kept it in treasury. The initial deposited amount and interest gained is calculated by cumulative rate during deposit and current cumulative rate.&#x20;

### withdrawInterest

```solidity
function withdrawInterest(address toAddress, uint256 amount)
```

| Param Name | Type    | Description                         |
| ---------- | ------- | ----------------------------------- |
| toAddress  | address | Receiver's address to get interest. |
| amount     | uint256 | Interest amount to withdraw.        |

Withdraws the interest gained in protocol by owner.

### transferEthToCdsLiquidators

```solidity
function transferEthToCdsLiquidators(address borrower, uint128 amount)
```

| Param Name | Type    | Description                                                        |
| ---------- | ------- | ------------------------------------------------------------------ |
| borrower   | address | Address of the CDS user who opted for liquidation to transfer ETH. |
| amount     | uint128 | Amount of ETH to transfer.                                         |

Gets USDa and Native token from destination chain to this chain treasury.This function can only be called by Borrowing or CDS contracts.

### transferFundsToGlobal

```solidity
function transferFundsToGlobal(
    uint256[4] memory transferAmounts
) external
```

| Param Name     | Type        | Description                                                                                                                                                    |
| -------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| transferAmount | uint256\[4] | Array of amounts needs to transfer to global contract in the order of [AssetName ](/autonomint/blockchain-docs/core-contracts/borrowing.md#deposittokens)enum. |

Transfers the respective amounts to the global contract.

### swapCollateralForUSDT

```solidity
function swapCollateralForUSDT(
    IBorrowing.AssetName asset,
    uint256 swapAmount,
    bytes memory odosAssembledData
) external
```

<table><thead><tr><th width="205">Param Name</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>asset</td><td><a href="/pages/dxfxIMA9T6U7krmdpR9S#deposittokens">AssetName</a></td><td>Collateral type to swap.</td></tr><tr><td>swapAmount</td><td>uint256</td><td>Amount of collateral to swap.</td></tr><tr><td>odosAssembledData</td><td>bytes</td><td>Odos assembled data got from Odos API for swapping.</td></tr></tbody></table>

Swaps the collateral to USDT.

### Setter Functions

#### setExternalProtocolAddresses

```solidity
function setExternalProtocolAddresses(
    address ionicWETHAddress,
    address wethAddress,
    address odosRouterV2Address
)
```

<table><thead><tr><th width="220">Param Name</th><th width="122">Type</th><th>Description</th></tr></thead><tbody><tr><td>ionicWETHAddress</td><td>address</td><td>ionic WETH contract address.</td></tr><tr><td>wethAddress</td><td>address</td><td>WETH address.</td></tr><tr><td>odosRouterV2Address</td><td>address</td><td>Odos router v2 address.</td></tr></tbody></table>

Sets the respective addresses, only called by owner.

## Read Functions

### calculateYieldsForExternalProtocol

```solidity
function calculateYieldsForExternalProtocol(address user, uint128 aBondAmount)
```

| Param Name  | Type    | Description                                 |
| ----------- | ------- | ------------------------------------------- |
| user        | address | Address of the user who is redeeming ABOND. |
| aBondAmount | uint128 | Amount of ABOND the user is redeeming.      |

Returns the yields acquired for that ABOND tokens to the user.

### getBorrowing

```solidity
function getBorrowing(address depositor,uint64 index)
```

| Param Name | Type    | Description            |
| ---------- | ------- | ---------------------- |
| depositor  | address | Borrower's address.    |
| index      | uint64  | Index of the borrower. |

Returns the borrower's total index and details of that particular index.

### getTotalDeposited

```solidity
function getTotalDeposited(
    address borrower
) external
```

| Param Name | Type    | Description         |
| ---------- | ------- | ------------------- |
| depositor  | address | Borrower's address. |

Returns the borrower's total deposited amount in ETH value.

### getCumulativeRate

```solidity
function getCumulativeRate(Protocol protocol)
```

Returns the last stored cumulative rate of the protocol.

### getCurrentCumulativeRate

```solidity
function getCurrentCumulativeRate(
    uint256 _currentExchangeRate, 
    Protocol _protocol
)
```

Returns the current cumulative rate of the protocol.

### getExternalProtocolCumulativeRate

```solidity
function getExternalProtocolCumulativeRate(bool maximum)
```

Returns the external protocol cumulative rate. If the param is true, returns the maximum cumulative rate, else vice versa.


---

# 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-contracts/treasury.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.
