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

OrderBook

Last updated 1 year ago

Was this helpful?

InternalAddLiquidityToOrderParams

  struct InternalAddLiquidityToOrderParams(
    address maker
    address baseToken
    address pool
    int24 lowerTick
    int24 upperTick
    uint256 feeGrowthGlobalX128
    uint128 liquidity
    uint256 base
    uint256 quote
    struct Funding.Growth globalFundingGrowth
  )

InternalRemoveLiquidityParams

  struct InternalRemoveLiquidityParams(
    address maker
    address baseToken
    address pool
    bytes32 orderId
    int24 lowerTick
    int24 upperTick
    uint128 liquidity
  )
  struct InternalSwapStep(
    uint160 initialSqrtPriceX96
    int24 nextTick
    bool isNextTickInitialized
    uint160 nextSqrtPriceX96
    uint256 amountIn
    uint256 amountOut
    uint256 fee
  )
  function initialize(
  ) external
  function setExchange(
  ) external
  function addLiquidity(
    struct IOrderBook.AddLiquidityParams params
  ) external returns (struct IOrderBook.AddLiquidityResponse)

Add liquidity logic

Only used by ClearingHouse contract

Name
Type
Description

params

struct IOrderBook.AddLiquidityParams

Add liquidity params, detail on IOrderBook.AddLiquidityParams

Name
Type
Description

response

struct IOrderBook.AddLiquidityResponse

Add liquidity response, detail on IOrderBook.AddLiquidityResponse

  function removeLiquidity(
    struct IOrderBook.RemoveLiquidityParams params
  ) external returns (struct IOrderBook.RemoveLiquidityResponse)

Remove liquidity logic, only used by ClearingHouse contract

Name
Type
Description

params

struct IOrderBook.RemoveLiquidityParams

Remove liquidity params, detail on IOrderBook.RemoveLiquidityParams

Name
Type
Description

response

struct IOrderBook.RemoveLiquidityResponse

Remove liquidity response, detail on IOrderBook.RemoveLiquidityResponse

  function updateFundingGrowthAndLiquidityCoefficientInFundingPayment(
    address trader,
    address baseToken,
    struct Funding.Growth fundingGrowthGlobal
  ) external returns (int256 liquidityCoefficientInFundingPayment)

This is the non-view version of getLiquidityCoefficientInFundingPayment(), only can be called by Exchange contract

Name
Type
Description

trader

address

The trader address

baseToken

address

The base token address

fundingGrowthGlobal

struct Funding.Growth

The funding growth info, detail on Funding.Growth

Name
Type
Description

liquidityCoefficientInFundingPayment

int256

the funding payment of all orders/liquidity of a maker

  function updateOrderDebt(
  ) external
  function uniswapV3MintCallback(
    uint256 amount0Owed,
    uint256 amount1Owed,
    bytes data
  ) external

Called to msg.sender after minting liquidity to a position from IUniswapV3Pool#mint.

In the implementation you must pay the pool tokens owed for the minted liquidity. The caller of this method must be checked to be a UniswapV3Pool deployed by the canonical UniswapV3Factory.

Name
Type
Description

amount0Owed

uint256

The amount of token0 due to the pool for the minted liquidity

amount1Owed

uint256

The amount of token1 due to the pool for the minted liquidity

data

bytes

Any data passed through by the caller via the IUniswapV3PoolActions#mint call

  function replaySwap(
    struct IOrderBook.ReplaySwapParams params
  ) external returns (struct IOrderBook.ReplaySwapResponse)

Replay the swap and get the swap result (price impact and swap fee), only can be called by Exchange contract;

ReplaySwapResponse.insuranceFundFee = fee * insuranceFundFeeRatio

Name
Type
Description

params

struct IOrderBook.ReplaySwapParams

ReplaySwap params, detail on IOrderBook.ReplaySwapParams

Name
Type
Description

response

struct IOrderBook.ReplaySwapResponse

The swap result encoded in ReplaySwapResponse

  function getExchange(
  ) external returns (address)

Get Exchange contract address

Name
Type
Description

exchange

address

The Exchange contract address

  function getOpenOrderIds(
    address trader,
    address baseToken
  ) external returns (bytes32[])

Get open order ids of a trader in the given market

Name
Type
Description

trader

address

The trader address

baseToken

address

The base token address

Name
Type
Description

orderIds

