// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.7.6; pragma abicoder v2; interface IVault { /// @notice Emitted when trader deposit collateral into vault /// @param collateralToken The address of token deposited /// @param trader The address of trader /// @param amount The amount of token deposited event Deposited(address indexed collateralToken, address indexed trader, uint256 amount); /// @notice Emitted when trader withdraw collateral from vault /// @param collateralToken The address of token withdrawn /// @param trader The address of trader /// @param amount The amount of token withdrawn event Withdrawn(address indexed collateralToken, address indexed trader, uint256 amount); /// @notice Emitted when a trader's collateral is liquidated /// @param trader The address of trader /// @param collateralToken The address of the token that is liquidated /// @param liquidator The address of liquidator /// @param collateral The amount of collateral token liquidated /// @param repaidSettlementWithoutInsuranceFundFeeX10_S The amount of settlement token repaid /// for trader (in settlement token's decimals) /// @param insuranceFundFeeX10_S The amount of insurance fund fee paid(in settlement token's decimals) /// @param discountRatio The discount ratio of liquidation price event CollateralLiquidated( address indexed trader, address indexed collateralToken, address indexed liquidator, uint256 collateral, uint256 repaidSettlementWithoutInsuranceFundFeeX10_S, uint256 insuranceFundFeeX10_S, uint24 discountRatio ); /// @notice Emitted when trustedForwarder is changed /// @dev trustedForwarder is only used for metaTx /// @param trustedForwarder The address of trustedForwarder event TrustedForwarderChanged(address indexed trustedForwarder); /// @notice Emitted when clearingHouse is changed /// @param clearingHouse The address of clearingHouse event ClearingHouseChanged(address indexed clearingHouse); /// @notice Emitted when collateralManager is changed /// @param collateralManager The address of collateralManager event CollateralManagerChanged(address indexed collateralManager); /// @notice Emitted when WETH9 is changed /// @param WETH9 The address of WETH9 event WETH9Changed(address indexed WETH9); /// @notice Emitted when bad debt realized and settled /// @param trader Address of the trader /// @param amount Absolute amount of bad debt event BadDebtSettled(address indexed trader, uint256 amount); /// @notice Deposit collateral into vault /// @param token The address of the token to deposit /// @param amount The amount of the token to deposit function deposit(address token, uint256 amount) external; /// @notice Deposit the collateral token for other account /// @param to The address of the account to deposit to /// @param token The address of collateral token /// @param amount The amount of the token to deposit function depositFor( address to, address token, uint256 amount ) external; /// @notice Deposit ETH as collateral into vault function depositEther() external payable; /// @notice Deposit ETH as collateral for specified account /// @param to The address of the account to deposit to function depositEtherFor(address to) external payable; /// @notice Withdraw collateral from vault /// @param token The address of the token to withdraw /// @param amount The amount of the token to withdraw function withdraw(address token, uint256 amount) external; /// @notice Withdraw ETH from vault /// @param amount The amount of the ETH to withdraw function withdrawEther(uint256 amount) external; /// @notice Withdraw all free collateral from vault /// @param token The address of the token to withdraw /// @return amount The amount of the token withdrawn function withdrawAll(address token) external returns (uint256 amount); /// @notice Withdraw all free collateral of ETH from vault /// @return amount The amount of ETH withdrawn function withdrawAllEther() external returns (uint256 amount); /// @notice Liquidate trader's collateral by given settlement token amount or non settlement token amount /// @param trader The address of trader that will be liquidated /// @param token The address of non settlement collateral token that the trader will be liquidated /// @param amount The amount of settlement token that the liquidator will repay for trader or /// the amount of non-settlement collateral token that the liquidator will charge from trader /// @param isDenominatedInSettlementToken Whether the amount is denominated in settlement token or not /// @return returnAmount The amount of a non-settlement token (in its native decimals) that is liquidated /// when `isDenominatedInSettlementToken` is true or the amount of settlement token that is repaid /// when `isDenominatedInSettlementToken` is false function liquidateCollateral( address trader, address token, uint256 amount, bool isDenominatedInSettlementToken ) external returns (uint256 returnAmount); /// @notice Settle trader's bad debt /// @param trader The address of trader that will be settled function settleBadDebt(address trader) external; /// @notice Get the specified trader's settlement token balance, without pending fee, funding payment /// and owed realized PnL /// @param trader The address of the trader /// @dev The function is equivalent to `getBalanceByToken(trader, settlementToken)` /// We keep this function solely for backward-compatibility with the older single-collateral system. /// In practical applications, the developer might want to use `getSettlementTokenValue()` instead /// because the latter includes pending fee, funding payment etc. /// and therefore more accurately reflects a trader's settlement (ex. USDC) balance /// @return balance The balance amount (in settlement token's decimals) function getBalance(address trader) external view returns (int256 balance); /// @notice Get the balance of Vault of the specified collateral token and trader /// @param trader The address of the trader /// @param token The address of the collateral token /// @return balance The balance amount (in its native decimals) function getBalanceByToken(address trader, address token) external view returns (int256 balance); /// @notice Get the array of collateral token addresses that a trader has in their account /// @param trader The address of the trader /// @return collateralTokens array of collateral token addresses function getCollateralTokens(address trader) external view returns (address[] memory collateralTokens); /// @notice Get account value of the specified trader /// @param trader The address of the trader /// @return accountValueX10_S account value (in settlement token's decimals) function getAccountValue(address trader) external view returns (int256 accountValueX10_S); /// @notice Get the free collateral value denominated in the settlement token of the specified trader /// @param trader The address of the trader /// @return freeCollateral the value (in settlement token's decimals) of free collateral available /// for withdraw or opening new positions or orders) function getFreeCollateral(address trader) external view returns (uint256 freeCollateral); /// @notice Get the free collateral amount of the specified trader and collateral ratio /// @dev There are three configurations for different insolvency risk tolerances: /// **conservative, moderate &aggressive**. We will start with the **conservative** one /// and gradually move to **aggressive** to increase capital efficiency /// @param trader The address of the trader /// @param ratio The margin requirement ratio, imRatio or mmRatio /// @return freeCollateralByRatio freeCollateral (in settlement token's decimals), by using the /// input margin requirement ratio; can be negative function getFreeCollateralByRatio(address trader, uint24 ratio) external view returns (int256 freeCollateralByRatio); /// @notice Get the free collateral amount of the specified collateral token of specified trader /// @param trader The address of the trader /// @param token The address of the collateral token /// @return freeCollateral amount of that token (in the token's native decimals) function getFreeCollateralByToken(address trader, address token) external view returns (uint256 freeCollateral); /// @notice Get the specified trader's settlement value, including pending fee, funding payment, /// owed realized PnL and unrealized PnL /// @dev Note the difference between `settlementTokenBalanceX10_S`, `getSettlementTokenValue()` and `getBalance()`: /// They are all settlement token balances but with or without /// pending fee, funding payment, owed realized PnL, unrealized PnL, respectively /// In practical applications, we use `getSettlementTokenValue()` to get the trader's debt (if < 0) /// @param trader The address of the trader /// @return balance The balance amount (in settlement token's decimals) function getSettlementTokenValue(address trader) external view returns (int256 balance); /// @notice Get the settlement token address /// @dev We assume the settlement token should match the denominator of the price oracle. /// i.e. if the settlement token is USDC, then the oracle should be priced in USD /// @return settlementToken The address of the settlement token function getSettlementToken() external view returns (address settlementToken); /// @notice Check if a given trader's collateral token can be liquidated; liquidation criteria: /// 1. margin ratio falls below maintenance threshold + 20bps (mmRatioBuffer) /// 2. USDC debt > nonSettlementTokenValue * debtNonSettlementTokenValueRatio (ex: 75%) /// 3. USDC debt > debtThreshold (ex: $10000) // USDC debt = USDC balance + Total Unrealized PnL /// @param trader The address of the trader /// @return isLiquidatable If the trader can be liquidated function isLiquidatable(address trader) external view returns (bool isLiquidatable); /// @notice get the margin requirement for collateral liquidation of a trader /// @dev this value is compared with `ClearingHouse.getAccountValue()` (int) /// @param trader The address of the trader /// @return marginRequirement margin requirement (in 18 decimals) function getMarginRequirementForCollateralLiquidation(address trader) external view returns (int256 marginRequirement); /// @notice Get the maintenance margin ratio for collateral liquidation /// @return collateralMmRatio The maintenance margin ratio for collateral liquidation function getCollateralMmRatio() external view returns (uint24 collateralMmRatio); /// @notice Get a trader's liquidatable collateral amount by a given settlement amount /// @param token The address of the token of the trader's collateral /// @param settlementX10_S The amount of settlement token the liquidator wants to pay /// @return collateral The collateral amount(in its native decimals) the liquidator can get function getLiquidatableCollateralBySettlement(address token, uint256 settlementX10_S) external view returns (uint256 collateral); /// @notice Get a trader's repaid settlement amount by a given collateral amount /// @param token The address of the token of the trader's collateral /// @param collateral The amount of collateral token the liquidator wants to get /// @return settlementX10_S The settlement amount(in settlement token's decimals) the liquidator needs to pay function getRepaidSettlementByCollateral(address token, uint256 collateral) external view returns (uint256 settlementX10_S); /// @notice Get a trader's max repaid settlement & max liquidatable collateral by a given collateral token /// @param trader The address of the trader /// @param token The address of the token of the trader's collateral /// @return maxRepaidSettlementX10_S The maximum settlement amount(in settlement token's decimals) /// the liquidator needs to pay to liquidate a trader's collateral token /// @return maxLiquidatableCollateral The maximum liquidatable collateral amount /// (in the collateral token's native decimals) of a trader function getMaxRepaidSettlementAndLiquidatableCollateral(address trader, address token) external view returns (uint256 maxRepaidSettlementX10_S, uint256 maxLiquidatableCollateral); /// @notice Get settlement token decimals /// @dev cached the settlement token's decimal for gas optimization /// @return decimals The decimals of settlement token function decimals() external view returns (uint8 decimals); /// @notice (Deprecated) Get the borrowed settlement token amount from insurance fund /// @return debtAmount The debt amount (in settlement token's decimals) function getTotalDebt() external view returns (uint256 debtAmount); /// @notice Get `ClearingHouseConfig` contract address /// @return clearingHouseConfig The address of `ClearingHouseConfig` contract function getClearingHouseConfig() external view returns (address clearingHouseConfig); /// @notice Get `AccountBalance` contract address /// @return accountBalance The address of `AccountBalance` contract function getAccountBalance() external view returns (address accountBalance); /// @notice Get `InsuranceFund` contract address /// @return insuranceFund The address of `InsuranceFund` contract function getInsuranceFund() external view returns (address insuranceFund); /// @notice Get `Exchange` contract address /// @return exchange The address of `Exchange` contract function getExchange() external view returns (address exchange); /// @notice Get `ClearingHouse` contract address /// @return clearingHouse The address of `ClearingHouse` contract function getClearingHouse() external view returns (address clearingHouse); /// @notice Get `CollateralManager` contract address /// @return clearingHouse The address of `CollateralManager` contract function getCollateralManager() external view returns (address clearingHouse); /// @notice Get `WETH9` contract address /// @return clearingHouse The address of `WETH9` contract function getWETH9() external view returns (address clearingHouse); }