Perpetual

Handle all the logic involving the pool. Interact with the CryptoSwap pool

Methods

DEFAULT_ADMIN_ROLE

function DEFAULT_ADMIN_ROLE() external view returns (bytes32)

Returns

EMERGENCY_ADMIN

function EMERGENCY_ADMIN() external view returns (bytes32)

Returns

GOVERNANCE

function GOVERNANCE() external view returns (bytes32)

Returns

changePosition

function changePosition(address account, uint256 amount, uint256 minAmount, enum LibPerpetual.Side direction, bool isLiquidation) external nonpayable returns (int256 quoteProceeds, int256 baseProceeds, int256 profit, int256 tradingFeesPayed, bool isPositionIncreased, bool isPositionClosed)

Open or increase or reduce a position, either LONG or SHORT

Function can be used to extend or reduce a position. Reversing a position is prohibited.

Parameters

Returns

clearingHouse

function clearingHouse() external view returns (contract IClearingHouse)

Clearing House contract

Returns

curveCryptoViews

function curveCryptoViews() external view returns (contract ICurveCryptoViews)

Curve Crypto Views

Returns

getGlobalPosition

function getGlobalPosition() external view returns (struct LibPerpetual.GlobalPosition)

Get global market position

Returns

getLpLiquidity

function getLpLiquidity(address account) external view returns (uint256)

Get the lp tokens owned by a Liquidity Provider

Parameters

Returns

getLpOpenNotional

function getLpOpenNotional(address account) external view returns (int256)

Get the dollar value of the liquidity provided by a liquidity Provider

Parameters

Returns

getLpPosition

function getLpPosition(address account) external view returns (struct LibPerpetual.LiquidityProviderPosition)

Get the position of a liquidity provider

Parameters

Returns

getLpPositionAfterWithdrawal

function getLpPositionAfterWithdrawal(address account) external view returns (struct LibPerpetual.TraderPosition)

Get the (active) position of a liquidity provider after withdrawing liquidity

Parameters

Returns

getLpTradingFees

function getLpTradingFees(address account) external view returns (uint256 tradingFeesEarned)

Get the trading fees earned by a LP

Parameters

Returns

getLpUnrealizedPnL

function getLpUnrealizedPnL(address account) external view returns (int256 pnl)

Get the unrealized profit and loss of a LP

Parameters

Returns

getPendingPnL

function getPendingPnL(address account) external view returns (int256 pnL)

Get the Profit and Loss of a user

Parameters

Returns

getRoleAdmin

function getRoleAdmin(bytes32 role) external view returns (bytes32)

Returns the admin role that controls role. See {grantRole} and {revokeRole}. To change a role's admin, use {_setRoleAdmin}.

Parameters

Returns

getTotalLiquidityProvided

function getTotalLiquidityProvided() external view returns (uint256)

Get the market total liquidity provided to the Crypto Swap pool

Returns

getTraderPosition

function getTraderPosition(address account) external view returns (struct LibPerpetual.TraderPosition)

Get the position of a trader

Parameters

Returns

getTraderUnrealizedPnL

function getTraderUnrealizedPnL(address account) external view returns (int256 pnl)

Get the unrealized profit and loss of a trader

Parameters

Returns

getUserDebt

function getUserDebt(address account) external view returns (int256 debt)

Get the user debt of an user

Parameters

Returns

grantRole

function grantRole(bytes32 role, address account) external nonpayable

Grants role to account. If account had not been already granted role, emits a {RoleGranted} event. Requirements: - the caller must have role's admin role. May emit a {RoleGranted} event.

Parameters

hasRole

function hasRole(bytes32 role, address account) external view returns (bool)

Returns true if account has been granted role.

Parameters

Returns

indexPrice

function indexPrice() external view returns (int256)

Return the current off-chain exchange rate for vBase/vQuote

Returns

insuranceFee

function insuranceFee() external view returns (int256)

paid on dollar value of an opened position (used in ClearingHouse)

Returns

isEmergencyAdmin

function isEmergencyAdmin(address account) external view returns (bool)

Parameters

Returns

isGovernor

function isGovernor(address account) external view returns (bool)

Parameters

Returns

isLpPositionOpen