bytes32[]

The open order ids

  function getOpenOrderById(
    bytes32 orderId
  ) external returns (struct OpenOrder.Info)

Get open order info by given order id

Name
Type
Description

orderId

bytes32

The order id

Name
Type
Description

info

struct OpenOrder.Info

The open order info encoded in OpenOrder.Info

  function getOpenOrder(
    address trader,
    address baseToken,
    int24 upperTick,
    int24 lowerTick
  ) external returns (struct OpenOrder.Info)

Get open order info by given base token, upper tick and lower tick

Name
Type
Description

trader

address

The trader address

baseToken

address

The base token address

upperTick

int24

The upper tick

lowerTick

int24

The lower tick

Name
Type
Description

info

struct OpenOrder.Info

he open order info encoded in OpenOrder.Info

  function hasOrder(
    address trader,
    address[] tokens
  ) external returns (bool)

Check if the specified trader has order in given markets

Name
Type
Description

trader

address

The trader address

tokens

address[]

The base token addresses

Name
Type
Description

hasOrder

bool

True if the trader has order in given markets

  function getTotalQuoteBalanceAndPendingFee(
    address trader,
    address[] baseTokens
  ) external returns (int256 totalQuoteAmountInPools, uint256 totalPendingFee)

Get the total quote token amount and pending fees of all orders in given markets

Name
Type
Description

trader

address

The trader address

baseTokens

address[]

The base token addresses

Name
Type
Description

totalQuoteAmountInPools

int256

The total quote token amount

totalPendingFee

uint256

The total pending fees in the orders

  function getTotalTokenAmountInPoolAndPendingFee(
    address trader,
    address baseToken,
    bool fetchBase
  ) external returns (uint256 tokenAmount, uint256 pendingFee)

Get the total token amount (quote or base) and pending fees of all orders in the given market

Name
Type
Description

trader

address

The trader address

baseToken

address

The base token addresses

fetchBase

bool

True if fetch base token amount, false if fetch quote token amount

Name
Type
Description

tokenAmount

uint256

The total quote/base token amount

totalPendingFee

uint256

The total pending fees in the orders

  function getLiquidityCoefficientInFundingPayment(
  ) external returns (int256 liquidityCoefficientInFundingPayment)

Get the pending funding payment of all orders in the given market

This is the view version of updateFundingGrowthAndLiquidityCoefficientInFundingPayment(), so only part of the funding payment will be returned. Use it with caution because it does not return all the pending funding payment of orders. Normally you won't need to use this function

Name
Type
Description

liquidityCoefficientInFundingPayment

int256

the funding payment of all orders/liquidity of a maker

  function getPendingFee(
    address trader,
    address baseToken,
    int24 lowerTick,
    int24 upperTick
  ) external returns (uint256)

Get the pending fees of a order

Name
Type
Description

trader

address

The trader address

baseToken

address

The base token address

lowerTick

int24

The lower tick

upperTick

int24

The upper tick

Name
Type
Description

fee

uint256

The pending fees

  function getTotalOrderDebt(
    address trader,
    address baseToken,
    bool fetchBase
  ) public returns (uint256)

Get the total debt token amount (base or quote) of all orders in the given market

Name
Type
Description

trader

address

The trader address

baseToken

address

The base token address

fetchBase

bool

True if fetch base token amount, false if fetch quote token amount

Name
Type
Description

debtAmount

uint256

The total debt token amount

InternalSwapStep

Functions

initialize

setExchange

addLiquidity

Parameters:

Return Values:

removeLiquidity

Parameters:

Return Values:

updateFundingGrowthAndLiquidityCoefficientInFundingPayment

Parameters:

Return Values:

updateOrderDebt

uniswapV3MintCallback

Parameters:

replaySwap

Parameters:

Return Values:

getExchange

Return Values:

getOpenOrderIds

Parameters:

Return Values:

getOpenOrderById

Parameters:

Return Values:

getOpenOrder

Parameters:

Return Values:

hasOrder

Parameters:

Return Values:

getTotalQuoteBalanceAndPendingFee

Parameters:

Return Values:

getTotalTokenAmountInPoolAndPendingFee

Parameters:

Return Values:

getLiquidityCoefficientInFundingPayment

Return Values:

getPendingFee

Parameters:

Return Values:

getTotalOrderDebt

Parameters:

Return Values:

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