Manage Orders
Last updated
Last updated
This guide shows you how to:
Create an order.
Place an order.
Cancel an order.
Query orders by subaccount.
If you inspect the underlying types for these transactions, you'll notice that a "nonce" field is required. This is a unique integer in ascending order. Our off-chain engine has a nonce
query to return the latest nonce for a given subaccount. All this is abstracted away within the SDK, so you do not need to manually use this query.
To place orders, we need a subaccount with funds. We need to perform the step as before.
An order requires a number of parameters, encapsulated by the OrderActionParams['order']
type.
In the example below:
The order expiration
time is given by calling the nowInSeconds
function from the utils
package and adding 60 seconds. This means the order will expire 60 seconds from now.
Notice the usage of getExpirationTimestamp
, order types, such as IOC (immediate or cancel) are encoded in the expiration field, the getExpirationTimestamp
function will return the correct expiration timestamp for the given order type.
The price
field is set at 20000
- a low value (at the time of writing) to prevent execution. This enables us to cancel the order later on without it being instantly filled. Please adjust this price accordingly.
The amount
field is set at 10**16
- this is the amount to buy/sell. A positive value is to buy, negative is to sell.
Amount is normalized to 18 decimal places, which is what toFixedPoint
does by default.
NOTE: Min size for BTC
is 10**16
and for ETH
is 10**17
. Orders below these sizes will fail to be placed.
Use the order parameters to place the order with the placeOrder
function.
Alternatively, you can create an order tx
and interface with lower level methods to place the order. The order parameters are encapsulated by OrderParams
.
You can optionally generate the order digest, which can then be used to further manage the order e.g: cancelling the order.
Now, you can place the order by signing and executing the generated order tx
.
Now we can query the subaccount for open orders with the getOpenSubaccountOrders
function.
Cancel the order using the digest of the placed order. You can cancel multiple orders at once.
Finally, clean up by withdrawing the same amount.
Run again to make sure the cancellation was successful.