function isLpPositionOpen(address account) external view returns (bool)

Whether or not a LP position is opened

Parameters

Returns

isTraderPositionOpen

function isTraderPositionOpen(address account) external view returns (bool)

Whether a trader position is opened or not

Parameters

Returns

isTradingExpansionAllowed

function isTradingExpansionAllowed() external view returns (bool)

wether opening and extending trading positions is allowed

Returns

lockPeriod

function lockPeriod() external view returns (uint256)

time when the liquidity provision has to be locked

Returns

lpDebtCoef

function lpDebtCoef() external view returns (int256)

lp debt coefficient

Returns

market

function market() external view returns (contract ICryptoSwap)

Curve CryptoSwap pool

Returns

marketCumulativeAmount

function marketCumulativeAmount() external view returns (int256)

Returns

marketCumulativeAmountAtBeginningOfPeriod

function marketCumulativeAmountAtBeginningOfPeriod() external view returns (int256)

Returns

marketPrice

function marketPrice() external view returns (uint256)

Return the last traded price (used for TWAP)

Returns

marketTwap

function marketTwap() external view returns (int128)

Market Time-weighted average price of base

Returns

maxBlockTradeAmount

function maxBlockTradeAmount() external view returns (uint256)

max trade amount in one block

Returns

maxLiquidityProvided

function maxLiquidityProvided() external view returns (uint256)

maximum liquidity which can be provided to the pool

Returns

maxPosition

function maxPosition() external view returns (uint256)

max position size (1/10 of maxBlockTradeAmount)

Returns

oracleCumulativeAmount

function oracleCumulativeAmount() external view returns (int256)

Returns

oracleCumulativeAmountAtBeginningOfPeriod

function oracleCumulativeAmountAtBeginningOfPeriod() external view returns (int256)

Returns

oracleTwap

function oracleTwap() external view returns (int128)

Oracle Time-weighted average price of base

Returns

pause

function pause() external nonpayable

Pause the contract

Can only be called by Emergency Admin

paused

function paused() external view returns (bool)

Returns true if the contract is paused, and false otherwise.

Returns

provideLiquidity

function provideLiquidity(address account, uint256[2] amounts, uint256 minLpAmount) external nonpayable returns (int256 tradingFees)

Provide liquidity to the pool

Parameters

Returns

removeLiquidity

function removeLiquidity(address account, uint256 liquidityAmountToRemove, uint256[2] minVTokenAmounts, uint256 proposedAmount, uint256 minAmount, bool isLiquidation) external nonpayable returns (int256 profit, int256 tradingFeesPayed, uint256 reductionRatio, int256 quoteProceeds, bool isPositionClosed)

Remove liquidity from the pool

Parameters

Returns

removeLiquiditySwap

function removeLiquiditySwap(address account, uint256 liquidityAmountToRemove, uint256[2] minVTokenAmounts, bytes func) external nonpayable

Simulate removing liquidity from the curve pool to increase the slippage and then performs a single swap on the curve pool. Returns the proceeds from the trade with revert message

Used to compute the proposedAmount parameter needed for removing liquidityTo be statically called from ClearingHouseViewer.removeLiquiditySwap

Parameters

renounceRole

function renounceRole(bytes32 role, address account) external nonpayable

Revokes role from the calling account. Roles are often managed via {grantRole} and {revokeRole}: this function's purpose is to provide a mechanism for accounts to lose their privileges if they are compromised (such as when a trusted device is misplaced). If the calling account had been revoked role, emits a {RoleRevoked} event. Requirements: - the caller must be account. May emit a {RoleRevoked} event.

Parameters

revokeRole

function revokeRole(bytes32 role, address account) external nonpayable

Revokes role from account. If account had been granted role, emits a {RoleRevoked} event. Requirements: - the caller must have role's admin role. May emit a {RoleRevoked} event.

Parameters

riskWeight

function riskWeight() external view returns (uint256)

risk weight of the perpetual pair

Returns

sensitivity

function sensitivity() external view returns (int256)

funding rate sensitivity to price deviations

Returns

setParameters

function setParameters(IPerpetual.PerpetualParams params) external nonpayable

Parameters

settleLpFunding

