Vertex Docs
  • Getting Started
    • 📋Overview
    • 📜Table of Contents
    • 🚀Mission
    • ⚔️Vertex Edge
      • 1️⃣Vertex on Arbitrum
      • 2️⃣Blitz on Blast
        • Blitz Documentation
      • 3️⃣Vertex on Mantle
        • 🎆Trading Rewards
        • 📊Perpetual & Spot Market Specifications
        • 🖥️Open-Source Contracts
        • 🔌API
      • 4️⃣Vertex on Sei
        • 💰Trading Rewards
        • 📊Perpetual & Spot Market Specifications
        • 🖥️On-Chain Contracts
        • 🔌API
      • 5️⃣Vertex on Base
        • 💰Trading Rewards
        • 📊Perpetual & Spot Market Specifications
        • 💻Onchain Contracts
        • 🔌API
        • ✍️Create a Coinbase Smart Wallet
      • 6️⃣Vertex on Sonic
        • 💰Trading Rewards
        • 📊Perpetual & Spot Market Specifications
        • ⛓️On-Chain Contracts
        • 🔌API
      • 7️⃣Vertex on Abstract
        • 📊Market Specifications
        • ⛓️On-Chain Contracts
        • 🔌API
      • 8️⃣Bro.Trade on Berachain
        • ⛓️On-Chain Contracts
        • 🔌API
      • 9️⃣Vertex on Avalanche
        • 💰Trading Rewards
        • 📊Market Specifications
        • ⛓️On-Chain Contracts
        • 🔌API
  • Basics
    • 📽️Products
    • ⚡Technical Architecture
    • 🧵Universal Cross Margin
    • ✂️Isolated Margin
    • ℹ️Subaccounts & Health
    • ⏰Liquidations & Insurance Fund
    • 💵PnL Settlements
    • 📊Pricing (Oracles)
    • 💰Fees
    • 📈Funding Rates
    • 📅Market Specifications
    • ⚡One-Click Trading
    • 🔫Trigger Orders
    • 🗂️GMCI 30 & MEME Indices
    • 💰Trade & Earn
      • 🤝Maker Program
      • 🔁Cross-Chain VRTX Rewards
      • ❓Trading Rewards - FAQs
    • 🧑‍🤝‍🧑Referral Program
    • ⚒️Resources & Links
    • ❔FAQs
  • VRTX Token
    • 🎓VRTX Tokenomics
    • 📊Token Supply Distribution
  • More
    • 🌐Glossary
    • 🔬Contracts & Code Audits
    • 🦋Bug Bounties
    • 💽Data & Research
    • 🪟Maintenance Windows
    • 🏁Terms of Use
    • ✍️Statement of Risk
    • 🌍Restricted Territories
    • 📸Brand Assets
  • Tutorials
    • 🏁Quick Start
    • 🖱️One-Click Trading
    • ⚙️Trading Basics
    • ♾️Perpetuals
    • 🔮Spot
    • 🧑‍🤝‍🧑Multiple Accounts
    • 🔫TP/SL & Stop-Market
    • 💰Withdrawals
    • ⚡Fast Withdrawals
    • 🌉Cross-Chain Deposits
    • 🔄Lend, Borrow & Repay
    • ❤️Margin Health
    • 💧Pools
    • 💫VRTX Staking
    • ↔️Switching Chains
    • 🪙Trading Rewards
    • ✂️Shortcuts
  • Developer Resources
    • API
      • 🔌Endpoints
      • Gateway
        • Executes
          • Place Order
          • Place Isolated Order
          • Cancel Orders
          • Cancel Product Orders
          • Cancel And Place
          • Withdraw Collateral
          • Transfer Quote
          • Liquidate Subaccount
          • Mint LP
          • Burn LP
          • Link Signer
        • Queries
          • Status
          • Contracts
          • Nonces
          • Order
          • Orders
          • Subaccount Info
          • Isolated Positions
          • Market Liquidity
          • Symbols
          • All Products
          • Edge All Products
          • Market Prices
          • Max Order Size
          • Max Withdrawable
          • Max LP Mintable
          • Fee Rates
          • Health Groups
          • Linked Signer
          • Insurance
          • Min Deposit Rates
        • Signing
          • Examples
          • Q&A
      • Subscriptions
        • Authentication
        • Streams
        • Events
        • Rate limits
      • Archive (indexer)
        • Orders
        • Matches
        • Events
        • Summary
        • Candlesticks
        • Edge Candlesticks
        • Funding Rate
        • Interest & funding payments
        • Oracle Price
        • Perp Prices
        • Product Snapshots
        • Market Snapshots
        • Edge Market Snapshots
        • Maker Statistics
        • Merkle Proofs
        • Rewards
        • Referral Code
        • Liquidation Feed
        • Linked Signer Rate Limit
        • Subaccounts
        • Signatures
        • USDC Price
      • Trigger
        • Executes
          • Place Order
          • Cancel Orders
          • Cancel Product Orders
        • Queries
          • List Trigger Orders
      • V2
        • Assets
        • Pairs
        • APR
        • Orderbook
        • Tickers
        • Contracts
        • Trades
        • VRTX Token
      • Rate limits
      • Errors
      • Symbols
      • Depositing
      • Withdrawing (on-chain)
      • Integrate via Smart Contracts
      • Definitions / Formulas
    • Vertex Typescript SDK
      • Getting Started
      • How To
        • Create a Vertex client
        • Useful Common Functions
        • Query Markets & Products
        • Deposit Funds
        • Withdraw Funds
        • Manage Orders
      • User Guide
        • Client
          • Market
            • Place Order
            • Cancel Orders
            • Cancel Product Orders
            • Mint LP
            • Burn LP
            • Get All Markets
            • Get Latest Market Price
            • Get Market Liquidity
            • Get Open Subaccount orders
            • Get Historical Orders
            • Get Max Order Size
            • Get Max Mint LP
            • Get Candlesticks
            • Get Funding Rate
            • Get Product Snapshots
          • Spot
            • Approve Allowance
            • Deposit
            • Withdraw
            • Get Max Withdrawable
            • Get Token Contract for Product
            • Get Token Wallet Balance
            • Get Token Allowance
          • Perp
            • Get prices
          • Subaccount
            • Link Signer
            • Get Subaccount Linked Signer with Rate limits
            • Get Subaccount Summary
            • Get Subaccount Fee Rates
            • Get Subaccount Token Rewards
        • Engine Client
          • Executes
          • Queries
        • Indexer Client
          • Base Queries
          • Paginated Queries
        • Websocket
          • Executes
          • Queries
          • Subscriptions
        • Utilities
          • Contracts
          • Utils
      • API Reference
    • Vertex Python SDK
    • Vertex Rust SDK
    • Contracts
    • API/SDK Terms of Use
