Creating a Buy 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 are willing to pay for each unit of the base token you want to buy. The quantity is the number of base tokens you want to buy. 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 buy 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 scale 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 currency tokens that you need to approve for the exchange to spend on your behalf. This calculation is done in the (quote) token's native scale. For buy orders, this is the total amount of quote tokens that you are willing to pay for your order, plus necessary fees. You can use the following formula to calculate the quantity to approve:
For example, if you want to buy 100 tokens at a price of 0.01 quote (currency) tokens each, and the commission values are 20 basis points each, you can calculate the quantity to approve as:
assuming that quoteTokenNativeScale
returned by the API endpoint for the corresponding trading pair is 1 000 000.
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 currency 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. You can use the following code snippet to approve the allowance:
Finally, you need to call the newBuyOrder function of the order book contract to create a buy order. The newBuyOrder 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 buy order:
Congratulations! You have successfully created a buy order on 21X DLT Exchange