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
  • Blast
    • Blast multiplier addresses
      GET
  • OAuth
    • Finalize a OAuth connection
    • Get the orders for a OAuth connection
    • Create an order for a OAuth connection
    • Cancel the orders for a OAuth connection
    • Get the positions for a OAuth connection
  • ZeroDev
    • Validate a user op
  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 cancel orders
How to interface with contracts
For more details, you can check out the specific functions implementations on the GitHub repository.

How to install the SDK#

This package has ethers v6 as a peer dependency. It's recommended to use version 1.0.0 or above to avoid issues ("@predictdotfun/sdk": "^1.0.0").
yarn add @predictdotfun/sdk ethers
npm install @predictdotfun/sdk ethers
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 How to interface with 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#

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.

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 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.
Group by isNegRisk: Separate orders based on the isNegRisk property.
3.
Cancel Orders: Call the specific cancel function based on the order(s) type (isNegRisk).
4.
Check Transaction Success: Check to confirm the transaction was successful.

How to interface with contracts#

To facilitate interactions with Predict's contracts we provide the necessary ABIs and some common functions to get you started.
Previous
How to authenticate your API requests
Next
Get auth message
Built with