function settleLpFunding(address account) external nonpayable returns (int256 fundingPayments)

Settle funding payments for a liquidity providerUpdate the cumulative funding rate for the LP and return pending funding payments

Parameters

Returns

settleTraderFunding

function settleTraderFunding(address account) external nonpayable returns (int256 fundingPayments)

Settle funding payments for a traderUpdate the cumulative funding rate for the trader and return pending funding payments

Parameters

Returns

supportsInterface

function supportsInterface(bytes4 interfaceId) external view returns (bool)

See {IERC165-supportsInterface}.

Parameters

Returns

toggleTradingExpansionPause

function toggleTradingExpansionPause(bool toPause) external nonpayable

Allow/block trading operations tapping into the liquidity (i.e. opening and extending positions)Meant to be called once an agreed uppon minimum liquidity level is reached (or dropped back to)

Can only be called by Emergency Admin

Parameters

twapFrequency

function twapFrequency() external view returns (uint256)

period over which twap is calculated

Returns

unpause

function unpause() external nonpayable

Unpause the contract

Can only be called by Emergency Admin

updateGlobalState

function updateGlobalState() external nonpayable

Update the global state of the perpetual market

Can be called by offchain worker to update market conditions

vBase

function vBase() external view returns (contract IVBase)

vBase token (traded on CryptoSwap pool)

Returns

vQuote

function vQuote() external view returns (contract IVQuote)

vQuote token (traded on CryptoSwap pool)

Returns

Events

DustGenerated

event DustGenerated(int256 vBaseAmount)

Emitted when (base) dust is generated

Parameters

FundingPaid

event FundingPaid(address indexed account, int256 amount, int256 globalCumulativeFundingRate, int256 userCumulativeFundingRate, bool isTrader)

Emitted when funding payments are exchanged for a trader / lp

Parameters

FundingRateUpdated

event FundingRateUpdated(int256 cumulativeFundingRate, int256 cumulativeFundingPerLpToken, int256 fundingRate)

Emitted when funding rate is updated

Parameters

Log

event Log(string errorMessage)

Emitted when swap with cryptoswap pool fails

Parameters

Paused

event Paused(address account)

Parameters

PerpetualParametersChanged

event PerpetualParametersChanged(uint256 newRiskWeight, uint256 newMaxLiquidityProvided, uint256 newTwapFrequency, int256 newSensitivity, uint256 newMaxBlockTradeAmount, int256 newInsuranceFee, int256 newLpDebtCoef, uint256 lockPeriod)

Emitted when parameters are updated

Parameters

RoleAdminChanged

event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole)

Parameters

RoleGranted

event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender)

Parameters

RoleRevoked

event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender)

Parameters

TradingExpansionPauseToggled

event TradingExpansionPauseToggled(address admin, bool toPause)

Emitted when an admin pauses or unpause trading expansion operations

Parameters

TwapUpdated

event TwapUpdated(int256 newOracleTwap, int256 newMarketTwap)

Emitted when TWAP is updated

Parameters

Unpaused

event Unpaused(address account)

Parameters

Errors

PRBMathSD59x18__AbsInputTooSmall

error PRBMathSD59x18__AbsInputTooSmall()

Emitted when the input is MIN_SD59x18.

PRBMathSD59x18__DivInputTooSmall

error PRBMathSD59x18__DivInputTooSmall()

Emitted when one of the inputs is MIN_SD59x18.

PRBMathSD59x18__DivOverflow

error PRBMathSD59x18__DivOverflow(uint256 rAbs)

Emitted when one of the intermediary unsigned results overflows SD59x18.

Parameters

PRBMathSD59x18__MulInputTooSmall

error PRBMathSD59x18__MulInputTooSmall()

Emitted when one of the inputs is MIN_SD59x18.

PRBMathSD59x18__MulOverflow

error PRBMathSD59x18__MulOverflow(uint256 rAbs)

Emitted when the intermediary absolute result overflows SD59x18.

Parameters

PRBMath__MulDivFixedPointOverflow

error PRBMath__MulDivFixedPointOverflow(uint256 prod1)

Emitted when the result overflows uint256.

Parameters

PRBMath__MulDivOverflow

