> ## Documentation Index
> Fetch the complete documentation index at: https://docs.upsidemax.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# updateMarginMode: Switch Between Cross and Isolated Margin

> Switch a contract between cross margin (shared pool) and isolated margin (per-position allocation), subject to having no open positions or orders.

The `updateMarginMode` action switches the margin mode for a specific contract between **cross** and **isolated**. In cross margin mode, your positions share a common collateral pool and can draw on your full available balance. In isolated margin mode, each position is allocated a fixed amount of collateral that you control explicitly.

Before switching modes, the contract must have no open positions and no active orders. If either condition is not met, the exchange rejects the request — close all positions and cancel all orders on the contract first.

<Info>
  This action uses full field names (`asset`, `isCross`) rather than the compact single-character keys used by order and cancel actions.
</Info>

<Info>
  This action is idempotent: setting the same margin mode that is already active returns a success response without modifying any state.
</Info>

## Endpoint

```
POST https://dev.upsidemax.xyz/exchange
```

## Request

All requests must include an ECDSA signature and a millisecond-precision nonce.

```json theme={"system"}
{
  "action": {
    "type": "updateMarginMode",
    "asset": 1,
    "isCross": false
  },
  "signature": { "r": "0x...", "s": "0x...", "v": 28 },
  "nonce": 1778859200000
}
```

### Action fields

<ParamField body="type" type="string" required>
  Fixed value: `"updateMarginMode"`.
</ParamField>

<ParamField body="asset" type="int32" required>
  The contract ID to update the margin mode for.
</ParamField>

<ParamField body="isCross" type="boolean" required>
  `true` = switch to **cross** margin mode. `false` = switch to **isolated** margin mode.
</ParamField>

## Response

### Success

```json theme={"system"}
{
  "status": "ok",
  "response": {
    "type": "updateMarginMode",
    "data": { "marginModeBefore": 1, "marginModeAfter": 2 }
  }
}
```

### Rejection

```json theme={"system"}
{
  "status": "ok",
  "response": {
    "type": "updateMarginMode",
    "data": {
      "marginModeBefore": 0,
      "marginModeAfter": 0,
      "errorCode": 11,
      "errorMessage": "cannot switch marginMode with open position"
    }
  }
}
```

<Note>
  Rejections still return HTTP 200 with `status: "ok"`. Check for the presence of `errorCode` in `data` to detect failures.
</Note>

### Response fields

<ResponseField name="marginModeBefore" type="int">
  The margin mode for the contract before the update: `1` = cross, `2` = isolated. Returns `0` on rejection.
</ResponseField>

<ResponseField name="marginModeAfter" type="int">
  The margin mode for the contract after the update: `1` = cross, `2` = isolated. Returns `0` on rejection.
</ResponseField>

<ResponseField name="errorCode" type="int">
  Present only on rejection. Identifies the failure reason.
</ResponseField>

<ResponseField name="errorMessage" type="string">
  Present only on rejection. Human-readable description of the failure.
</ResponseField>

## Error reference

| `errorMessage`                                      | Cause                                                                                                              |
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
| `invalid marginMode`                                | The requested margin mode value is not recognized. Ensure `isCross` is a valid boolean.                            |
| `cannot switch marginMode with open position`       | The contract has at least one open position. Close all positions before switching.                                 |
| `cannot switch marginMode with open orders`         | The contract has at least one active order. Cancel all orders before switching.                                    |
| `ISOLATED margin mode requires HEDGE position mode` | Your account is in ONE\_WAY position mode, which is incompatible with isolated margin. Switch to HEDGE mode first. |
| `MD owner/liquidator account must stay CROSS (R17)` | Your account type is restricted to cross margin and cannot switch to isolated.                                     |
