ClearingHouse
Entry point for users to vault and perpetual markets
Methods
DEFAULT_ADMIN_ROLE
Returns
Name | Type | Description |
---|---|---|
_0 | bytes32 | undefined |
EMERGENCY_ADMIN
Returns
Name | Type | Description |
---|---|---|
_0 | bytes32 | undefined |
GOVERNANCE
Returns
Name | Type | Description |
---|---|---|
_0 | bytes32 | undefined |
addStakingContract
Add a staking contract
Staking contract is not implemented yet
Parameters
Name | Type | Description |
---|---|---|
staking | contract IStakingContract | Staking Contract |
allowListPerpetual
Add one perpetual market to the list of markets
Parameters
Name | Type | Description |
---|---|---|
perp | contract IPerpetual | Market to add to the list of supported market |
canSeizeCollateral
Parameters
Name | Type | Description |
---|---|---|
liquidatee | address | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | bool | undefined |
changePosition
Open or increase or reduce a position, either LONG or SHORT
No number for the leverage is given but the amount in the vault must be bigger than minMarginAtCreation
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market |
amount | uint256 | Amount in vQuote (if LONG) or vBase (if SHORT) to sell. 18 decimals |
minAmount | uint256 | Minimum amount that the user is willing to accept. 18 decimals |
direction | enum LibPerpetual.Side | Whether the trader wants to go in the LONG or SHORT direction overall |
closePositionWithdrawCollateral
Single close position function, groups closing position and withdrawing collateralImportant: proposedAmount
must be large enough to close the entire position else the function call will fail
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market |
proposedAmount | uint256 | Amount of tokens to be sold, in vBase if LONG, in vQuote if SHORT. 18 decimals |
minAmount | uint256 | Minimum amount that the user is willing to accept, in vQuote if LONG, in vBase if SHORT. 18 decimals |
token | contract IERC20Metadata | Token used for the collateral |
decreaseAllowance
Decrease withdrawal approval for a receiving address on the vault
Parameters
Name | Type | Description |
---|---|---|
receiver | address | Address allowed to transfer |
subtractedAmount | uint256 | Amount to subtract from the current approved value. 18 decimals |
token | contract IERC20Metadata | Token to be withdrawn by the |
delistPerpetual
Remove on perpetual market of the list of markets
Parameters
Name | Type | Description |
---|---|---|
perp | contract IPerpetual | Market to add to the list of supported market |
deposit
Deposit tokens into the vault
Parameters
Name | Type | Description |
---|---|---|
amount | uint256 | Amount to be used as collateral. Might not be 18 decimals |
token | contract IERC20Metadata | Token to be used for the collateral |
depositFor
Deposit tokens into the vault on behalf of another user
Parameters
Name | Type | Description |
---|---|---|
user | address | Address of user whose balance should be adjusted |
amount | uint256 | Amount to be used as collateral. Might not be 18 decimals |
token | contract IERC20Metadata | Token to be used for the collateral |
extendPositionWithCollateral
Single open position function, groups depositing collateral and extending position
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market |
user | address | Address of user whose balance should be adjusted |
collateralAmount | uint256 | Amount to be used as the collateral of the position. Might not be 18 decimals |
token | contract IERC20Metadata | Token to be used for the collateral of the position |
positionAmount | uint256 | Amount to be sold, in vQuote (if LONG) or vBase (if SHORT). Must be 18 decimals |
direction | enum LibPerpetual.Side | Whether the position is LONG or SHORT |
minAmount | uint256 | Minimum amount that the user is willing to accept. 18 decimals |
getDebtAcrossMarkets
Get user debt across all perpetual markets
Parameters
Name | Type | Description |
---|---|---|
account | address | User address (trader and/or liquidity provider) |
Returns
Name | Type | Description |
---|---|---|
userDebt | int256 | undefined |
getFreeCollateralByRatio
Get free collateral of a user given a chosen margin ratio
Parameters
Name | Type | Description |
---|---|---|
account | address | User address (trader and/or liquidity provider) |
ratio | int256 | Margin ratio (minMargin or minMarginAtCreation) |
Returns
Name | Type | Description |
---|---|---|
freeCollateral | int256 | undefined |
getNumMarkets
Return the number of active markets
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
getPnLAcrossMarkets
Get user profit/loss across all perpetual markets
Parameters
Name | Type | Description |
---|---|---|
account | address | User address (trader and/or liquidity provider) |
Returns
Name | Type | Description |
---|---|---|
unrealizedPositionPnl | int256 | undefined |
getRoleAdmin
Returns the admin role that controls role
. See {grantRole} and {revokeRole}. To change a role's admin, use {_setRoleAdmin}.
Parameters
Name | Type | Description |
---|---|---|
role | bytes32 | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | bytes32 | undefined |
getTotalMarginRequirement
Get the margin required to serve user debt at a chosen margin ratio
Parameters
Name | Type | Description |
---|---|---|
account | address | User address (trader and/or liquidity provider) |
ratio | int256 | Margin ratio (minMargin or minMarginAtCreation) |
Returns
Name | Type | Description |
---|---|---|
requiredMargin | int256 | undefined |
grantRole
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
Name | Type | Description |
---|---|---|
role | bytes32 | undefined |
account | address | undefined |
hasRole
Returns true
if account
has been granted role
.
Parameters
Name | Type | Description |
---|---|---|
role | bytes32 | undefined |
account | address | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | bool | undefined |
id
Allowlisted Perpetual indices
Parameters
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
increaseAllowance
Increase withdrawal approval for a receiving address on the vault
Parameters
Name | Type | Description |
---|---|---|
receiver | address | Address allowed to transfer |
addedAmount | uint256 | Amount to add to the current approved value. 18 decimals |
token | contract IERC20Metadata | Token to be withdrawn by the |
insurance
Insurance contract
Returns
Name | Type | Description |
---|---|---|
_0 | contract IInsurance | undefined |
insuranceRatio
Insurance ratio
Once the insurance reserve exceed this ratio of the tvl, governance can withdraw exceeding insurance fee
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
isEmergencyAdmin
Parameters
Name | Type | Description |
---|---|---|
account | address | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | bool | undefined |
isGovernor
Parameters
Name | Type | Description |
---|---|---|
account | address | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | bool | undefined |
liquidateLp
Submit the address of a LP whose position is worth liquidating for a reward
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market |
liquidatee | address | Address of the account to liquidate |
minVTokenAmounts | uint256[2] | undefined |
proposedAmount | uint256 | Amount of tokens to be sold, in vBase if LONG, in vQuote if SHORT. 18 decimals |
minAmount | uint256 | Minimum amount that the user is willing to accept. 18 decimals |
liquidateTrader
Submit the address of an Trader whose position is worth liquidating for a reward
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market |
liquidatee | address | Address of the account to liquidate |
proposedAmount | uint256 | Amount of tokens to be sold, in vBase if LONG, in vQuote if SHORT. 18 decimals |
minAmount | uint256 | Minimum amount that the user is willing to accept. 18 decimals |
liquidationDiscount
Discount on the collateral price for the liquidator
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
liquidationReward
liquidation reward paid to liquidators
Paid on dollar value of an trader position. important: liquidationReward < minMargin or liquidations will result in protocol losses
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
liquidationRewardInsuranceShare
Portion of the liquidation reward that the insurance gets
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
marketIds
Number of Allowlisted Perpetuals
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
minMargin
minimum maintenance margin
Returns
Name | Type | Description |
---|---|---|
_0 | int256 | undefined |
minMarginAtCreation
minimum margin when opening a position
Returns
Name | Type | Description |
---|---|---|
_0 | int256 | undefined |
minPositiveOpenNotional
minimum positive open notional when opening a position
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
nonUACollSeizureDiscount
Discount ratio to be applied on non-UA collaterals before seizing said collaterals for some UA
Must be lower than liquidationDiscount to ensure liquidations don't generate bad debt
Returns
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
openReversePosition
Open a position in the opposite direction of the currently opened positionFor example, a trader with a LONG position can switch to a SHORT position with just one call to this function
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market |
closeProposedAmount | uint256 | Amount in vQuote (if SHORT) or vBase (if LONG) to sell to close the position. 18 decimals |
closeMinAmount | uint256 | Minimum amount that the user is willing to accept when closing the position. 18 decimals |
openProposedAmount | uint256 | Amount in vQuote (if LONG) or vBase (if SHORT) to sell to open the reversed position. 18 decimals |
openMinAmount | uint256 | Minimum amount that the user is willing to accept when opening the reversed position. 18 decimals |
direction | enum LibPerpetual.Side | Whether the trader wants to go in the LONG or SHORT direction overall |
pause
Pause the contract
Can only be called by Emergency Admin
paused
Returns true if the contract is paused, and false otherwise.
Returns
Name | Type | Description |
---|---|---|
_0 | bool | undefined |
perpetuals
Allowlisted Perpetual contracts
Parameters
Name | Type | Description |
---|---|---|
_0 | uint256 | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | contract IPerpetual | undefined |
provideLiquidity
Provide liquidity to the pool, without depositing new capital in the vault
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market |
amounts | uint256[2] | Amount of virtual tokens ([vQuote, vBase]) provided. 18 decimals |
minLpAmount | uint256 | Minimum amount of Lp tokens minted. 18 decimals |
removeLiquidity
Remove liquidity from the pool and account profit/loss in UA
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market |
liquidityAmountToRemove | uint256 | Amount of liquidity (in LP tokens) to be removed from the pool. 18 decimals |
minVTokenAmounts | uint256[2] | Minimum amount of virtual tokens [vQuote, vBase] to withdraw from the curve pool. 18 decimals |
proposedAmount | uint256 | Amount at which to sell the active LP position (in vBase if LONG, in vQuote if SHORT). 18 decimals |
minAmount | uint256 | Minimum amount that the user is willing to accept when closing his active position generated after removing liquidity, in vQuote if LONG, in vBase if SHORT. 18 decimals |
renounceRole
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
Name | Type | Description |
---|---|---|
role | bytes32 | undefined |
account | address | undefined |
revokeRole
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
Name | Type | Description |
---|---|---|
role | bytes32 | undefined |
account | address | undefined |
seizeCollateral
Buy the non-UA collaterals of a user at a discounted UA price to settle the debt of said user
Parameters
Name | Type | Description |
---|---|---|
liquidatee | address | Address of the account to liquidate |
setParameters
Parameters
Name | Type | Description |
---|---|---|
params | IClearingHouse.ClearingHouseParams | undefined |
settleDust
Sell dust of a given market
Can only be called by Emergency Admin
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | Index of the perpetual market to sell dust from |
proposedAmount | uint256 | Amount of tokens to be sold, in vBase if LONG, in vQuote if SHORT. 18 decimals |
minAmount | uint256 | Minimum amount that the user is willing to accept, in vQuote if LONG, in vBase if SHORT. 18 decimals |
direction | enum LibPerpetual.Side | undefined |
stakingContract
Staking contract
Returns
Name | Type | Description |
---|---|---|
_0 | contract IStakingContract | undefined |
supportsInterface
See {IERC165-supportsInterface}.
Parameters
Name | Type | Description |
---|---|---|
interfaceId | bytes4 | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | bool | undefined |
uaDebtSeizureThreshold
UA debt amount at which non-UA collaterals can be seized to pay back UA debts
Returns
Name | Type | Description |
---|---|---|
_0 | int256 | undefined |
unpause
Unpause the contract
Can only be called by Emergency Admin
updateGlobalState
vault
Vault contract
Returns
Name | Type | Description |
---|---|---|
_0 | contract IVault | undefined |
withdraw
Withdraw tokens from the vault
Parameters
Name | Type | Description |
---|---|---|
amount | uint256 | Amount of collateral to withdraw. Might not be 18 decimals (decimals of |
token | contract IERC20Metadata | Token of the collateral |
withdrawAll
Withdraw all tokens from the vault
Should only be called by the trader
Parameters
Name | Type | Description |
---|---|---|
token | contract IERC20Metadata | Token of the collateral |
withdrawFrom
Withdraw tokens from the vault on behalf of a user
Parameters
Name | Type | Description |
---|---|---|
user | address | Account to withdraw collateral from |
amount | uint256 | Amount of collateral to withdraw. Might not be 18 decimals (decimals of |
token | contract IERC20Metadata | Token of the collateral |
Events
ChangePosition
Emitted when a position is opened/extended
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | undefined |
user | address | undefined |
direction | enum LibPerpetual.Side | undefined |
addedOpenNotional | int256 | undefined |
addedPositionSize | int256 | undefined |
profit | int256 | undefined |
tradingFeesPayed | int256 | undefined |
insuranceFeesPayed | int256 | undefined |
isPositionIncreased | bool | undefined |
isPositionClosed | bool | undefined |
ClearingHouseParametersChanged
Emitted when parameters are changed
Parameters
Name | Type | Description |
---|---|---|
newMinMargin | int256 | undefined |
newMinMarginAtCreation | int256 | undefined |
newMinPositiveOpenNotional | uint256 | undefined |
newLiquidationReward | uint256 | undefined |
newInsuranceRatio | uint256 | undefined |
newLiquidationRewardInsuranceShare | uint256 | undefined |
newLiquidationDiscount | uint256 | undefined |
nonUACollSeizureDiscount | uint256 | undefined |
uaDebtSeizureThreshold | int256 | undefined |
DustSold
Emitted when dust is sold by governance
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | undefined |
profit | int256 | undefined |
tradingFeesPayed | int256 | undefined |
LiquidationCall
Emitted when an user position is liquidated
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | undefined |
liquidatee | address | undefined |
liquidator | address | undefined |
notional | uint256 | undefined |
profit | int256 | undefined |
tradingFeesPayed | int256 | undefined |
isTrader | bool | undefined |
LiquidityProvided
Emitted when (additional) liquidity is provided
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | undefined |
liquidityProvider | address | undefined |
quoteAmount | uint256 | undefined |
baseAmount | uint256 | undefined |
tradingFeesEarned | int256 | undefined |
LiquidityRemoved
Emitted when liquidity is removed
Parameters
Name | Type | Description |
---|---|---|
idx | uint256 | undefined |
liquidityProvider | address | undefined |
reductionRatio | uint256 | undefined |
profit | int256 | undefined |
tradingFeesPayed | int256 | undefined |
isPositionClosed | bool | undefined |
MarketAdded
Emitted when new perpetual market is added
Parameters
Name | Type | Description |
---|---|---|
perpetual | contract IPerpetual | undefined |
listedIdx | uint256 | undefined |
numPerpetuals | uint256 | undefined |
MarketRemoved
Emitted when perpetual market is removed
Parameters
Name | Type | Description |
---|---|---|
perpetual | contract IPerpetual | undefined |
delistedIdx | uint256 | undefined |
numPerpetuals | uint256 | undefined |
Paused
Parameters
Name | Type | Description |
---|---|---|
account | address | undefined |
RoleAdminChanged
Parameters
Name | Type | Description |
---|---|---|
role | bytes32 | undefined |
previousAdminRole | bytes32 | undefined |
newAdminRole | bytes32 | undefined |
RoleGranted
Parameters
Name | Type | Description |
---|---|---|
role | bytes32 | undefined |
account | address | undefined |
sender | address | undefined |
RoleRevoked
Parameters
Name | Type | Description |
---|---|---|
role | bytes32 | undefined |
account | address | undefined |
sender | address | undefined |
SeizeCollateral
Emitted when an user non-UA collaterals are seized
Parameters
Name | Type | Description |
---|---|---|
liquidatee | address | undefined |
liquidator | address | undefined |
StakingContractChanged
Parameters
Name | Type | Description |
---|---|---|
newStakingContract | contract IStakingContract | undefined |
Unpaused
Parameters
Name | Type | Description |
---|---|---|
account | address | undefined |
Errors
ClearingHouse_AmountProvidedTooLarge
Emitted when a user attempts to provide liquidity with amount larger than his free collateral or collateral balance
ClearingHouse_ChangePositionZeroAmount
Emitted when a user attempts to change his position with no amount
ClearingHouse_ClosePositionStillOpen
Emitted when the position is not reduced entirely using closePositionWithdrawCollateral
ClearingHouse_DepositForZeroAddress
Emitted when attempting to deposit to the zero address
ClearingHouse_ExcessiveLiquidationDiscount
Emitted when the liquidationDiscount is too high
ClearingHouse_ExcessiveLiquidationRewardInsuranceShare
Emitted when the proposed share of the liquidation reward for the insurance is too high
ClearingHouse_ExcessivePositiveOpenNotional
Emitted when the proposed minimum open notional is too high
ClearingHouse_ExtendPositionInsufficientMargin
Emitted when there is not enough margin to extend to the proposed position amount
ClearingHouse_ExtendPositionZeroAmount
Emitted when a user attempts to extend their position with amount equal to 0
ClearingHouse_InsufficientDiffBtwLiquidationDiscountAndNonUACollSeizureDiscount
Emitted when the difference between liquidationDiscount and nonUACollSeizureDiscount isn't large enough
ClearingHouse_InsufficientUaDebtSeizureThreshold
Emitted when the proposed UA debt limit is lower than the minimum acceptable value
ClearingHouse_InvalidInsuranceRatio
Emitted when the proposed insurance ratio is too low or too high
ClearingHouse_InvalidLiquidationReward
Emitted when the proposed liquidation reward is too low or too high
ClearingHouse_InvalidMinMargin
Emitted when the proposed minMargin is too low or too high
ClearingHouse_InvalidMinMarginAtCreation
Emitted when the proposed minMarginAtCreation is too low or too high
ClearingHouse_LiquidateInsufficientProposedAmount
Emitted when the attempted liquidation does not close the full position
ClearingHouse_LiquidateInvalidPosition
Emitted when the liquidatee does not have an open position
ClearingHouse_LiquidateValidMargin
Emitted when the margin of the liquidatee's position is still valid
ClearingHouse_LiquidationDebtSizeZero
Emitted when a collateral liquidation for a user with no UA debt is tried
ClearingHouse_MarketDoesNotExist
Emitted when attempting to remove a perpetual market which does not exist
ClearingHouse_NegativeDustProceeds
Emitted when governance tries to sell dust with a negative balance
ClearingHouse_PerpetualMarketAlreadyAssigned
Emitted when passing the address of a perpetual market which has already been added
ClearingHouse_ProvideLiquidityZeroAmount
Emitted when a user attempts to provide liquidity with amount equal to 0
ClearingHouse_ReducePositionZeroAmount
Emitted when a user attempts to reduce their position with amount equal to 0
ClearingHouse_RemoveLiquidityInsufficientFunds
Emitted when a user attempts to withdraw more liquidity than they have
ClearingHouse_SufficientUserCollateral
Emitted when a liquidator tries seizing collateral of user with sufficient collaterals level
ClearingHouse_UnderOpenNotionalAmountRequired
Emitted when a user tries to open a position with an incorrect open notional amount
ClearingHouse_WithdrawInsufficientMargin
Emitted when there is not enough margin to withdraw the requested amount
ClearingHouse_ZeroAddress
Emitted when the zero address is provided
PRBMathSD59x18__AbsInputTooSmall
Emitted when the input is MIN_SD59x18.
PRBMathSD59x18__MulInputTooSmall
Emitted when one of the inputs is MIN_SD59x18.
PRBMathSD59x18__MulOverflow
Emitted when the intermediary absolute result overflows SD59x18.
Parameters
Name | Type | Description |
---|---|---|
rAbs | uint256 | undefined |
PRBMath__MulDivFixedPointOverflow
Emitted when the result overflows uint256.
Parameters
Name | Type | Description |
---|---|---|
prod1 | uint256 | undefined |
Last updated