Vault

Functions

receive

  function receive(
  ) external

only used for unwrapping weth in withdrawETH

initialize

  function initialize(
  ) external

setTrustedForwarder

  function setTrustedForwarder(
  ) external

setClearingHouse

  function setClearingHouse(
  ) external

setCollateralManager

  function setCollateralManager(
  ) external

setWETH9

  function setWETH9(
  ) external

deposit

  function deposit(
    address token,
    uint256 amount
  ) external

Deposit collateral into vault

Parameters:

depositFor

  function depositFor(
    address to,
    address token,
    uint256 amount
  ) external

Deposit the collateral token for other account

Parameters:

depositEther

  function depositEther(
  ) external

Deposit ETH as collateral into vault

depositEtherFor

  function depositEtherFor(
    address to
  ) external

Deposit ETH as collateral for specified account

Parameters:

withdraw

  function withdraw(
    address token,
    uint256 amount
  ) external

Withdraw collateral from vault

Parameters:

withdrawEther

  function withdrawEther(
    uint256 amount
  ) external

Withdraw ETH from vault

Parameters:

withdrawAll

  function withdrawAll(
    address token
  ) external returns (uint256 amount)

Withdraw all free collateral from vault

Parameters:

Return Values:

withdrawAllEther

  function withdrawAllEther(
  ) external returns (uint256 amount)

Withdraw all free collateral of ETH from vault

Return Values:

liquidateCollateral

  function liquidateCollateral(
    address trader,
    address token,
    uint256 amount,
    bool isDenominatedInSettlementToken
  ) external returns (uint256)

Liquidate trader's collateral by given settlement token amount or non settlement token amount

Parameters:

          the amount of non-settlement collateral token that the liquidator will charge from trader

|isDenominatedInSettlementToken | bool | Whether the amount is denominated in settlement token or not

Return Values:

    when `isDenominatedInSettlementToken` is true or the amount of settlement token that is repaid
    when `isDenominatedInSettlementToken` is false

getSettlementToken

  function getSettlementToken(
  ) external returns (address)

Get the settlement token address

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 Values:

decimals

  function decimals(
  ) external returns (uint8)

Get settlement token decimals

cached the settlement token's decimal for gas optimization

Return Values:

getTotalDebt

  function getTotalDebt(
  ) external returns (uint256)

(Deprecated) Get the borrowed settlement token amount from insurance fund

Return Values:

getClearingHouseConfig

  function getClearingHouseConfig(
  ) external returns (address)

Get ClearingHouseConfig contract address

Return Values:

getAccountBalance

  function getAccountBalance(
  ) external returns (address)

Get AccountBalance contract address

Return Values:

getInsuranceFund

  function getInsuranceFund(
  ) external returns (address)

Get InsuranceFund contract address

Return Values:

getExchange

  function getExchange(
  ) external returns (address)

Get Exchange contract address

Return Values:

getClearingHouse

  function getClearingHouse(
  ) external returns (address)

Get ClearingHouse contract address

Return Values:

getCollateralManager

  function getCollateralManager(
  ) external returns (address)

Get CollateralManager contract address

Return Values:

getWETH9

  function getWETH9(
  ) external returns (address)

Get WETH9 contract address

Return Values:

getFreeCollateral

  function getFreeCollateral(
    address trader
  ) external returns (uint256)

Get the free collateral value denominated in the settlement token of the specified trader

Parameters:

Return Values:

    for withdraw or opening new positions or orders)

getFreeCollateralByRatio

  function getFreeCollateralByRatio(
    address trader,
    uint24 ratio
  ) external returns (int256)

Get the free collateral amount of the specified trader and collateral ratio

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

Parameters:

Return Values:

    input margin requirement ratio; can be negative

getSettlementTokenValue

  function getSettlementTokenValue(
    address trader
  ) external returns (int256)

Get the specified trader's settlement value, including pending fee, funding payment, owed realized PnL and unrealized PnL

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)

Parameters:

Return Values:

getAccountValue

  function getAccountValue(
    address trader
  ) external returns (int256)

Get account value of the specified trader

Parameters:

Return Values:

getCollateralTokens

  function getCollateralTokens(
    address trader
  ) external returns (address[])

Get the array of collateral token addresses that a trader has in their account

Parameters:

Return Values:

getBalance

  function getBalance(
    address trader
  ) public returns (int256)

Get the specified trader's settlement token balance, without pending fee, funding payment and owed realized PnL

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

Parameters:

Return Values:

getBalanceByToken

  function getBalanceByToken(
    address trader,
    address token
  ) public returns (int256)

Get the balance of Vault of the specified collateral token and trader

Parameters:

Return Values:

getFreeCollateralByToken

  function getFreeCollateralByToken(
    address trader,
    address token
  ) public returns (uint256)

Get the free collateral amount of the specified collateral token of specified trader

getFreeCollateralByToken(token) = (getSettlementTokenValue() >= 0) ? min(getFreeCollateral() / indexPrice[token], getBalanceByToken(token)) : 0 if token is settlementToken, then indexPrice[token] = 1

Parameters:

Return Values:

isLiquidatable

  function isLiquidatable(
    address trader
  ) public returns (bool)

Parameters:

Return Values:

getMarginRequirementForCollateralLiquidation

  function getMarginRequirementForCollateralLiquidation(
    address trader
  ) public returns (int256)

get the margin requirement for collateral liquidation of a trader

this value is compared with ClearingHouse.getAccountValue() (int)

Parameters:

Return Values:

getCollateralMmRatio

  function getCollateralMmRatio(
  ) public returns (uint24)

Get the maintenance margin ratio for collateral liquidation

Return Values:

getRepaidSettlementByCollateral

  function getRepaidSettlementByCollateral(
    address token,
    uint256 collateral
  ) public returns (uint256 settlementX10_S)

Get a trader's repaid settlement amount by a given collateral amount

Parameters:

Return Values:

getLiquidatableCollateralBySettlement

  function getLiquidatableCollateralBySettlement(
    address token,
    uint256 settlementX10_S
  ) public returns (uint256 collateral)

Get a trader's liquidatable collateral amount by a given settlement amount

Parameters:

Return Values:

getMaxRepaidSettlementAndLiquidatableCollateral

  function getMaxRepaidSettlementAndLiquidatableCollateral(
    address trader,
    address token
  ) public returns (uint256 maxRepaidSettlementX10_S, uint256 maxLiquidatableCollateral)

Get a trader's max repaid settlement & max liquidatable collateral by a given collateral token

formula: maxRepaidSettlement = maxLiquidatableCollateral (indexTwap (1 - discountRatio)) maxLiquidatableCollateral = min(maxRepaidSettlement / (indexTwap * (1 - discountRatio)), getBalanceByToken(trader, token))

Parameters:

Return Values:

    the liquidator needs to pay to liquidate a trader's collateral token

|maxLiquidatableCollateral | uint256 | The maximum liquidatable collateral amount (in the collateral token's native decimals) of a trader

settleBadDebt

  function settleBadDebt(
    address trader
  ) public

Settle trader's bad debt

will only settle the bad debt when trader didn't have position and non-settlement collateral

Parameters:

Last updated