Open Position
The openPosition
function allows users to open a new position in the perpetual futures market.
Function Signature
Parameters
collateralToken
(string): The token used as collateral for the positionindexToken
(string): The token being tradedleverage
(number): The leverage multiplier for the positioncollateral
(number): The amount of collateral to be usedpositionConfig
(IPositionConfig): Configuration object for the positioncoinObjects
(string[]): Array of coin object IDs to be usedlong
(boolean): Whether this is a long (true) or short (false) positionindexPrice
(number): The market price or limit price of the index token. Refer to subOraclePrices on how you can get the token prices via Pyth using our provided API with a working example.
Note: For market order, the index price parameter will not be used. The smart contract internally will use Pyth to get the current index price. For limit order, the index price parameter will be used and set as the limited order price when the order is executed.
collateralPrice
(number): The market price of the collateral token. Refer to subOraclePrices on how you can get the token prices via Pyth using our provided API with a working example.pricesSlippage
(number, default: 0.003): Maximum allowed slippage for pricescollateralSlippage
(number, default: 0.5): Maximum allowed slippage for collateralisLimitOrder
(boolean, default: false): Whether this is a limit orderisIocOrder
(boolean, default: false): Whether this is an Immediate-or-Cancel order
Note: This parameter is currently a placeholder.
relayerFee
(bigint, default: BigInt(1)): Fee paid to the relayer
Returns
Promise<Transaction>
: A promise that resolves to a transaction object.
Usage Example
Notes
Ensure you have sufficient balance and have approved the necessary permissions before calling this function.
The function uses the current oracle prices for the tokens. Ensure your frontend is updated with the latest prices before calling this function.
The
pricesSlippage
andcollateralSlippage
parameters allow you to control the maximum allowed price movement. Adjust these based on market volatility and your risk tolerance.For limit orders, set
isLimitOrder
totrue
.The
relayerFee
is paid in SUI. Adjust this value based on the current network conditions and relayer requirements.
Error Handling
This function may throw errors if:
The input parameters are invalid
There's insufficient balance
The slippage tolerance is exceeded
The position size is outside allowed limits
Always wrap the function call in a try-catch block and handle potential errors appropriately in your application.
Last updated