error PRBMath__MulDivOverflow(uint256 prod1, uint256 denominator)

Emitted when the result overflows uint256.

Parameters

Perpetual_AttemptReducePosition

error Perpetual_AttemptReducePosition()

Emitted when the user attempts to reduce their position using extendPosition

Perpetual_AttemptReversePosition

error Perpetual_AttemptReversePosition()

Emitted when the user attempts to reverse their position using changePosition

Perpetual_ExcessiveBlockTradeAmount

error Perpetual_ExcessiveBlockTradeAmount()

Emitted when the price impact of a position is too high

Perpetual_InsuranceFeeInvalid

error Perpetual_InsuranceFeeInvalid(int256 fee)

Emitted when the proposed insurance fee is insufficient/excessive

Parameters

Perpetual_InvalidAdminFee

error Perpetual_InvalidAdminFee()

Emitted when the curve admin fee is invalid

Perpetual_LPOpenPosition

error Perpetual_LPOpenPosition()

Emitted when the liquidity provider has an open position

Perpetual_LPWithdrawExceedsBalance

error Perpetual_LPWithdrawExceedsBalance()

Emitted when the user attempts to withdraw more liquidity than they have deposited

Perpetual_LockPeriodInvalid

error Perpetual_LockPeriodInvalid(uint256 lockPeriod)

Emitted when the proposed lp lock period is insufficient/excessive

Parameters

Perpetual_LockPeriodNotReached

error Perpetual_LockPeriodNotReached(uint256 withdrawTime)

Emitted when the user attempts remove liquidity too early

Parameters

Perpetual_LpAmountDeviation

error Perpetual_LpAmountDeviation()

Emitted when the user attempts provide liquidity with skewed ratios

Perpetual_LpDebtCoefInvalid

error Perpetual_LpDebtCoefInvalid(int256 lpDebtCoef)

Emitted when the proposed lp debt coefficient is insufficient/excessive

Parameters

Perpetual_MarketBalanceTooLow

error Perpetual_MarketBalanceTooLow()

Emitted when a token balance of the market is lte 1

Perpetual_MaxBlockAmountInvalid

error Perpetual_MaxBlockAmountInvalid(uint256 maxBlockTradeAmount)

Emitted when the proposed maximum block trade amount is insufficient

Parameters

Perpetual_MaxLiquidityProvided

error Perpetual_MaxLiquidityProvided()

Emitted when the max tvl is reached

Perpetual_MaxPositionSize

error Perpetual_MaxPositionSize()

Emitted when the position exceeds the max position size

Perpetual_NoOpenPosition

error Perpetual_NoOpenPosition()

Emitted when the user does not have an open position

Perpetual_RiskWeightInvalid

error Perpetual_RiskWeightInvalid(uint256 riskWeight)

Emitted when the proposed market risk weight is insufficient/excessive

Parameters

Perpetual_SenderNotClearingHouse

error Perpetual_SenderNotClearingHouse()

Emitted when the sender is not the clearing house

Perpetual_SenderNotClearingHouseOwner

error Perpetual_SenderNotClearingHouseOwner()

Emitted when the sender is not the clearing house owner

Perpetual_SensitivityInvalid

error Perpetual_SensitivityInvalid(int256 sensitivity)

Emitted when the proposed funding rate sensitivity is insufficient/excessive

Parameters

Perpetual_TooMuchExposure

error Perpetual_TooMuchExposure()

Emitted when the user attempts to open a too large short position

Perpetual_TradingExpansionPaused

error Perpetual_TradingExpansionPaused()

Emitted when trading expansion operations are paused

Perpetual_TwapFrequencyInvalid

error Perpetual_TwapFrequencyInvalid(uint256 twapFrequency)

Emitted when the proposed twap frequency is insufficient/excessive

Parameters

Perpetual_VirtualTokenApprovalConstructor

error Perpetual_VirtualTokenApprovalConstructor(uint256 tokenIndex)

Emitted when the constructor fails to give approval of a virtual token to the market

Parameters

Perpetual_ZeroAddressConstructor

error Perpetual_ZeroAddressConstructor(uint256 paramIndex)

Emitted when the zero address is provided as a parameter in the constructor

Parameters

Last updated