# CryndexTreasury.sol

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

struct InterestRate {
    uint112 rate;
    uint256 timestamp;
}

interface ICryndexTreasury {
    /// @notice Emitted when a new accepted collateral is added
    /// @param token The address of the collateral token
    /// @param aggregator The address of the price aggregator for the collateral
    /// @param liquidationThreshold The threshold (in percentage) for liquidation
    /// @param liquidationBonus The bonus (in percentage) awarded during liquidation
    event AcceptedCollateralAdded(
        address token,
        address aggregator,
        uint256 liquidationThreshold,
        uint256 liquidationBonus
    );

    /// @notice Emitted when an accepted collateral token is disabled
    /// @param token The address of the collateral token
    event AcceptedCollateralDisabled(address token);

    /// @notice Emitted when a flashloan is executed
    /// @param receiver The address receiving the flashloan
    /// @param tokens The token addresses to flashloan
    /// @param amounts The amount of tokens loaned
    /// @param fees The fee charged for each token the flashloan
    /// @param timestamp The block timestamp of the flashloan event
    event FlashLoanExecuted(
        address receiver,
        address[] tokens,
        uint256[] amounts,
        uint112[] fees,
        uint256 timestamp
    );

    /// @notice Emitted when a deposit is made
    /// @param depositer The account making the deposit
    /// @param account The account credited with the deposit
    /// @param token The token being deposited
    /// @param amount The amount of tokens deposited
    /// @param timestamp The block timestamp of the deposit event
    event Deposit(
        address depositer,
        address account,
        address token,
        uint256 amount,
        uint256 timestamp
    );

    /// @notice Emitted when a withdrawal is made
    /// @param account The account withdrawing the tokens
    /// @param receiver The account receiving the withdrawn tokens
    /// @param token The token being withdrawn
    /// @param amount The amount of tokens withdrawn
    /// @param fee The fee charged for withdrawing tokens
    /// @param timestamp The block timestamp of the withdrawal event
    event Withdrawal(
        address account,
        address receiver,
        address token,
        uint256 amount,
        uint256 fee,
        uint256 timestamp
    );

    /// @notice Emitted when an asset is borrowed
    /// @param borrower The account borrowing the asset
    /// @param receiver The account who will receive the borrowed tokens
    /// @param asset The asset being borrowed
    /// @param amount The amount of the tokens borrowed
    /// @param timestamp The block timestamp of the event
    event Borrow(
        address borrower,
        address receiver,
        address asset,
        uint256 amount,
        uint256 timestamp
    );

    /// @notice Emitted when an asset is repaid
    /// @param borrower The account credited with the repayment
    /// @param repayer The account that is repaying the debt
    /// @param asset The asset being repaid
    /// @param amount The amount of the asset repaid
    /// @param timestamp The block timestamp of the burning event
    event Repayment(
        address borrower,
        address repayer,
        address asset,
        uint256 amount,
        uint256 timestamp
    );

    /// @notice Emitted when a borrower is liquidated
    /// @param liquidator The account liquidating the borrower
    /// @param token The collateral token seized
    /// @param asset The asset being repaid
    /// @param debtCovered The amount of debt repaid
    /// @param collateralSeized The amount of collateral tokens seized
    /// @param borrower The account that is being liquidated
    event Liquidation(
        address liquidator,
        address token,
        address asset,
        uint256 debtCovered,
        uint256 collateralSeized,
        address borrower
    );

    /// @notice Returns all accepted collateral tokens
    /// @return Array of token addresses
    function getAcceptedCollateral() external view returns (address[] memory);

    /// @notice Returns the address of the savings contract
    /// @return The savings contract address
    function getSavings() external view returns (address);

    /// @notice Adds token that can be accepted as collateral
    /// @param token Address of the collateral
    /// @param aggregator Aggregator address for price feed
    /// @param threshold The initial liquidation threshold
    /// @param bonus The initial liquidation bonus
    function addAcceptedCollateral(
        address token,
        address aggregator,
        uint112 threshold,
        uint112 bonus
    ) external;

    /// @notice Disables token that can be accepted as collateral
    /// @param token Address of the collateral
    function disableAcceptedCollateral(address token) external;

    /// @notice Gets the latest price for a token from its Chainlink aggregator
    /// @param token The address of the token
    /// @return The normalized price in TARGET_PRECISION (18) decimals
    function latestAnswer(address token) external view returns (uint256);

    /// @notice Gets the decimal places of a token's price feed
    /// @param token The address of the token
    /// @return The number of decimal places in the token’s price feed
    // function decimals(address token) external view returns (uint8);

    /// @notice Gets the borrow rate for a asset token at a specific index
    /// @param asset The address of the asset token
    /// @param idx The index in the borrow rates array
    /// @return The borrow rate and its timestamp as an InterestRate struct
    function getBorrowRate(
        address asset,
        uint256 idx
    ) external view returns (InterestRate memory);

