DomFiPerp
DomFiPerp
Perpetual long/short levered instruments for Domination Finance
The heart of Domination Finance exchange. Tracks positions per user. Prevents positions from being changed in ways that would violate vault-level risk management constraints.
- Gets prices from PriceFeed
- Gets trade fee amounts from FeeCalculator and sends them to FeeDistributor
- Gets PnLs from FundingManager and PerpLib
- Stores collateral in DomFiVault
Addresses
Chain | Address |
---|---|
Arbitrum Sepolia | 0xAbeB453C5a7735E8F91228a812EB083aB45B8b44 |
Base Sepolia | 0x260b93E1c41b9de692aeBE4B5313125484e74f7C |
Base Mainnet | 0x53E219e30CB0697E312902295f2aAb69cC06c744 |
Functions
initialize
function initialize(struct StorageInstance app_) external
asset
Collateral token of this perp, e.g. USDC or wETH
function asset() public view returns (contract IERC20 assetToken)
Return Values
Name | Type | Description |
---|---|---|
assetToken | contract IERC20 | assetToken Address of token |
fundingManager
function fundingManager() external view returns (contract IFundingManager)
priceFeed
function priceFeed() external view returns (contract IPriceFeed)
feeCalculator
function feeCalculator() external view returns (contract IFeeCalculator)
feeDistributor
function feeDistributor() external view returns (contract IFeeDistributor)
feeReferral
function feeReferral() external view returns (contract IFeeReferral)
vault
function vault() external view returns (contract IDomFiVault)
totalWeight
function totalWeight() external view returns (FPUnsigned)
totalOpenInterest
total size (margin * leverage) of all outstanding positions in token wei
function totalOpenInterest() external view returns (FPUnsigned)
getMaxExposure
Get max exposure for a product: the highest permitted value of
|long open interest - short open interest|
function getMaxExposure(
FPUnsigned productWeight,
FPUnsigned productExposureMultiplier
) external view returns (
FPUnsigned maxExposure
)
Parameters
Name | Type | Description |
---|---|---|
productWeight | FPUnsigned | product.weight ; determines vault balance allocated to this product |
productExposureMultiplier | FPUnsigned | multiple of product's vault share allowed for exposure |
Return Values
Name | Type | Description |
---|---|---|
maxExposure | FPUnsigned | permitted max exposure |
increasePosition
Increase (or create) a position.
function increasePosition(
address user,
PositionLabel positionLabel,
ProductId productId,
uint256 margin,
bool isLong,
FPUnsigned size
) external
Parameters
Name | Type | Description |
---|---|---|
user | address | Owner of the position. msg.sender must be user or their manager. |
positionLabel | PositionLabel | User-defined ID of position to increase or create. See getPositionId(). |
productId | ProductId | Product of this position. Ignored in favor of stored value if position already exists. |
margin | uint256 | Margin to increase this position by. |
isLong | bool | Direction of this position. Ignored in favor of stored value if position already exists. |
size | FPUnsigned | Size (margin * leverage) of this increase. After increase, resulting position must have valid leverage. |
previewIncrease
Preview results of an increase
function previewIncrease(
address user,
PositionLabel positionLabel,
ProductId productId,
uint256 margin,
bool isLong,
FPUnsigned size
) external returns (
struct IExchangeDomain.IncreasePreview
)
May revert if the increase would revert.
Parameters
Name | Type | Description |
---|---|---|
user | address | Owner of the position |
positionLabel | PositionLabel | User-defined label of the position |
productId | ProductId | Product for this position. Required for new positions; ignored ONLY if modifying an existing position. |
margin | uint256 | Collateral added to the position by the increase |
isLong | bool | Whether position is long. Required for new positions; ignored ONLY if modifying an existing position. |
size | FPUnsigned | Size (margin * leverage) of the increase |
Return Values
Name | Type | Description |
---|---|---|
[0] | struct IExchangeDomain.IncreasePreview |
removeMargin
Remove margin from a position (increasing leverage). Caller must be the position's owner or their manager.
function removeMargin(PositionId positionId, uint256 marginToRemove, address recipient) external
Reverts if
- resulting position is liquidatable
- resulting position has
leverage < 1
orleverage > product.maxLeverage
- resulting position has margin < minMargin
- removeMargin > position.margin
Parameters
Name | Type | Description |
---|---|---|
positionId | PositionId | Position to remove margin from |
marginToRemove | uint256 | Collateral to remove from margin |
recipient | address | Send removed collateral to this address |
canRemove
Whether a position can have margin withdrawn without liquidation
or overleverage, and if so how much. See removeMargin()
.
function canRemove(PositionId positionId) public returns (bool canRemove_, int256 removeableMargin)
Parameters
Name | Type | Description |
---|---|---|
positionId | PositionId | Global ID of Position to check |
Return Values
Name | Type | Description |
---|---|---|
canRemove_ | bool | Whether it has margin available to withdraw |
removeableMargin | int256 | Available removal. Negative if !canRemove_ |
decreasePositions
Decrease (or close) multiple positions simultaneously. Fails unless all succeed.
function decreasePositions(struct IExchangeDomain.DecreasePositionParams[] params) external
Parameters
Name | Type | Description |
---|---|---|
params | struct IExchangeDomain.DecreasePositionParams[] | List of decreases. See DecreasePositionParams |
previewDecrease
Preview results of a decrease
function previewDecrease(
PositionId positionId,
uint256 decreaseMargin,
FPUnsigned decreaseSize
) external returns (
struct IExchangeDomain.DecreasePreview decreasePreview
)
Parameters
Name | Type | Description |
---|---|---|
positionId | PositionId | Global ID of position to be decreased. Must exist. |
decreaseMargin | uint256 | Margin amount to decrease by. May be greater than position margin |
decreaseSize | FPUnsigned | Size to decrease by. May be greater than position size |
Return Values
Name | Type | Description |
---|---|---|
decreasePreview | struct IExchangeDomain.DecreasePreview | Computed DecreasePreview for the position |
decreasePositionWithId
Decrease (or close) a position, and claim proportional share of position margin + PnL. Unlike removing margin, leverage remains the same.
function decreasePositionWithId(
PositionId positionId,
uint256 margin,
FPUnsigned size,
address recipient
) public
If margin >= position.margin
or size >= position.size
, close position.
Parameters
Name | Type | Description |
---|---|---|
positionId | PositionId | Target position. msg.sender must be its owner or their manager. |
margin | uint256 | Margin units to decrease |
size | FPUnsigned | |
recipient | address | Where to send reclaimed share of position value + PnL |
liquidatePositions
Liquidate positionIds
and send any bounty to feeReceiver
function liquidatePositions(PositionId[] positionIds, address feeReceiver) external
Reverts if any positions are not liquidatable. Reverts if public liquidation is disabled but msg.sender is not a registered liquidator.
Parameters
Name | Type | Description |
---|---|---|
positionIds | PositionId[] | Positions to liquidate |
feeReceiver | address | Recipient of any liquidator rewards |
canLiquidate
Liquidation information for each position in positionIds
function canLiquidate(
PositionId[] positionIds
) external returns (
FPSigned[] pnl,
FPSigned[] funding,
bool[] isLiquidatable
)
Parameters
Name | Type | Description |
---|---|---|
positionIds | PositionId[] | Positions to compute liquidation info for |
Return Values
Name | Type | Description |
---|---|---|
pnl | FPSigned[] | Position PnL. See getPositionPnlAndFunding |
funding | FPSigned[] | Position funding. See getPositionPnlAndFunding |
isLiquidatable | bool[] | Whether this position is liquidatable |
getPositionPnlAndFunding
Get the Pnl of a position, including funding
function getPositionPnlAndFunding(
ProductId productId,
bool isLong,
FPUnsigned size,
FPUnsigned positionPrice,
uint256 margin,
FPSigned funding,
FPUnsigned price
) public returns (
FPSigned pnl,
FPSigned fundingDebt
)
Parameters
Name | Type | Description |
---|---|---|
productId | ProductId | productId of position to evaluate |
isLong | bool | isLong of position to evaluate |
size | FPUnsigned | size of position to evaluate |
positionPrice | FPUnsigned | price of position to evaluate |
margin | uint256 | margin of position to evaluate |
funding | FPSigned | funding of position to evaluate |
price | FPUnsigned | Current price of the position's product |
Return Values
Name | Type | Description |
---|---|---|
pnl | FPSigned | Increase in position value since creation, minus funding |
fundingDebt | FPSigned | Accrued funding (and interest) which was subtracted from this position's PnL. A positive value reduces position value. |
getPositionPnlRate
Compute the current PnL rate (PnL / margin) of a position at the most recent price.
function getPositionPnlRate(PositionId positionId) public returns (FPSigned pnlRate)
Parameters
Name | Type | Description |
---|---|---|
positionId | PositionId | Globally-unique ID of the position in question |
Return Values
Name | Type | Description |
---|---|---|
pnlRate | FPSigned | PnL rate of this position. 100% = 1.0 |
isManagerFor
Whether manager
is a registered manager approved by account
.
function isManagerFor(address manager, address account) public view returns (bool isManager)
Parameters
Name | Type | Description |
---|---|---|
manager | address | Possible manager (for instance, an OrderBook) |
account | address | User that may have approved manager |
Return Values
Name | Type | Description |
---|---|---|
isManager | bool | Whether manager is a registered manager approved by account . |
validateOI
Revert if balance
times utilizationMultiplier is less than OI
function validateOI(uint256 balance) external view
Triggers after each transfer of dfUSDC
Parameters
Name | Type | Description |
---|---|---|
balance | uint256 | Balance of DomFiVault |
getTotalPnl
Compute total trader PnL across all positions, ignoring maxPositionProfit
function getTotalPnl() external returns (FPSigned totalPnl)
Used for withdraw delays. Computed with composite positions.
Return Values
Name | Type | Description |
---|---|---|
totalPnl | FPSigned | totalPnl Sum of all position PnLs if there was no maxPositionProfit |
getProduct
Get product details. See product.
function getProduct(ProductId productId) external view returns (struct IExchangeDomain.Product product)
Parameters
Name | Type | Description |
---|---|---|
productId | ProductId | ID of this product |
Return Values
Name | Type | Description |
---|---|---|
product | struct IExchangeDomain.Product | Saved parameters for this product |
getPositionId
Compute the global position ID of a user's position
function getPositionId(
address account,
PositionLabel positionLabel
) public view returns (
PositionId positionId
)
Parameters
Name | Type | Description |
---|---|---|
account | address | owner of this position |
positionLabel | PositionLabel | User-defined label for this position |
Return Values
Name | Type | Description |
---|---|---|
positionId | PositionId | Globally unique position ID |
getPosition
Get details of a position. See position
function getPosition(PositionId positionId) external view returns (struct IExchangeDomain.Position position)
Parameters
Name | Type | Description |
---|---|---|
positionId | PositionId |
Return Values
Name | Type | Description |
---|---|---|
position | struct IExchangeDomain.Position | Saved position |
getPosition
Get details of a user position. See position
function getPosition(
address account,
PositionLabel positionLabel
) external view returns (
struct IExchangeDomain.Position position
)
Parameters
Name | Type | Description |
---|---|---|
account | address | Owner of the position |
positionLabel | PositionLabel | User-defined label for this position |
Return Values
Name | Type | Description |
---|---|---|
position | struct IExchangeDomain.Position | Saved position |
getPositions
Get details for many positions. See position
function getPositions(
PositionId[] positionIds
) external view returns (
struct IExchangeDomain.Position[] _positions
)
Parameters
Name | Type | Description |
---|---|---|
positionIds | PositionId[] | Globally-unique position IDs |
Return Values
Name | Type | Description |
---|---|---|
_positions | struct IExchangeDomain.Position[] | Saved positions. May be uninitialized |
addProduct
Add a new product
function addProduct(struct IExchangeDomain.ProductParams _product) external
Parameters
Name | Type | Description |
---|---|---|
_product | struct IExchangeDomain.ProductParams | See IDomFiPerp.ProductParams |
updateProductMaxLeverage
Update the max leverage of a product
function updateProductMaxLeverage(ProductId _productId, FPUnsigned _maxLeverage) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to change |
_maxLeverage | FPUnsigned | See IDomFiPerp.Product.maxLeverage |
updateProductFee
Update the base trade fee for a product
function updateProductFee(ProductId _productId, FPUnsigned _fee) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to change |
_fee | FPUnsigned | See IDomFiPerp.Product.fee |
updateProductActive
Enable or disable a product
function updateProductActive(ProductId _productId, bool _isActive) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to (de)activate |
_isActive | bool | IDomFiPerp.Product.isActive |
updateProductMinPriceChange
Update the minimum price change for positions on a product.
function updateProductMinPriceChange(ProductId _productId, FPUnsigned _minPriceChange) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to change |
_minPriceChange | FPUnsigned | See IDomFiPerp.Product.minPriceChange |
updateProductLiquidationBounty
Update the liquidation bounty for positions on a product
function updateProductLiquidationBounty(ProductId _productId, FPUnsigned _liquidationBounty) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to change |
_liquidationBounty | FPUnsigned | See IDomFiPerp.Product.liquidationBounty |
updateProductExposureMultiplier
Update a product's exposure multiplier
function updateProductExposureMultiplier(ProductId _productId, FPUnsigned _exposureMultiplier) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to change |
_exposureMultiplier | FPUnsigned | See IDomFiPerp.Product.exposureMultiplier |
updateProductReserveMultiplier
Update a product's reserve multiplier
function updateProductReserveMultiplier(ProductId _productId, FPUnsigned _reserveMultiplier) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to change |
_reserveMultiplier | FPUnsigned | See IDomFiPerp.Product.reserveMultiplier |
updateProductWeight
Update a product's weight for exposure calculations
function updateProductWeight(ProductId _productId, FPUnsigned _weight) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to change |
_weight | FPUnsigned | See IDomFiPerp.Product.weight |
updateProductLiquidationThreshold
Update the liquidation threshold for positions on a product
function updateProductLiquidationThreshold(ProductId _productId, FPUnsigned _liquidationThreshold) external
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | Product to change |
_liquidationThreshold | FPUnsigned | See IDomFiPerp.Product.liquidationThreshold |
updateProductFundingExponent
Set product funding exponent. Funding rate is (productExposure / maxExposure) ^ fundingExponent * fundingMultiplier
function updateProductFundingExponent(ProductId _productId, FPUnsigned _fundingExponent) external
Set to 1 to "disable".
Parameters
Name | Type | Description |
---|---|---|
_productId | ProductId | ID of product to change funding exponent |
_fundingExponent | FPUnsigned | New funding exponent. Must be > 0 |
updatePrice
Emit a PriceUpdate event for easy off-chain data access
function updatePrice(ProductId productId, FPUnsigned value, uint64 timestamp, address oracle) external
This allows order bots to get price events without tracking oracle changes.
Parameters
Name | Type | Description |
---|---|---|
productId | ProductId | ID of product with price update |
value | FPUnsigned | New price |
timestamp | uint64 | Timestamp at which price was generated |
oracle | address | Source of price update |
managers
function managers(address _manager) external view returns (bool)
setManager
(De)register an address as a manager. With permission, managers can change positions on behalf of other addresses.
function setManager(address _manager, bool _isActive) external
Parameters
Name | Type | Description |
---|---|---|
_manager | address | Address to affect |
_isActive | bool | Whether this address should be a manager. |
approvedManagers
function approvedManagers(address _account, address _manager) external view returns (bool)
setAccountManager
Permit or prohibit another address from changing the sender's positions
function setAccountManager(address _manager, bool _isActive) external
Parameters
Name | Type | Description |
---|---|---|
_manager | address | Address to approve or remove, e.g. OrderBook |
_isActive | bool | Whether this address may change the sender's positions. |
minMargin
function minMargin() external view returns (uint256)
setMinMargin
Set the minimum margin increment for all positions
function setMinMargin(uint256 _minMargin) external
USD value of minMargin * liquidationThreshold * liquidationBounty should be greater than
the typical USD value of gas to call liquidatePosition
when the network is congested.
Parameters
Name | Type | Description |
---|---|---|
_minMargin | uint256 | Prevent actions that would create a position with less than this much collateral |
isTradeEnabled
function isTradeEnabled() external view returns (bool)
setTradeEnabled
Enable or disable trading
function setTradeEnabled(bool _isTradeEnabled) external
Parameters
Name | Type | Description |
---|---|---|
_isTradeEnabled | bool | Whether positions can be created/increased or not |
maxShift
function maxShift() external view returns (FPUnsigned)
setMaxShift
Set max exposure-balancing slippage. See maxShift
function setMaxShift(FPUnsigned _maxShift) external
Parameters
Name | Type | Description |
---|---|---|
_maxShift | FPUnsigned | New value for maxShift. 0-1% |
minProfitTime
function minProfitTime() external view returns (uint256)
setMinProfitTime
Set delay before positions with < minProfit can be cashed out
function setMinProfitTime(uint256 _minProfitTime) external
Parameters
Name | Type | Description |
---|---|---|
_minProfitTime | uint256 | New value for minProfitTime |
allowPublicLiquidator
function allowPublicLiquidator() external view returns (bool)
setAllowPublicLiquidator
Allow anybody to liquidate positions, or restrict it to approved addresses only
function setAllowPublicLiquidator(bool _allowPublicLiquidator) external
Parameters
Name | Type | Description |
---|---|---|
_allowPublicLiquidator | bool | New value for allowPublicLiquidator |
isManagerOnlyForOpen
function isManagerOnlyForOpen() external view returns (bool)
setIsManagerOnlyForOpen
Restrict position increases to approved addresses, or open to all
function setIsManagerOnlyForOpen(bool _isManagerOnlyForOpen) external
Parameters
Name | Type | Description |
---|---|---|
_isManagerOnlyForOpen | bool | New value for isManagerOnlyForOpen |
isManagerOnlyForClose
function isManagerOnlyForClose() external view returns (bool)
setIsManagerOnlyForClose
Restrict position decreases to approved addresses, or open to all
function setIsManagerOnlyForClose(bool _isManagerOnlyForClose) external
Parameters
Name | Type | Description |
---|---|---|
_isManagerOnlyForClose | bool | New value for isManagerOnlyForClose |
utilizationMultiplier
function utilizationMultiplier() external view returns (FPUnsigned)
setUtilizationMultiplier
Set the utilization multiplier. Open interest is restricted to utilizationMultiplier times the vault balance
function setUtilizationMultiplier(FPUnsigned _utilizationMultiplier) external
Parameters
Name | Type | Description |
---|---|---|
_utilizationMultiplier | FPUnsigned | New value for utilizationMultiplier |
maxExposureMultiplier
function maxExposureMultiplier() external view returns (FPUnsigned)
setMaxExposureMultiplier
Set the max exposure multiplier. Exposure in any product is limited to maxExposureMultiplier times its share of the vault-level max exposure.
function setMaxExposureMultiplier(FPUnsigned _maxExposureMultiplier) external
Parameters
Name | Type | Description |
---|---|---|
_maxExposureMultiplier | FPUnsigned | New value, > 0, for maxExposureMultiplier. |
helpfulShiftScaler
function helpfulShiftScaler() external view returns (FPUnsigned)
setHelpfulShiftScaler
Set helpfulShiftScaler. Prices are shifted in proportion to exposure. For "helpful" orders that reduce exposure, scale price shift.
function setHelpfulShiftScaler(FPUnsigned _helpfulShiftScaler) external
This should be < 1. Set to 1 to disable.
Parameters
Name | Type | Description |
---|---|---|
_helpfulShiftScaler | FPUnsigned | New value, >0, for helpfulShiftScaler. |
maxPositionProfit
function maxPositionProfit() external view returns (FPUnsigned)
setMaxPositionProfit
function setMaxPositionProfit(FPUnsigned _maxPositionProfit) external
liquidators
function liquidators(address _liquidator) external view returns (bool)
setLiquidator
Register or deregister an address as an active liquidator
function setLiquidator(address _liquidator, bool _isActive) external
Parameters
Name | Type | Description |
---|---|---|
_liquidator | address | Address to (de)activate as a liquidator |
_isActive | bool | Whether it can liquidate when !allowPublicLiquidators |
pauseTrading
Pause trading, preventing anyone from opening new positions or increasing existing ones. Guardian only.
function pauseTrading() external