Perp v2
Perp v2
  • About Perpetual Protocol
    • Social Links
    • Governance
    • v2 Era Ecosystem
    • PERP Token
  • Terms of Service
  • General
    • Wallets
    • Transfering Assets
    • Deposit & Withdrawal
    • Perpetual + Uniswap
    • FAQs
    • Guides
    • Legacy Reward Programs
    • Security
  • Hot Tub
  • Trading on Perp v2
    • How It Works
    • UI Overview
    • Perpetual Futures Basics
    • Trading Quick-start
    • Opening and Closing Positions
    • Account Value & More
    • Funding Payments
    • Order Types
    • Trading Fees and Gas Fees
    • Managing Risk
    • Multi-collateral
    • More docs
  • Providing Liquidity
    • Basics
    • Introduction to Maker UI
    • Add / Remove Liquidity
    • Estimated Fees and Rewards APR
    • More on LPing
    • Tools for LPs
  • Perp v2 Specs
  • Developer Docs
    • Developer FAQs
    • Source Code
    • Protocol Attributes
    • Contract guide
      • AccountBalance
      • BaseToken
      • ClearingHouse
      • CollateralManager
      • DelegateApproval
      • Exchange
      • InsuranceFund
      • MarketRegistry
      • OrderBook
      • QuoteToken
      • Vault
      • VirtualToken
    • Interface Contracts
      • IAccountBalance
      • IBaseToken
      • IClearingHouse
      • IClearingHouseConfig
      • IClearingHouseConfigEvent
      • ICollateralManager
      • IDelegateApproval
      • IERC20Metadata
      • IExchange
      • IIndexPrice
      • IInsuranceFund
      • IMarketRegistry
      • IOrderBook
      • IVault
      • IVirtualToken
    • Integration guide
    • Dev tools
Powered by GitBook
On this page

Was this helpful?

  1. Developer Docs
  2. Contract guide

Vault

Last updated 1 year ago

Was this helpful?

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
  function setWETH9(
  ) external
  function deposit(
    address token,
    uint256 amount
  ) external

Deposit collateral into vault

Name
Type
Description

token

address

The address of the token to deposit

amount

uint256

The amount of the token to deposit

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

Deposit the collateral token for other account

Name
Type
Description

to

address

The address of the account to deposit to

token

address

The address of collateral token

amount

uint256

The amount of the token to deposit

  function depositEther(
  ) external

Deposit ETH as collateral into vault

  function depositEtherFor(
    address to
  ) external

Deposit ETH as collateral for specified account

Name
Type
Description

to

address

The address of the account to deposit to

  function withdraw(
    address token,
    uint256 amount
  ) external

Withdraw collateral from vault

Name
Type
Description

token

address

The address of the token to withdraw

amount

uint256

The amount of the token to withdraw

  function withdrawEther(
    uint256 amount
  ) external

Withdraw ETH from vault

Name
Type
Description

amount

uint256

The amount of the ETH to withdraw

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

Withdraw all free collateral from vault

Name
Type
Description

token

address

The address of the token to withdraw

Name
Type
Description

amount

uint256

The amount of the token withdrawn

  function withdrawAllEther(
  ) external returns (uint256 amount)

Withdraw all free collateral of ETH from vault

Name
Type
Description

amount

uint256

The amount of ETH withdrawn

  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

Name
Type
Description

trader

address

The address of trader that will be liquidated

token

address

The address of non settlement collateral token that the trader will be liquidated

amount

uint256

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

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

Name
Type
Description

returnAmount

uint256

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

Name
Type
Description

settlementToken

address

The address of the settlement token

  function decimals(
  ) external returns (uint8)

Get settlement token decimals

cached the settlement token's decimal for gas optimization

Name
Type
Description

decimals

uint8

The decimals of settlement token

  function getTotalDebt(
  ) external returns (uint256)

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

Name
Type
Description

debtAmount

uint256

