Creating a Sell Order
Last updated
Last updated
If you want to buy some tokens on 21X DLT Exchange, you need to follow these steps:
First, you need to encode the order data of price and quantity into a payload data structure. The price is the amount of quote tokens you ask for each unit of the base token you want to sell. The quantity is the number of base tokens you want to sell. You can use the following formula to encode the order data:
Internal scales help achieve consistent form for the user interface and transactional engine. They are available through the API endpoint .
This can be considered as the basic setup. Optionally, the order book offers additional order flags that you can use to control the way in which your order is processed.
For example, if you want to sell 100 tokens at a price of 0.01 currency tokens each, the base (asset) internal scale is 10 000 000 000, the quote (currency) internal scales is 10 000, and your order is a Limit Order with BookOrCancel condition which should live for 90 days, you can encode the order data as:
Next, you need to calculate the quantity of base tokens that you need to approve for the exchange to spend on your behalf. This calculation is done in the (base) token's native scale. For sell orders, this is the total number of base tokens that you are willing to sell. You can use the following formula to calculate the quantity to approve:
For example, if you want to sell 99 tokens, you can calculate the quantity to approve as:
assuming that baseTokenNativeScale
returned by the API endpoint for the corresponding trading pair is 1 000 000 000 000 000 000 (10^18).
There are two additional pieces of order payload that are optional, but can be used to control the behavior of the system.
The cross-identifier
can be set to an arbitrary 32bit number that is checked during order matching. Orders that were sent from the same wallet but have different cross-identifiers will not trigger an order rejection because of a self-trade. If the cross-identifier is not provided, it defaults to 0.
The reporting_data
can take 256 bits of arbitrary data (encoded as an integer value). The 21X backend will use the last (least significant) 32 bits of the reportingData to try to determine an execution decision maker by cross-referencing them to the referenceNumber field of applicable persons. If successful, the person will be included in the report as the decision maker, otherwise the decision maker will stay empty.
The rest of reporting_data
will not be interpreted by the 21X backend, so you can store any data in there, for example to match the order to your own system later on.
Then, you need to call the approve function of the product token contract to set the allowance of the exchange contract. The approve function takes two arguments: the address of the spender and the amount of tokens to allow. The amount is simply the quantity you want to sell in the product token's native scale. You can use the following code snippet to approve the allowance:
Finally, you need to call the newSellOrder function of the order book contract to create a sell order. The newSellOrder function takes three arguments: the encoded order data (mandatory), the reporting data and the cross-identifier. You can use the following code snippet to create a sell order:
Congratulations! You have successfully created a sell order on 21X DLT Exchange