Powered by GitBook
On this page
  • Import the functions
  • Scaffold Your Subaccount
  • Create an order
  • Place the order
  • Alternative order placement
  • Order digest
  • Query orders on the subaccount
  • Cancel order
  • Query Orders to Verify Cancellation
  • Clean up
  • Full example
Export as PDF
  1. Developer Resources
  2. Vertex Typescript SDK
  3. How To

Manage Orders

PreviousWithdraw FundsNextUser Guide

Last updated 2 months ago

This guide shows you how to:

  • Create an order.

  • Place an order.

  • Cancel an order.

  • Query orders by subaccount.

If you inspect the underlying types for these transactions, you'll notice that a nonce field is required. This is a unique integer in ascending order. Our off-chain engine has a nonce query to return the latest nonce for a given subaccount. All this is abstracted away within the SDK, so you do not need to manually use this query.

Import the functions

import { getVertexClient, prettyPrintJson } from "./common";
import { OrderActionParams } from "@vertex-protocol/client";
import { nowInSeconds, toFixedPoint } from "@vertex-protocol/utils";
import { getExpirationTimestamp } from "@vertex-protocol/contracts";

Scaffold Your Subaccount

To place orders, we need a subaccount with funds. We need to perform the step as before, this time with 1000 USDC.

const vertexClient = getVertexClient();
const { walletClient, publicClient } = vertexClient.context;

