Create a buy
or sell
order in the available markets.
Guide for Creating an Order
This guide dives into the process of creating orders using the Tegro DEX API. It follows the same methodology employed by the TegroDex website.
Libraries used
Listed below are the libraries used in the steps
- ethers (6.11.0)
- axios (1.6.7)
Steps
There are two main steps when creating orders.
First, an order object is generated which has all the details required like the raw order data, the type of order and domain where the order will be placed.
The only information that is missing to place the order now, is the signature, which can be generated using the data obtained.
Once we obtain the signature the order is placed in the order book, using Place Order in the order book endpoint.
Step 1 - Generate Order for signing
The signed order transaction data for creating the order can be obtained by making a request to the Generate Order for Signing endpoint. This is demonstrated with an example below.
import axios from 'axios';
const baseDomain = `https://api.tegro.com/api`; // The base domain to use depends on the environment being used. In this case, we will be demonstrating with the base domain.
let url = `https://${baseDomain}/v1//trading/market/orders/typedData/generate`;
const body = {
chain_id: 8453, // Base Chain ID
wallet_address: "0x3da2b15eB80B1F7d499D18b6f0B671C838E64Cb3",//Please enter your wallet address
market_symbol: "WETH_USDC",
side: "buy",
price: 2700,
amount: 0.1
};
axios.post(url, body)
.then((response) => {
const signedOrderCreationTransactionData = response.data;
})
.catch(console.error);
return data;
The following JSON object represents the data extracted from the response and stored in the signedOrderCreationTransactionData
variable.
{
"message": "success",
"data": {
"limit_order": {
"chain_id": 8453,
"base_asset": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
"quote_asset": "0x4200000000000000000000000000000000000006",
"side": 1,
"volume_precision": "100000000000000000",
"price_precision": "2700000000",
"order_hash": "0x5a28a76181ab0c008368ed09cc018b6d40eb23997b4a234cfe5650b7034d6611",
"raw_order_data": "{\"baseToken\":\"0x4200000000000000000000000000000000000006\",\"expiryTime\":\"0\",\"isBuy\":true,\"maker\":\"0x3da2b15eB80B1F7d499D18b6f0B671C838E64Cb3\",\"price\":\"2700000000\",\"quoteToken\":\"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913\",\"salt\":\"277564373322\",\"totalQuantity\":\"100000000000000000\"}",
"signature": null,
"signed_order_type": "tegro",
"market_id": "8453_0x4200000000000000000000000000000000000006_0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
"market_symbol": "WETH_USDC"
},
"sign_data": {
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"Order": [
{
"name": "baseToken",
"type": "address"
},
{
"name": "quoteToken",
"type": "address"
},
{
"name": "price",
"type": "uint256"
},
{
"name": "totalQuantity",
"type": "uint256"
},
{
"name": "isBuy",
"type": "bool"
},
{
"name": "salt",
"type": "uint256"
},
{
"name": "maker",
"type": "address"
},
{
"name": "expiryTime",
"type": "uint256"
}
]
},
"primaryType": "Order",
"domain": {
"name": "TegroDEX",
"version": "1",
"chainId": 8453,
"verifyingContract": "0xa492c74aAc592F7951d98000a602A22157019563"
},
"message": {
"baseToken": "0x4200000000000000000000000000000000000006",
"expiryTime": "0",
"isBuy": true,
"maker": "0x3da2b15eB80B1F7d499D18b6f0B671C838E64Cb3",
"price": "2700000000",
"quoteToken": "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
"salt": "277564373322",
"totalQuantity": "100000000000000000"
}
}
}
}
Step 2 - Creating the Order
With the order transaction data obtained, we can now proceed to place the order on the Tegro DEX platform using the Place Order endpoint. The limit_order
and sign_data
properties from the object extracted in the above step should be used to create the body for that request. The limit_order
property, after an update to the signature property within it, will serve as the body for this request. The signature to be used in the request is generated using the sign_data
property.
This process is demonstrated in the below snippet.
Note: 'this.wallet' refers to the wallet object created by the Ethers library to sign the transaction. You will also have to use the Ethers library to give approvals to the ERC-20 contract of the token you are trying to trade.
let url = `https://${this.baseDomain}/v1/trading/market/orders/place`;
const signature = await this.wallet.signTypedData(signedOrderCreationTransactionData.data.sign_data.domain,
{ Order: signedOrderCreationTransactionData.data.sign_data.types.Order }, JSON.parse(signedOrderCreationTransactionData.data.limit_order.raw_order_data))
const requestBody = {
...signedOrderCreationTransactionData.data.limit_order,
signature
}
axios.post(url, requestBody).then(console.log);
When a successful create order request is made, a 200 response is returned, signifying that the order has been successfully placed.