Predict API (Beta)
  1. Guides
Predict API (Beta)
  • FAQs
  • Deployed Contracts
  • 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
  • Accounts
    • Get connected account
      GET
    • Set a referral
      POST
  • Orders
    • Get order by hash
      GET
    • Get orders
      GET
    • Create an order
      POST
    • Remove orders from the orderbook
      POST
  • Categories
    • Get categories
      GET
    • Get category by slug
      GET
  • Markets
    • Get markets
      GET
    • Get market by ID
      GET
    • Get market statistics
      GET
    • Get market last sale information
      GET
    • Get the orderbook for a market
      GET
  • Positions
    • Get positions
      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
  • Schemas
    • Sample Schemas
      • Pet
      • Category
      • Tag
  1. Guides

How to create or cancel orders

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

Sections:#

How to install the SDK
How to set approvals
How to use a Predict account
How to create a LIMIT order (recommended)
How to create a MARKET order
How to redeem positions
How to merge positions
How to check USDT balance
How to interface with contracts
How to cancel orders
License

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.

Predict Account#

Predict supports interacting with the protocol using either a traditional Externally Owned Account (EOA) or a Smart Wallet ("Predict Account"). If you use the web app, a Smart Wallet is automatically created for you. To interact with your Smart Wallet programmatically using the SDK, follow the example shown in How to use a Predict account.

How to set approvals#

Before trading, you need to set approvals for ERC-1155 (ConditionalTokens) and ERC-20 (USDT). This can be achieved by sending a transaction to the respective contracts 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.
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 the redeemPositions method to redeem your positions on the Predict protocol. The method supports all market types through the isNegRisk and isYieldBearing options.
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 options.
The conditionId and indexSet can be fetched from the GET /positions endpoint.

How to merge positions#

The OrderBuilder class provides the mergePositions method to combine both outcome tokens back into collateral (USDT). This is useful when you hold equal amounts of both YES and NO positions.
1.
Create a Wallet: Initialize a wallet that will be used to sign the merge transaction.
2.
Initialize OrderBuilder: Instantiate the OrderBuilder class by calling the static make method.
3.
Merge Positions: Call the mergePositions method with the appropriate options.
The conditionId can be fetched from the GET /positions endpoint.

How to check USDT balance#

The method balanceOf allows to easily check the current USDT 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.
Group by isNegRisk and isYieldBearing: Separate orders based on the isNegRisk and isYieldBearing properties.
3.
Cancel Orders: Call the cancel function and provide the orders to cancel and the isNegRisk and isYieldBearing properties.
4.
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-12-12 02:57:15
Previous
How to authenticate your API requests
Next
Get auth message
Built with