    /// @notice Gets the current borrow rate for an asset token
    /// @param asset The address of the asset token
    /// @return rate The current borrow rate and its timestamp as an InterestRate struct
    /// @return idx The index of the current borrow rate
    function getCurrentBorrowRate(
        address asset
    ) external view returns (InterestRate memory rate, uint256 idx);

    /// @notice Sets the borrow rate for an asset
    /// @param asset Address of the asset
    /// @param rate New borrow rate value
    /// @return Updated borrow rate
    function setBorrowRate(
        address asset,
        uint112 rate
    ) external returns (InterestRate memory);

    /// @notice Gets the liquidation threshold for a specific token
    /// @param token The address of the token
    /// @return The liquidation threshold value
    function getLiquidationThreshold(
        address token
    ) external view returns (uint112);

    /// @notice Sets the liquidation threshold for a token, only callable by the governor
    /// @param token The address of the token
    /// @param threshold The new liquidation threshold value
    /// @return The updated liquidation threshold value
    function setLiquidationThreshold(
        address token,
        uint112 threshold
    ) external returns (uint112);

    /// @notice Gets the liquidation bonus for a specific token
    /// @param token The address of the token
    /// @return The liquidation bonus value
    function getLiquidationBonus(address token) external view returns (uint112);

    /// @notice Sets the liquidation bonus for a token, only callable by the governor
    /// @param token The address of the token
    /// @param bonus The new liquidation bonus value
    /// @return The updated liquidation bonus value
    function setLiquidationBonus(
        address token,
        uint112 bonus
    ) external returns (uint112);

    /// @notice Allows an account to deposit ERC20 or native tokens into the treasury
    /// @param account The account to be credited with the deposit
    /// @param token Address of the token to deposit
    /// @param amount Amount of tokens to deposit
    function deposit(
        address account,
        address token,
        uint256 amount
    ) external payable;

    /// @notice Withdraws an ERC20 token stored in the treasury to an address
    /// @param account The account to receive the withdrawn tokens
    /// @param token Address of the ERC20 token
    /// @param amount Amount to withdraw
    function withdraw(address account, address token, uint256 amount) external;

    /// @notice Borrows new asset tokens
    /// @param account The account to receive the borrowed tokens
    /// @param asset Address of the asset to be borrowed
    /// @param amount Amount of tokens to be borrowed
    function borrow(address account, address asset, uint256 amount) external;

    /// @notice Repays the asset and burns the tokens
    /// @param account The account who's debt will be repaid
    /// @param asset Address of the asset to be repaid
    /// @param amount Amount of tokens to be repaid
    function repay(address account, address asset, uint256 amount) external;

    /// @notice Computes liquidation parameters and updated debt position, then applies state changes
    /// @param token Collateral token to seize
    /// @param asset Debt asset to repay
    /// @param debtToCover Amount of asset to repay
    /// @param account Account to liquidate
    /// @return Amount Liquidated
    function liquidate(
        address token,
        address asset,
        uint256 debtToCover,
        address account
    ) external returns (uint256);

    /// @notice Returns encoded debt position + interest for a borrower and collateral
    /// @param account Address of the account
    /// @param asset Address of the debt asset
    /// @return Encoded debt position for a given asset
    function getDebtBalance(
        address account,
        address asset
    ) external returns (uint256);

    /// @notice Gets the collateral balance of a borrower
    /// @param borrower The address of the borrower
    /// @param token The address of the token
    /// @return Collateral balance of the token held by the borrower
    function getCollateralBalance(
        address borrower,
        address token
    ) external view returns (uint256);

    /// @notice Returns how much collateral is held in reserves
    /// @param token The address of the token
    /// @return The amount of tokens held in the treasury
    function getTotalReservesAmount(
        address token
    ) external view returns (uint256);

    /// @notice Calculates how much debt is owed to the treasury for a given asset
    /// @param asset The address of the asset
    /// @return The total debt value
    function getTotalDebtAmount(address asset) external view returns (uint256);

    /// @notice Set a factory role
    /// @param factory The factory address that can create assets
    function addFactory(address factory) external;

    /// @notice Adds an asset that can be borrowed from the protocol
    /// @param asset The address of the asset to add
    function addAsset(address asset, uint112 rate) external;

    /// @notice Calculates current health factor for a borrower
    /// @param borrower Borrower address
    /// @return Health factor
    function calculateHealthFactor(
        address borrower
    ) external view returns (uint256);

    /// @notice Executes a flash loan
    /// @param receiver The address of the flash loan receiver contract
    /// @param tokens The tokens to loan
    /// @param amounts The amounts of each token to loan (in wei)
    /// @param params Additional parameters passed to the receiver
    function flashLoan(
        address receiver,
        address[] memory tokens,
        uint256[] memory amounts,
        bytes calldata params
    ) external;
}

```