const address = walletClient!.account.address;
const subaccountName = 'default';
const depositAmount = toFixedPoint(1000, 6);

const mintTxHash = await vertexClient.spot._mintMockERC20({
  amount: depositAmount,
  productId: 0,
});
await publicClient.waitForTransactionReceipt({
  hash: mintTxHash,
});

const approveTxHash = await vertexClient.spot.approveAllowance({
  amount: depositAmount,
  productId: 0,
});
await publicClient.waitForTransactionReceipt({
  hash: approveTxHash,
});

const depositTxHash = await vertexClient.spot.deposit({
  subaccountName: 'default',
  amount: depositAmount,
  productId: 0,
});
await publicClient.waitForTransactionReceipt({
  hash: depositTxHash,
});

await new Promise((resolve) => setTimeout(resolve, 10000));

Create an order

Placing an order requires a number of parameters, represented by the PlaceOrderParams['order'] type.

In the example below:

  • The order expiration time is given by calling the nowInSeconds function from the utils package and adding 60 seconds. This means the order will expire 60 seconds from now.

    • Notice the usage of getExpirationTimestamp, order types, such as IOC (immediate or cancel) are encoded in the expiration field, the getExpirationTimestamp function will return the correct expiration timestamp for the given order type.

  • The price field is set at 80000 - a low value (at the time of writing) to prevent execution. This enables us to cancel the order later on without it being instantly filled. Please adjust this price accordingly.

  • The amount field is set at 10**16 - this is the amount to buy/sell. A positive value is to buy, negative is to sell.

    • Amount is normalized to 18 decimal places, which is what toFixedPoint does by default.

    • NOTE: Min limit order size for BTC is 10**16 and for ETH is 10**17. Orders below these sizes will fail to be placed.

const orderParams: PlaceOrderParams['order'] = {
  // When using the `placeOrder` call, the `subaccountOwner` defaults to the
  // address of the `walletClient`
  subaccountName: 'default',
  expiration: getExpirationTimestamp({
    expirationTime: nowInSeconds() + 60,
    reduceOnly: false,
    type: 'post_only',
  }),
  price: 80000,
  // Setting order amount to 10**16
  amount: toFixedPoint(0.01, 18),
};

Place the order

Use the order parameters to place the order with the placeOrder function.

const placeOrderResult = await vertexClient.market.placeOrder({
  order: orderParams,
  productId: 2,
  // Used for spot orders to enable/disable borrowing
  spotLeverage: undefined,
});

prettyPrintJson("Place Order Result", placeOrderResult);

Alternative order placement

Alternatively, you can manually use payloadBuilder to manually generate the place order payload. This may be useful in cases where you want to build the tx separately from sending the execute API call.

// Use one of the following to generate a payload
vertexClient.context.engineClient.payloadBuilder.buildPlaceOrderPayload(...)
vertexClient.context.engineClient.payloadBuilder.buildPlaceOrderPayloadSync(...)

// Then execute 
vertexClient.context.engineClient.execute('place_order', placeOrderPayload.payload);

Order digest

You can optionally generate the order digest, which can then be used to further manage the order e.g: cancelling the order. The order digest is also returned upon executing the placeOrder transaction.

import { getOrderDigest } from "@vertex-protocol/contracts";

const productId = 2;
const orderParams: EIP712OrderParams = {
  subaccountOwner: address,
  subaccountName,
  expiration: getExpirationTimestamp({
    expirationTime: nowInSeconds() + 60,
    reduceOnly: false,
    type: 'post_only',
  }),
  price: 80000,
  // Setting order amount to 10**16
  amount: toFixedPoint(0.01, 18),
  nonce: getOrderNonce(),
};

