Docs · Guides
Open and close a perp
Open a leveraged long or short against the perps vault at the oracle price, read the position, change its collateral and close it, in the app or from code.
A perp on Mesa is a sealed position against the perps vault. It opens and closes at the oracle price, so there is no order book and no counterparty to find. Its size, direction and liquidation price are readable only by you.
Before you open
- A Mesa balance of the market's collateral asset. See Deposit and withdraw.
- A plan for renewal. A perp must be renewed before each deadline, like a borrow. Read Keep positions renewed.
Each market on Perps shows its oracle price, funding, open interest long and short, and the vault's size.
Open
1. Open Perps and choose the market.
2. In "Open a sealed position", choose Long or Short.
3. Enter the collateral and choose the leverage.
4. Check "Size", "Liquidation price, yours to see" and the open fee. Press "Review position".
5. Press "Open position".const { position } = await mesa.perp({
marketId: 'SOL-PERP',
side: 'long',
collateral: 500_000_000n, // 500 USDC
sizeBase: 10_000_000_000n, // 10 SOL
})In the app you choose collateral and leverage, and the size follows: collateral times leverage, divided by the oracle price. From code you give the collateral and the size directly.
The position's entry price is the oracle price at the moment you place it. The proof made then shows the position is within the market's maximum leverage and above its maintenance margin.
The open goes through a batch. When the batch clears, the market adds the position to its open interest. If that would pass the market's open interest limit, the open is refused, and Close on the position returns the collateral.
On the batch record, the market's open interest on your side rises by the size.
Read the position
| Shown | Meaning |
|---|---|
| Collateral | What the position holds |
| Entry | The oracle price it opened at |
| Unrealized | Its result at the current oracle price, before fees and funding |
| Health | 1.00 is the maintenance margin. Above it the position can renew. |
| Liquidation price | The oracle price at which health reaches 1.00. Shown only to you. |
| Next renewal | Time left until the position's deadline |
| Renewed by | This browser, the watcher, or your own script |
From code: entryPrice, unrealizedQuote, health, liquidationPrice, nextDeadlineAt and renewedBy on each entry of mesa.snapshot().positions.
Funding
While longs outweigh shorts in a market, longs pay funding and shorts receive it, and the reverse when shorts outweigh longs. Funding accrues against your position each time the market clears a batch. It counts in health at every renewal and is settled when the position closes. See Funding.
Change collateral
1. On the market's page, select "Collateral" in the position panel.
2. Choose "Add collateral" or "Remove collateral" and enter the amount.
3. Press "Review" and confirm.const account = mesa.mesa
await account.run({ kind: 'add_collateral', positionId: position!.id, amount: 100_000_000n }, console.log)
await account.run({ kind: 'remove_collateral', positionId: position!.id, amount: 50_000_000n }, console.log)Both land at once, prove the position healthy after the change, and move its deadline one heartbeat on. Adding collateral moves the liquidation price away from the market.
Close
Press "Close" on the position.await mesa.close({ positionId: position!.id })A close goes through the next batch and settles at the oracle price that batch's clear reads. Your balance receives
collateral + result − open fee − close fee − funding owedA gain is paid from the perps vault and a loss is paid into it. The open fee is charged on the size at the entry price and the close fee on the size at the closing price. Rates are in Fees and MESA.
If the vault cannot pay the gain in that batch, the close is refused and the position stays open. Take the close back, and the position returns with its collateral, its entry and a new deadline:
The position's line reads "Close not done in batch N". Press "Restore the position".const p = mesa.snapshot().positions.find((x) => x.id === position!.id)
if (p?.pendingClose?.state === 'reclaimable') {
await mesa.mesa.run({ kind: 'reclaim', orderId: p.pendingClose.actionId }, console.log)
}While a close is in its batch, the line reads "Closing in batch N" and the position's other actions are hidden.
What can stop a perp
| You see | Cause | Do |
|---|---|---|
| "More than your Mesa balance of …" | The collateral exceeds the free balance | Enter less or deposit |
MarketPaused | The perp market is paused | Opens resume when it is unpaused. Closing runs while paused. |
StaleOracle | The oracle price is older than the program accepts | Try again when the price updates |
ProofVerificationFailed | The oracle price moved between proving and landing | Try again. The proof is rebuilt at the new price. |
| A refused open | The market's open interest limit was reached in that batch | Close the position to take the collateral back, then open a smaller one |
| A refused close | The perps vault could not pay the gain in that batch | Reclaim the close. The position comes back with its collateral, its entry and a new deadline. Close it again in a later batch. |
If you stop renewing
A perp that passes its deadline unrenewed lapses, and its contents become readable. If its margin is still above maintenance, renewing seals it again. If not, anyone can liquidate it: the liquidator receives the market's liquidation bonus from the collateral and the rest goes to the perps vault. See Liquidation.