Skip to main content
This example walks through the full perp lifecycle on Monaco using @0xmonaco/core: depositing collateral, funding a parent margin account, opening a leveraged position, attaching TP/SL conditional orders, and closing. For concepts, see Margin, Account Vault, and Positions. For the spot-only walkthrough, see Complete Integration Example.

Installation

Complete Example

What this example covers

  1. Deposit — using the standard spot vault flow; collateral has to land on Monaco before it can be transferred to a margin account.
  2. Trading pair lookup — perps are filtered by marketType === "MARGIN".
  3. Parent margin account funding — one collateral asset per account; USDC is the default.
  4. Preflight via simulateRiskBucketOrderRisk — never place a non-trivial perp order without checking acceptance and projected liquidation.
  5. Order placement with perp options — tradingMode: "MARGIN" and leverage are the key perp options; side sets the one-way direction.
  6. Position lookup after fill — order events stream over userOrders; the position itself appears in listPositions shortly after.
  7. Live risk inspection — getPositionRisk is the right call for live UI updates (cheaper than getPosition).
  8. TP/SL via attachPositionTpSl — pass oco: true so the two legs cancel each other on trigger.
  9. Conditional order lifecycle subscription — sdk.ws.conditionalOrders streams TP/SL state changes.
  10. Close — partial or full, MARKET or LIMIT.

Common variations

  • Reduce-only manual close. Instead of closePosition, use placeMarketOrder with reduceOnly: true — useful when you want to apply slippage protection or specific TIF.
  • Withdraw collateral. After closing all positions, use transferCollateralFromParentMarginAccount to move USDC back to the wallet, then sdk.vault.withdraw(...) to leave Monaco entirely.