// Optional: generate a digest ahead of time so that you can manage the order, alternatively, the digest
// will be returned via placeOrderResult.data.digest
const orderbookAddress =
  await vertexClient.context.engineClient.getOrderbookAddress(productId);
const orderDigest = getOrderDigest({
  chainId: walletClient!.chain.id,
  order: orderParams,
  verifyingAddr: orderbookAddress,
});

Query orders on the subaccount

Now we can query the subaccount for open orders with the getOpenSubaccountOrders function.

const openOrders = await vertexClient.market.getOpenSubaccountOrders({
    subaccountOwner: address,
    subaccountName,
    productId: 2,
  });

prettyPrintJson('Subaccount Open Orders', openOrders);

Cancel order

Cancel the order using the digest of the placed order. You can cancel multiple orders at once.

const cancelOrderResult = await vertexClient.market.cancelOrders({
  digests: [placeOrderResult.data.digest],
  productIds: [2],
  subaccountName: 'default',
});

prettyPrintJson('Cancel Order Result', cancelOrderResult);

Query Orders to Verify Cancellation

Clean up

Finally, clean up by withdrawing the same amount as you have deposited, minus the 1 USDC withdrawal fee.

await vertexClient.spot.withdraw({
  productId: 0,
  amount: depositAmount - toFixedPoint(1, 6),
  subaccountName,
});

Full example

import { PlaceOrderParams } from '@vertex-protocol/client';
import { getExpirationTimestamp } from '@vertex-protocol/contracts';
import { nowInSeconds, toFixedPoint } from '@vertex-protocol/utils';
import { getVertexClient, prettyPrintJson } from './common';

async function main() {
  const vertexClient = getVertexClient();
  const { walletClient, publicClient } = vertexClient.context;

  const address = walletClient!.account.address;
  const subaccountName = 'default';
  const depositAmount = toFixedPoint(1000, 6);

  const mintTxHash = await vertexClient.spot._mintMockERC20({
    amount: depositAmount,
    productId: 0,
  });
  await publicClient.waitForTransactionReceipt({
    hash: mintTxHash,
  });

  const approveTxHash = await vertexClient.spot.approveAllowance({
    amount: depositAmount,
    productId: 0,
  });
  await publicClient.waitForTransactionReceipt({
    hash: approveTxHash,
  });

  const depositTxHash = await vertexClient.spot.deposit({
    subaccountName: 'default',
    amount: depositAmount,
    productId: 0,
  });
  await publicClient.waitForTransactionReceipt({
    hash: depositTxHash,
  });

  await new Promise((resolve) => setTimeout(resolve, 10000));

  const orderParams: PlaceOrderParams['order'] = {
    subaccountName,
    expiration: getExpirationTimestamp({
      expirationTime: nowInSeconds() + 60,
      reduceOnly: false,
      type: 'post_only',
    }),
    price: 80000,
    // Setting order amount to 10**16
    amount: toFixedPoint(0.01, 18),
  };

  const placeOrderResult = await vertexClient.market.placeOrder({
    order: orderParams,
    productId: 2,
    // Used for spot orders to enable/disable borrowing
    spotLeverage: undefined,
  });

  prettyPrintJson('Place Order Result', placeOrderResult);

  const openOrders = await vertexClient.market.getOpenSubaccountOrders({
    subaccountOwner: address,
    subaccountName,
    productId: 2,
  });

  prettyPrintJson('Subaccount Open Orders', openOrders);

  const cancelOrderResult = await vertexClient.market.cancelOrders({
    digests: [placeOrderResult.data.digest],
    productIds: [2],
    subaccountName: 'default',
  });

  prettyPrintJson('Cancel Order Result', cancelOrderResult);

  await vertexClient.spot.withdraw({
    productId: 0,
    amount: depositAmount - toFixedPoint(1, 6),
    subaccountName,
  });
}

main();

Run again to make sure the cancellation was successful.

deposit funds
query orders on the subaccount