GuidesTrading

Cancel orders

Remove orders from the book through the API, or cancel them on-chain so their signatures can never be used.

You can stop an order in two ways:

Remove through the APICancel on-chain
HowPOST /v1/orders/remove or /v1/orders/remove-by-hashcancelOrders on the exchange contract
CostFreeA transaction, paid in BNB
When it takes effectRight awayOnce the transaction confirms and Predict picks it up
The signed orderLeaves Predict's book. Its signature stays valid on-chain.Becomes invalid on-chain for good.

Both end with the order's status CANCELLED. Removing through the API is the usual way. Cancel on-chain when you need the signature itself to become unusable.

Remove orders through the API

Send up to 100 order IDs to POST /v1/orders/remove, or up to 100 order hashes to POST /v1/orders/remove-by-hash. Both need the API key and the JWT of the wallet that owns the orders.

const apiKey = "YOUR_API_KEY";
const jwt = "YOUR_JWT"; // from the Authentication guide

const response = await fetch("https://api.predict.fun/v1/orders/remove", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-api-key": apiKey,
    Authorization: `Bearer ${jwt}`,
  },
  body: JSON.stringify({ data: { ids: ["ORDER_ID_1", "ORDER_ID_2"] } }),
});
const result = await response.json();
if (!response.ok) throw new Error(`${result.error}: ${result.message}`);
const { removed, noop, rejected } = result;
console.log({ removed, noop, rejected });

To remove by hash, send { "data": { "hashes": ["0x…"] } } to /v1/orders/remove-by-hash instead.

The response sorts the orders you sent into three lists:

ListWhat it means
removedThe orders were removed.
noopNothing changed, because the orders were already filled, removed or cancelled.
rejectedThe orders can't be removed yet, because their removalLockedUntil hasn't passed. Try again after it.

Only some up/down markets lock removal for a short time. The create response's removalLockedUntil tells you when. See Place orders.

Every order in the request must belong to the logged-in wallet. If even one doesn't, including an ID or hash that doesn't exist, the whole request fails with 401 and nothing is removed.

Cancel orders on-chain

cancelOrders sends one transaction that marks the orders as cancelled in the exchange contract. The transaction must come from the orders' maker. With a Predict account, the SDK sends it through the account, and the Privy wallet pays the gas.

Each market type has its own exchange contract, so cancel each group separately. GET /v1/orders returns each order with its isNegRisk and isYieldBearing, which tell you its group. The TypeScript SDK checks that the orders belong to the exchange you chose, and throws InvalidNegRiskConfig if they don't. The Python SDK doesn't check. An order sent to the wrong exchange isn't cancelled, even though the transaction succeeds.

import { ChainId, OrderBuilder, type Order } from "@predictdotfun/sdk";
import { Wallet } from "ethers";

const apiKey = "YOUR_API_KEY";
const jwt = "YOUR_JWT"; // from the Authentication guide
const wallet = new Wallet(process.env.WALLET_PRIVATE_KEY!);
const orderBuilder = await OrderBuilder.make(ChainId.BnbMainnet, wallet);

// your open orders. Follow the response's cursor for more than one page.
const response = await fetch(
  "https://api.predict.fun/v1/orders?status=OPEN&first=150",
  { headers: { "x-api-key": apiKey, Authorization: `Bearer ${jwt}` } },
);
const openOrders: {
  order: Order;
  isNegRisk: boolean;
  isYieldBearing: boolean;
}[] = (await response.json()).data;

// each market type has its own exchange, so cancel each group separately.
for (const isNegRisk of [false, true]) {
  for (const isYieldBearing of [false, true]) {
    const orders = openOrders
      .filter(
        (o) => o.isNegRisk === isNegRisk && o.isYieldBearing === isYieldBearing,
      )
      .map((o) => o.order);
    if (orders.length === 0) continue;
    const result = await orderBuilder.cancelOrders(orders, {
      isNegRisk,
      isYieldBearing,
    });
    if (!result.success) throw new Error("A cancel transaction failed");
  }
}

If any order in the list is already filled or cancelled on-chain, the contract reverts the whole transaction. So send only orders that are still open.

Predict sees the contract's cancel event and marks the orders CANCELLED.

On this page