The debt amount (in settlement token's decimals)

  function getClearingHouseConfig(
  ) external returns (address)

Get ClearingHouseConfig contract address

Name
Type
Description

clearingHouseConfig

address

The address of ClearingHouseConfig contract

  function getAccountBalance(
  ) external returns (address)

Get AccountBalance contract address

Name
Type
Description

accountBalance

address

The address of AccountBalance contract

  function getInsuranceFund(
  ) external returns (address)

Get InsuranceFund contract address

Name
Type
Description

insuranceFund

address

The address of InsuranceFund contract

  function getExchange(
  ) external returns (address)

Get Exchange contract address

Name
Type
Description

exchange

address

The address of Exchange contract

  function getClearingHouse(
  ) external returns (address)

Get ClearingHouse contract address

Name
Type
Description

clearingHouse

address

The address of ClearingHouse contract

  function getCollateralManager(
  ) external returns (address)

Get CollateralManager contract address

Name
Type
Description

clearingHouse

address

The address of CollateralManager contract

  function getWETH9(
  ) external returns (address)

Get WETH9 contract address

Name
Type
Description

clearingHouse

address

The address of WETH9 contract

  function getFreeCollateral(
    address trader
  ) external returns (uint256)

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

Name
Type
Description

trader

address

The address of the trader

Name
Type
Description

freeCollateral

uint256

the value (in settlement token's decimals) of free collateral available

    for withdraw or opening new positions or orders)
  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

Name
Type
Description

trader

address

The address of the trader

ratio

uint24

The margin requirement ratio, imRatio or mmRatio

Name
Type
Description

freeCollateralByRatio

int256

freeCollateral (in settlement token's decimals), by using the

    input margin requirement ratio; can be negative
  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)

Name
Type
Description

trader

address

The address of the trader

Name
Type
Description

balance

int256

The balance amount (in settlement token's decimals)

  function getAccountValue(
    address trader
  ) external returns (int256)

Get account value of the specified trader

Name
Type
Description

trader

address

The address of the trader

Name
Type
Description

accountValueX10_S

int256

account value (in settlement token's decimals)

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

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

Name
Type
Description

trader

address

The address of the trader

Name
Type
Description

collateralTokens

address[]

array of collateral token addresses

  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

Name
Type
Description

trader

address

The address of the trader

Name
Type
Description

balance

int256

The balance amount (in settlement token's decimals)

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

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

Name
Type
Description

trader

address

The address of the trader

token

address

The address of the collateral token

Name
Type
Description

balance

int256

The balance amount (in its native decimals)

  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

Name
Type
Description

trader

address

The address of the trader

token

address

The address of the collateral token

Name
Type
Description

freeCollateral

uint256

amount of that token (in the token's native decimals)

  function isLiquidatable(
    address trader
  ) public returns (bool)
Name
Type
Description

trader

address

The address of the trader

Name
Type
Description

isLiquidatable

bool

If the trader can be liquidated

  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)

Name
Type
Description

trader

address

The address of the trader

Name
Type
Description

marginRequirement

int256

margin requirement (in 18 decimals)

  function getCollateralMmRatio(
  ) public returns (uint24)

Get the maintenance margin ratio for collateral liquidation

Name
Type
Description

collateralMmRatio

uint24

The maintenance margin ratio for collateral liquidation

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

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

Name
Type
Description

token

address

The address of the token of the trader's collateral

collateral

uint256

The amount of collateral token the liquidator wants to get

Name
Type
Description

settlementX10_S

uint256

The settlement amount(in settlement token's decimals) the liquidator needs to pay

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

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

Name
Type
Description

token

address

The address of the token of the trader's collateral

settlementX10_S

uint256

The amount of settlement token the liquidator wants to pay

Name
Type
Description

collateral

uint256

The collateral amount(in its native decimals) the liquidator can get

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

Name
Type
Description

trader

address

The address of the trader

token

address

The address of the token of the trader's collateral

Name
Type
Description

maxRepaidSettlementX10_S

uint256

The maximum settlement amount(in settlement token's decimals)

    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

  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

Name
Type
Description

trader

address

The address of trader that will be settled

setWETH9

deposit

Parameters:

depositFor

Parameters:

depositEther

depositEtherFor

Parameters:

withdraw

Parameters:

withdrawEther

Parameters:

withdrawAll

Parameters:

Return Values:

withdrawAllEther

Return Values:

liquidateCollateral

Parameters:

Return Values:

getSettlementToken

Return Values:

decimals

Return Values:

getTotalDebt

Return Values:

getClearingHouseConfig

Return Values:

getAccountBalance

Return Values:

getInsuranceFund

Return Values:

getExchange

Return Values:

getClearingHouse

Return Values:

getCollateralManager

Return Values:

getWETH9

Return Values:

getFreeCollateral

Parameters:

Return Values:

getFreeCollateralByRatio

Parameters:

Return Values:

getSettlementTokenValue

Parameters:

Return Values:

getAccountValue

Parameters:

Return Values:

getCollateralTokens

Parameters:

Return Values:

getBalance

Parameters:

Return Values:

getBalanceByToken

Parameters:

Return Values:

getFreeCollateralByToken

Parameters:

Return Values:

isLiquidatable

Parameters:

Return Values:

getMarginRequirementForCollateralLiquidation

Parameters:

Return Values:

getCollateralMmRatio

Return Values:

getRepaidSettlementByCollateral

Parameters:

Return Values:

getLiquidatableCollateralBySettlement

Parameters:

Return Values:

getMaxRepaidSettlementAndLiquidatableCollateral

Parameters:

Return Values:

settleBadDebt

Parameters:

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​