POST
/
api
/
closeposition
Close Position
curl --request POST \
  --url https://unidexv4-api-production.up.railway.app/api/closeposition \
  --header 'Content-Type: application/json' \
  --data '{
  "positionId": "<string>",
  "sizeDelta": 123,
  "allowedPrice": 123,
  "takeProfit": 123,
  "stopLoss": 123,
  "takeProfitClosePercent": 123,
  "stopLossClosePercent": 123
}'
{
  "calldata": "<string>",
  "vaultAddress": "<string>",
  "requiredGasFee": "<string>"
}

Overview

The Close Position endpoint allows you to close an existing position (partially or fully) or modify its take profit and stop loss orders. You can specify the amount to close and set price limits for the closing transaction.

Request Body

positionId
string
required
ID of the position to close or modify
sizeDelta
number
Optional. Amount to decrease the position by. If not provided, closes entire position
allowedPrice
number
Optional. Maximum (for longs) or minimum (for shorts) allowed price for closing
takeProfit
number
Optional. New take profit price. Set to modify existing take profit order
stopLoss
number
Optional. New stop loss price. Set to modify existing stop loss order
takeProfitClosePercent
number
Required if takeProfit is set. Percentage to close at take profit (in basis points, 10000 = 100%)
stopLossClosePercent
number
Required if stopLoss is set. Percentage to close at stop loss (in basis points, 10000 = 100%)

Response Fields

calldata
string
Encoded function call data for the transaction
vaultAddress
string
Address of the vault contract
requiredGasFee
string
Required gas fee for the transaction

Example Usage

Full Position Close

curl -X POST "https://unidexv4-api-production.up.railway.app/api/closeposition" \
  -H "Content-Type: application/json" \
  -d '{
    "positionId": "123456",
    "allowedPrice": 67000
}'

Partial Position Close

curl -X POST "https://unidexv4-api-production.up.railway.app/api/closeposition" \
  -H "Content-Type: application/json" \
  -d '{
    "positionId": "123456",
    "sizeDelta": 10.5,
    "allowedPrice": 67000
}'

Modify Take Profit/Stop Loss

curl -X POST "https://unidexv4-api-production.up.railway.app/api/closeposition" \
  -H "Content-Type: application/json" \
  -d '{
    "positionId": "123456",
    "takeProfit": 69000,
    "stopLoss": 61000,
    "takeProfitClosePercent": 10000,
    "stopLossClosePercent": 10000
}'

Example Response

{
  "calldata": "0x4d59524546455252414c434f4445000000000000000000000000000000000000",
  "vaultAddress": "0xe3ca135782e4a17aFb31a63ee3b15351C891A1A2",
  "requiredGasFee": "0.0012"
}

Common Use Cases

  1. Full Position Close
    • Only specify positionId and optionally allowedPrice
    • Closes entire position
  2. Partial Position Close
    • Specify positionId, sizeDelta, and optionally allowedPrice
    • Closes specified amount of position
  3. Modify Take Profit/Stop Loss
    • Specify positionId, takeProfit/stopLoss, and corresponding close percentages
    • Updates or adds take profit/stop loss orders without closing position

Notes

  • allowedPrice helps prevent slippage - transaction will fail if price moves beyond this limit
  • Close percentages are in basis points (10000 = 100%)
  • Take profit and stop loss can be modified without closing the position
  • sizeDelta must be less than or equal to current position size

Body

application/json

Response

200
application/json

Successful response

The response is of type object.