πŸ“™Place an Order - Openbook

Summary

After you get the market information and locate a trading opportunity, the next step is to place an order.

Creating Transactions

**bloXroute Labs will NEVER ask you for your private key. In general, no blockchain project should ask you for your private keys, as that would give them full control over your Solana account.

You can use the following endpoints to create partial signed transactions for any actions you want to take in Serum:

  • Create Order

  • Cancel Order

  • Cancel Order by ClientID

  • Settle

Note that these transactions are partially signed, and you will need to complete signing with your private keys for this transaction to be valid.

The easiest way of doing this is to use the Go & Python SDKs, which simplifies the step of fetching the partially signed transaction, signing it, and submitting the transaction to the network. By default, the SDKs require you to set an PRIVATE_KEY environment variable to load your private key, though hooks are included (see sample code) for specifying the private keys in other ways (for example loading from a file). The code for both of these SDKs is open-sourced, and you can vet the implementation yourself to understand how your keys are handled in the SDK.

Example of Create Order Transaction(Go SDK)

newOrderUnsignedTransaction, err := g.PostOrder(
    ctx, 
    "BraJjCwLLqw8TciQXYruDEF4YhDkGwoEnwnAdwJSjcgC", // owner solana wallet address
    "4raJjCwLLqw8TciQXYruDEF4YhDkGwoEnwnAdwJSjcgv", // SPL token wallet address 
    "SOL/USDC", // market 
    pb.Side_S_BID, // trade side (Bid/Ask)
    []pb.OrderType{pb.OrderType_OT_LIMIT}, // OrderType
    20, // order size
    float64(0.124), // order price 
    provider.PostOrderOpts{
    ClientOrderID: 5000, // Client controlled OrderID
    })

Example of Create Order Transaction(Python SDK)

await api.post_order(
    owner_address=PUBLIC_KEY,
    payer_address=PUBLIC_KEY,
    market="SOLUSDC",
    side=proto.Side.S_ASK,
    type=[proto.OrderType.OT_LIMIT],
    amount=0.1,
    price=150_000,
    # optional, but much faster if known
    open_orders_address="5yyh4mzzycmjfR6arY736d1mB6vNSLiUaFWfepKLf8kZ",
    # optional, for identification
    client_order_i_d=0,
)

More technically, all Solana transactions consist of a message blob (bytes) and an array of signatures. Serum API returns a transaction with the message blob and an array of all other signatures, with an empty slot left for you to generate a signature with your private key against the message blob. The SDKs find this slot and complete this step for you –– if you intend to do this step manually you might have to write some special handling with whatever Solana library you're using to create signatures.

Submitting Transaction

Once the transaction is signed, you can use our Submit Signed Transaction endpoint to propagate the transaction to the Solana network.

You need to submit transactions fairly soon after creating them: each transaction includes a reference to a recently generated Solana block hash. This reference only remains valid for a limited amount of time (150 blocks or ~1 minute 19 seconds). If you don't submit the transaction within this time frame it will no longer be valid. Again, the SDKs we provide will simplify this step for you if you choose to use them.

You might also find it useful to turn on the skipPreflight flag in some cases for this endpoint. By default, we simulate all transaction execution before submitting them to the network to prevent transactions with errors from being submitted, since that would waste transaction fees. For high-frequency trading applications, however, you may find that transaction simulation results don't match actual execution, which might hamper what you're trying to do. In our experience, this often happens fairly frequently with canceled instructions.

$ curl -X 'POST' \
  'http://uk.solana.dex.blxrbdn.com/api/v2/submit' \
  -H 'Content-Type: application/json' \
  -d '{
 "transaction":"AjF+Br2CUIENJqV10y7bkgvn48ZlEKaTQeZNMhaIntatrtIHdm5rvpf9o8tZJc42JvThn+KvgL/M9U/t3bkuGAzTILvd64CRiM6Nth3MsOzVK198UK4WUg3sx27b4G56du9gNhpAGfPW+WUFjsM/pIvYuGWzERAtT+3tQ3vfI7IMAgAFD4ls26fgpAnCYufUzDrXMMpDjMYkf2Y2FHuxqKE+2+Ir2imDM3wYq7Q5tLj5w93u4Gi/Y/2BJjzAaJGatpcPFFeEwvsYrtYZ9UZjJlPvBgKfAqhkvzgphnGBuyDfHXFcMEoHX5xmEhJgK0YZx3BKh/s3nhpE7IFyBzqsKBqiTDd6jfzI9XsPznt1ZnWa9u9nVKg1KibD5ElrzSfbftYpluJAIIlGU8/d+nt+YMlmaCc2otsPg4VkklsRB3oh4DbXlwD0JuFuuM8DEZF1+YBRQ0SVXONw52WUDzwpQ5VF+0Wppt/RXFB3Bfkzm5U8Gk39vJzBht0vYt9IqVgEXip2UlkfJvXwRhxAEL1cyMpwZt2lhKbucXk0xnet9MJfvRVqLWrj7TJ6D4hJp3KUHZcFDzpujLjdOrzbFHCIfIK1TT82BpuIV/6rgYT7aH9jRhjANdrEOdwa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