What decentralized exchanges are and how they work

cyber•Drop
cyber•Drop
Published in
5 min readFeb 28, 2019

by Maxim Odegov and Serge Nedashkovsky

In Ethereum network there are two main DEX operating principles: off-chain and on-chain ones. In the former case, orders are kept on the exchange servers and the interaction with blockchain happens at the moment when a customer wants to close an order. In the latter case, orders are kept in the exchange smart contracts.

Off-chain

Most existing exchanges use this operating principle. In general terms, it can be described in the following way:

Figure 1. Off-chain trading principle
  1. A seller (maker) makes an order, signs it and sends it to the exchange server.
  2. A buyer (taker) selects the order and receives its details from the server.
  3. The buyer sends a transaction to the exchange contract with a trade method call
  4. The contract checks the order authenticity and if it goes well, the contract changes the participants’ balances and completes the trade.
Token Store contract source

The example of the exchanges using an off-chain principle are IDEX, Bancor, as well as the exchanges based on the protocol 0x: DDEX, Starbit.

On-chain

The main difference of the on-chain principle is that the order signature is kept in the contract.

Figure 2. On-chain trading principle

The example of the exchange using an on-chain principle is Ethex. Another example is Etherdelta exchange. Despite the fact most Etherdelta trades use the off-chain principle, its smart contract has the method “order” which helps make orders and keep them in the contract memory. Using the example of Etherdelta exchange we can show on-chain trades in the following way:

  1. A seller calls the order method which creates the order hash and saves it into the contract storage (code)
  2. A buyer can receive the order details directly from blockchain using an Order event log.
  3. Similar to the previous method, the buyer sends a transaction containing the “trade” method call to the exchange contract.
  4. The contract checks whether one of the following conditions is true: the received order hash is contained in the orders object; the order is signed by a seller. The first check is required for on-chain based orders, the second one is for the off-chain orders.
Etherdelta contract source

If the conditions are met, the contract changes the participants’ balances and completes the trade.

Balance change

In the process of trading user balances are changed in different ways.

Some exchanges keep finances on the smart contracts. To make or close an order on such an exchange a user needs to transfer tokens from his address to the exchange smart contract using the deposit or depositToken methods. In this case user balances are kept in the inner contract objects. When the trade is completed successfully the contract rewrites the balances kept in its storage, and tokens stay on the contract balance.

Etherdelta contract source

User balances change actually when the methods withdraw/withdrawToken are called.

Other exchanges work differently and change user balances each time when the order is closed. The example of exchanges using this principle are the exchanges working with the protocol 0x. in this case, before the trade is made users send a transaction with the approve method which allows the exchange proxy contract to send a certain number of tokens in the process of trading. When an order closes the exchange contract calls the proxy contract which makes a transfer between user addresses:

0x v1 contract source

Protocols

Developers try to create exchange protocols which help make decentralized exchanges follow the uniform rules.

0x protocol

0x protocol is an off-chain exchange protocol. One of its main features is the usage of the liquidity pool common for all exchanges working with this protocol.

0x protocol exchanges use standard API due to which every exchange can get the access to other exchanges’ order books and display their orders in the interface. So, the contract specifies the address of the related exchange for every created order. Therefore, it doesn’t matter which interface was used to close the order,

only the first exchange will get a commission for it.

Generally, a flowchart with the liquidity pool can be described as following:

  1. A seller makes an order through the interface of exchange 1.
  2. Exchange 1 through the standard API shares the order data with other exchanges.
  3. A buyer receives the order information through the interface of exchange 2.
  4. The seller sends a transaction with the trade method call to DEX smart contract
  5. The smart contract checks the order validity
  6. If everything goes well, DEX contract calls proxy contract which exchanges tokens between a buyer and a seller.
Figure 3. 0x protocol trading principle

Another feature of the protocol is that user tokens are not stored in the exchange contract, they are moved between buyer and seller’s addresses through proxy contract.

Bancor

Bancor works like an exchange where users can buy and sell tokens, but the principles it uses are completely different.

The main feature of this protocol is that it uses smart tokens, it means, these tokens’ contracts can be directly exchanged with other tokens without any sellers. In this regard, every smart token has a connector — a contract which keeps the balances of other tokens. The process of buying using the connector looks like that:

  1. Buying a smart token, a user sends an attached token to the connector balance.
  2. The connector calls the issue method
  3. The smart token method issues a certain number of new tokens which are sent to the user.
  4. Apart from sending new tokens to a user, the issue method also increases the total supply.
Bancor Network Token contract
Figure 4. Bancor Network trading principle

And vice versa, when a smart token is sold, it is removed from the user balance with the destroy method call and also this amount is subtracted from the total supply.

Bancor Network Token contract

Dynamically changing total supply lets a token be always available for buying.

Smart token technology can be used with any ERC20 tokens, for that Bancor Network provides Relay Tokens — smart contracts that store virtual user balances.

A full list of DEX protocols is available here github.com/evbots/dex-protocols.

At the moment we are finishing the work on indexing trading of the biggest DEXs. These data and analytics to them will be available at santiment.net.

In the next article we are going to describe the history and the current state of trading on DEXs.

--

--