1. [[Decentralized Exchange Service (DEX)#Purchase the order|Purchase the order]]
2. [[Decentralized Exchange Service (DEX)#Creation of our own order|Creation of your own order]]
3. [[Decentralized Exchange Service (DEX)#Purchase the order partially|Purchase the order partially]]
Let's consider an example: you want to exchange a certain amount of KEL tokens for CELL tokens at a favorable rate on the Backbone network.
First, let's see what the market can offer us.
To do this, we need to get a list of orders using the command [[Node Command - SRV_XCHANGE ORDERS]].
`Request:`
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;orders;-net;Backbone;-status;opened;-token_from;CELL;-token_to;KEL"], "id":"1"}'
```
`Response:`
```json
{ "type": 0, "result": "limit: 1000\n
orderHash: 0x2B3B39E4ABF6B3B77702C5D6DB9EBF4593F9D1952B7BBC3C78F817B4FA992E17\n ts_created: Fri, 27 Oct 2023 17:34:26 +0700\n Status: OPENED, amount: 0.000001 (1000000000000) CELL, filled: 0%, rate (KEL\/CELL): 0.000001, net: Backbone\n\n
orderHash: 0x1D80DF6E1FA6E95BAF3077F6773EB268EF1661DDB4E631BEF2FA7378D35BA6BA\n ts_created: Wed, 03 Jan 2024 03:03:27 +0700\n Status: OPENED, amount: 0.1 (100000000000000000) CELL, filled: 0%, rate (KEL\/CELL): 50.0, net: Backbone\n\n
orderHash: 0x16CAD34E3BB584D62F2B0A67B44979CC774442103EEA3798130C52CE173B4809\n ts_created: Wed, 03 Jan 2024 03:10:45 +0700\n Status: OPENED, amount: 0.1 (100000000000000000) CELL, filled: 0%, rate (KEL\/CELL): 55.0, net: Backbone\n\n
orderHash: 0xAD9997EF18E7DE40109C696FD197AE30C96A2FB38CCEF838342D65BC68AB4E39\n ts_created: Thu, 25 Jan 2024 18:37:42 +0700\n Status: OPENED, amount: 0.0002 (200000000000000) CELL, filled: 0%, rate (KEL\/CELL): 45.45454545454545, net: Backbone\n\n
orderHash: 0xD3A8ACEF120BAD24CB178AE7BBE4D31A9E23DB9A7F65EDDD899082AED5D8458A\n ts_created: Mon, 01 Apr 2024 14:42:22 +0700\n Status: OPENED, amount: 1.0 (1000000000000000000) CELL, filled: 0%, rate (KEL\/CELL): 90.909090909090892561, net: Backbone\n\n
orderHash: 0x8A1C751CCF8B80325EB4B70EA336BF0EF1568119274DC33473640A78A6A40AED\n ts_created: Sun, 02 Jun 2024 08:16:22 +0700\n Status: OPENED, amount: 800.0 (800000000000000000000) CELL, filled: 0%, rate (KEL\/CELL): 200.0, net: Backbone\n\n
orderHash: 0xDDFC1FA6B77A8C6F40F78FC18636A246FA8DB9E38095A7B2F2EE98DF905B92E6\n ts_created: Fri, 30 Aug 2024 17:25:50 +0700\n Status: OPENED, amount: 1.0(1000000000000000000) CELL, filled: 99%, rate (KEL\/CELL): 100.0, net: Backbone\n\n
orderHash: 0x83B1F0AF986CDF64D0E6CC7681BC6700BA53C8C1922F29796F3CA1B49F5A325B\n ts_created: Mon, 09 Sep 2024 03:24:42 +0700\n Status: OPENED, amount: 1000.0 (1000000000000000000000) CELL, filled: 0%, rate (KEL\/CELL): 1000.0, net: Backbone\n\n
orderHash: 0x4DCAED5515DD1EC7CE6BDD5118EC3E35931D1215C948E55B98966CFC00143E99\n ts_created: Wed, 11 Sep 2024 17:53:29 +0700\n Status: OPENED, amount: 0.000000000000000002 (2) CELL, filled: 99%, rate (KEL\/CELL): 1.140000000000000001, net: Backbone\n\n", "id": 1 }
```
Let's consider order parameters:
```actionscript
status - order status
amount - amount of tokens offered for exchange
filled - this parameters shows order completion in percentage
rate - (token_to/token_from) relation of one token to another
net - network where order is active
```
We look at profitable orders by rate (the ratio of CELL to KEL).
To find out the average market rate of this pair of tokens, you need to use the command [[Node Command - SRV_XCHANGE TOKEN_PAIR RATE]].
`Request:`
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;token_pair;rate;average;-net;Backbone;-token_from;CELL;-token_to;KEL"], "id":"1"}'
```
`Response:`
```json
{ "type": 0, "result": "Average rate: 154.250463736363634256\nLast rate: 1.140000000000000001 Last rate time: Wed, 11 Sep 2024 17:53:29 +0700", "id": 1}
```
From the answer we see that the average rate is 154. Although the real CELL/KEL ratio fluctuates around 100.
Of all the orders presented, the following looks the most profitable:
```json
orderHash: 0xD3A8ACEF120BAD24CB178AE7BBE4D31A9E23DB9A7F65EDDD899082AED5D8458A\n ts_created: Mon, 01 Apr 2024 14:42:22 +0700\n Status: OPENED, amount: 1.0 (1000000000000000000) CELL, filled: 0%, rate (KEL\/CELL): 90.909090909090892561, net: Backbone
```
Rate is about 91. Try to purchase it and get 1 CELL.
## Purchase the order
Before purchase let's see the balance of our wallet "DANWALLET" using [[Node Command - WALLET INFO]]:
`Request:`
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"wallet", "params":["wallet;info;-net;Backbone;-w;DANWALLET"], "id":"1"}'
```
`Response:`
```json
{ "type": 0, "result":
sign: correct
wallet: DANWALLET
addr: Rj7J7MiX2bWy8sNyYF5njS2TWg97i4EtaViUAdri6qhjUN158aiGNAugRaqLjkSXA7Jq8xyrNiuzPgtyfQKeNxSDzWrCTngXJddfqKzK
network: Backbone
signs: sig_dil
tokens:
balance:
coins: 6.55
datoshi: 6550000000000000000
token:
ticker: CELL
description:
balance:
coins: 202.28
datoshi: 202280000000000000000
token:
ticker: KEL
description:
, "id": 1}
```
We see that we have enough KEL to execute the order. Let's try to buy it. To do this, we need to indicate that we want to sell about 90 KEL.
`Request:`
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;purchase;-net;Backbone;-order;0xD3A8ACEF120BAD24CB178AE7BBE4D31A9E23DB9A7F65EDDD899082AED5D8458A;-w;DANWALLET;-value;90.0e+18;-fee;0.01e+18"], "id":"1"}'
```
`Response:`
```json
{ "type": 0, "result": "Exchange transaction has done. tx hash: 0x3749F78CAE6CAE532AA61D5436E66B01C35D6888D2877B61DDEDAAC7848D7095", "id": 1 }
```
The transaction has been completed, now let's look at our wallet balance and order status.
```json
{ "type": 0, "result":
стало sign: correct
wallet: DANWALLET
addr: Rj7J7MiX2bWy8sNyYF5njS2TWg97i4EtaViUAdri6qhjUN158aiGNAugRaqLjkSXA7Jq8xyrNiuzPgtyfQKeNxSDzWrCTngXJddfqKzK
network: Backbone
signs: sig_dil
tokens:
balance:
coins: 7.53
datoshi: 7530000000000000000
token:
ticker: CELL
description:
balance:
coins: 111.370909090909107439
datoshi: 111370909090909107439
token:
ticker: KEL
description:
, "id": 1}
```
1 CELL added, and 90 KEL left. Also 0.01 CELL was spent on commission.
Let's look at the order status:
`Request:`
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;order;status;-net;Backbone;-order;0xD3A8ACEF120BAD24CB178AE7BBE4D31A9E23DB9A7F65EDDD899082AED5D8458A"], "id":"1"}'
```
```json
{ "type": 0, "result": "orderHash: 0xD3A8ACEF120BAD24CB178AE7BBE4D31A9E23DB9A7F65EDDD899082AED5D8458A\n ts_created: Mon, 01 Apr 2024 14:42:22 +0700\n Status: CLOSED, amount: 0.0 (0) CELL, filled: 100%, rate (KEL\/CELL): 90.909090909090892561, net: Backbone\n\n", "id": 1 }
```
The order is closed because we have filled it 100 percent.
There are no more interesting orders, but we, for example, would like to get another CELL at a normal rate somewhere around KEL/CELL = 0.01 (which is the same as CELL/KEL =100).
So let's create our own order.
## Creation of our own order
Order parameters will be like this:
```actionscript
token_sell: KEL
token_buy: CELL
rate: 0.01
value: 100.0e+18 KEL (100 KEL)
fee: 0.01e+18 CELL (0.01 CELL)
```
`Request:`
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;order;create;-net;Backbone;-token_sell;KEL;-token_buy;CELL;-w;DANWALLET;-value;100.0e+18;-rate;0.01;-fee;0.01e+18"], "id":"1"}'
```
`Response:`
```json
{ "type": 0, "result": "\nSuccessfully created order 0x86A53D20ADAE71BC1DBADB3F24A3449A6E664090F3FB4371EE49211B5DDE18B0", "id": 1 }
```
The order has been successfully created.
Let's check the status of the created order:
`Request:
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;order;status;-net;Backbone;-order;0x86A53D20ADAE71BC1DBADB3F24A3449A6E664090F3FB4371EE49211B5DDE18B0"], "id":"1"}
```
`Response:`
```json
{ "type": 0, "result": "orderHash: 0x86A53D20ADAE71BC1DBADB3F24A3449A6E664090F3FB4371EE49211B5DDE18B0\n ts_created: Thu, 12 Dec 2024 17:57:51 +0700\n Status: OPENED, amount: 100.0 (100000000000000000000) KEL, filled: 0%, rate (CELL\/KEL): 0.01, net: Backbone\n\n", "id": 1 }
```
Now all that remains is to wait until someone finds this order profitable and buys it out partially or completely.
You need to periodically update the order status with commands
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;order;status;-net;Backbone;-order;0x86A53D20ADAE71BC1DBADB3F24A3449A6E664090F3FB4371EE49211B5DDE18B0"], "id":"1"}
```
Eventually, our order is executed.
```json
{ "type": 0, "result": "orderHash: 0x86A53D20ADAE71BC1DBADB3F24A3449A6E664090F3FB4371EE49211B5DDE18B0\n ts_created: Thu, 12 Dec 2024 17:57:51 +0700\n Status: CLOSED, amount: 0.0 (0) KEL, filled: 100%, rate (CELL\/KEL): 0.01, net: Backbone\n\n", "id": 1 }
```
Let's now look at the balance of our wallet.
```json
{ "type": 0, "result":
sign: correct
wallet: DANWALLET
addr: Rj7J7MiX2bWy8sNyYF5njS2TWg97i4EtaViUAdri6qhjUN158aiGNAugRaqLjkSXA7Jq8xyrNiuzPgtyfQKeNxSDzWrCTngXJddfqKzK
network: Backbone
signs: sig_dil
tokens:
balance:
coins: 8.52
datoshi: 8520000000000000000
token:
ticker: CELL
description:
balance:
coins: 11.370909090909107439
datoshi: 11370909090909107439
token:
ticker: KEL
description:
, "id": 1}
```
100 KEL left, 1 CELL added, also 0.01 CELL was spent on paying the commission.
If no one bought our order or bought it only partially, we can close it with the command [[Node Command - SRV_XCHANGE ORDER REMOVE]], as a result of which all our blocked funds will be unblocked and returned to our wallet.
## Purchase the order partially
It may happen that we see a profitable order, but we do not want to buy it out completely, or we do not have enough funds, in which case we can execute it partially.
For example, there is the following order:
```json
{
"type": 0,
"result": "orderHash: 0x3AA3A8072E14E09C5CC11D80CE6F1D0C4CDCB8CD5BA76A02CA655F97865AFC4A\n ts_created: Fri, 06 Dec 2024 08:19:33 +0000\n Status: OPENED, amount: 2.0 (2000000000000000000) KEL, filled: 0%, rate (CELL/KEL): 1.0, net: Backbone\n\n",
"id": 1
}
```
Let's try to partially buy it back using the command [[Node Command - SRV_XCHANGE PURCHASE]]. In particular, we will buy 1 KEL out of the 2 specified in the order.
`Request:`
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;purchase;-order;0x3AA3A8072E14E09C5CC11D80CE6F1D0C4CDCB8CD5BA76A02CA655F97865AFC4A;-net;Backbone;-w;CELLWALLET;-value;1.0e+18;-fee;0.05e+18"], "id":"1"}'
```
`Response:`
```json
{
"type": 0,
"result": "Exchange transaction has done. tx hash: 0x03687FDF843E77280769AA3107005C45E2F631F9CC7AF161A15B4D4EB4371C46",
"id": 1
}
```
The transaction was successful, now let's look at the order status.
`Request:`
```bash
curl --unix-socket /opt/cellframe-node/var/run/node_cli -X POST http://localhost/connect -d '{"method":"srv_xchange", "params":["srv_xchange;order;status;-net;Backbone;-order;0x3AA3A8072E14E09C5CC11D80CE6F1D0C4CDCB8CD5BA76A02CA655F97865AFC4A"], "id":"1"}'
```
`Response:`
```json
{
"type": 0,
"result": "orderHash: 0x3AA3A8072E14E09C5CC11D80CE6F1D0C4CDCB8CD5BA76A02CA655F97865AFC4A\n ts_created: Fri, 06 Dec 2024 08:19:33 +0000\n Status: OPENED, amount: 1.0 (1000000000000000000) KEL, filled: 50%, rate (CELL/KEL): 1.0, net: Backbone\n\n",
"id": 1
}
```
We see that the number of available KELs has dropped from 2 to 1, and the order itself is 50 percent filled.