Predict API (Beta)
  1. Guides
Predict API (Beta)
  • FAQs
  • Guides
    • Understanding the Orderbook
    • How to authenticate your API requests
    • How to create or cancel orders
  • Authorization
    • Get auth message
      GET
    • Get JWT with valid signature
      POST
  • Orders
    • Get order by hash
      GET
    • Get orderbook for specific market
      GET
    • Get orders
      GET
    • Create an order
      POST
    • Optimistically cancel orders
      POST
  • Categories
    • Get categories
      GET
    • Get category by slug
      GET
  • Markets
    • Get markets
      GET
    • Get market statistics
      GET
    • Get market last sale information
      GET
    • Get market activity
      GET
  • Positions
    • Get your positions
      GET
  • Season
    • Get leaderboard
      GET
    • Get season totals
      GET
  • OAuth
    • Finalize a OAuth connection
      POST
    • Get the orders for a OAuth connection
      POST
    • Create an order for a OAuth connection
      POST
    • Cancel the orders for a OAuth connection
      POST
    • Get the positions for a OAuth connection
      POST
  1. Guides

How to create or cancel orders

This guide will demonstrate how to create orders using the Predict's SDK.

Sections:#

How to install the SDK
How to set approvals
How to use a Predict account
How to create an order
How to redeem positions
How to check USDB balance
How to interface with contracts
How to cancel orders
License
For more details, you can check out the specific functions implementations on the GitHub repository.
A TypeScript SDK to help developers interface with the Predict's protocol.

How to install the SDK#

This package has ethers v6 as a peer dependency.
See the OrderBuilder class for more in-depth details on each function.

How to set approvals#

Before trading, you need to set approvals for ERC-1155 (ConditionalTokens) and ERC-20 (USDB). This can be achieved by sending a transaction to the respective contracts (see the Contracts section) and approving both the CTF_EXCHANGE and the NEG_RISK_CTF_EXCHANGE or via the SDK utils.
Contracts: The current deployed contracts can be found either in the Constants.ts file or in the Deployed Contracts documentation.
The following example demonstrates how to set the necessary approvals using the SDK utils.

How to use a Predict account#

In the previous version of Predict, all users (including EOAs), would receive a smart wallet when interacting via the web app. In the current version, EOAs no longer receive a smart wallet account and as such, they interact with the on-chain protocol directly.
Here's an example of how to use a Predict account to create/cancel orders and set approvals.
1.
Initiate the Privy Wallet: The wallet is needed to sign orders. Can be found in the account settings.
2.
Ensure the Privy Wallet has funds: You will need to add some ETH to be able to set approvals and cancel orders, if needed.
3.
Initialize OrderBuilder: Instantiate the OrderBuilder class by calling OrderBuilder.make.
NOTE: Include the predictAccount address, which is also known as the deposit address.
4.
Set Approvals: Ensure the necessary approvals are set (refer to Set Approvals).
5.
Determine Order Amounts: Use getLimitOrderAmounts to calculate order amounts.
6.
Build Order: Use buildOrder to generate a LIMIT strategy order.
NOTE: Fetch the feeRateBps via the GET /markets endpoint on the REST API
NOTE: Set the signer and maker to the predictAccount address, NOT the signer/privy wallet address.
7.
Generate Typed Data: Call buildTypedData to generate typed data for the order.
8.
Sign Order: Obtain a SignedOrder object by calling signTypedDataOrder.
9.
Compute Order Hash: Compute the order hash using buildTypedDataHash.
This will allow you to perform operations via your Predict Account (via the smart wallet).

How to create a LIMIT order (recommended)#

Here's an example of how to use the OrderBuilder to create and sign a LIMIT strategy buy order:
1.
Create Wallet: The wallet is needed to sign orders.
2.
Initialize OrderBuilder: Instantiate the OrderBuilder class by calling OrderBuilder.make.
3.
Set Approvals: Ensure the necessary approvals are set (refer to Set Approvals).
4.
Determine Order Amounts: Use getLimitOrderAmounts to calculate order amounts.
5.
Build Order: Use buildOrder to generate a LIMIT strategy order.
NOTE: Fetch the feeRateBps via the GET /markets endpoint on the REST API
6.
Generate Typed Data: Call buildTypedData to generate typed data for the order.
7.
Sign Order: Obtain a SignedOrder object by calling signTypedDataOrder.
8.
Compute Order Hash: Compute the order hash using buildTypedDataHash.

How to create a MARKET order#

Similarly to the above, here's the flow to create a MARKET sell order:
1.
Create Wallet: The wallet is needed to sign orders.
2.
Initialize OrderBuilder: Instantiate the OrderBuilder class by calling OrderBuilder.make.
3.
Set Approvals: Ensure the necessary approvals are set (refer to Set Approvals).
4.
Fetch Orderbook: Query the Predict API for the latest orderbook for the market.
5.
Determine Order Amounts: Use getMarketOrderAmounts to calculate order amounts.
6.
Build Order: Call buildOrder to generate a MARKET strategy order.
NOTE: Fetch the feeRateBps via the GET /markets endpoint on the REST API
7.
Generate Typed Data: Use buildTypedData to create typed data for the order.
8.
Sign Order: Obtain a SignedOrder object by calling signTypedDataOrder.
9.
Compute Order Hash: Compute the order hash using buildTypedDataHash.

How to redeem positions#

The OrderBuilder class provides methods to redeem your positions on the Predict protocol. Depending on the type of market you're interacting with, you can use either redeemPositions for standard markets or redeemNegRiskPositions for NegRisk markets.
1.
Create a Wallet: Initialize a wallet that will be used to sign the redemption transaction.
2.
Initialize OrderBuilder: Instantiate the OrderBuilder class by calling the static make method.
3.
Redeem Positions: Call the redeemPositions method with the appropriate conditionId and indexSet.
The conditionId and indexSet can be fetched from the GET /positions endpoint.

How to check USDB balance#

The method balanceOf allows to easily check the current USDB balance of the connected signer.

How to interface with contracts#

To facilitate interactions with Predict's contracts we expose the necessary instances of each contract, including ABIs and types.
Some other useful utils, ABIs and types exposed by the SDK.

How to cancel orders#

Here's an example on how to cancel orders via the SDK
1.
Fetch Orders: Retrieve your open orders using GET /orders.
2.
Cancel Orders off chain: Call POST /orders/cancel with orderIds and cancel orders from the orderbook
3.
Group by isNegRisk: Separate orders based on the isNegRisk property.
4.
Cancel Orders: Call the specific cancel function based on the order(s) type (isNegRisk).
5.
Check Transaction Success: Check to confirm the transaction was successful.

License#

By contributing to this project, you agree that your contributions will be licensed under the project's MIT License.
Modified at 2025-11-07 00:36:33
Previous
How to authenticate your API requests
Next
Get auth message
Built with