Photo by Austin Distel on Unsplash

A Decentralized Token Exchange

Angel Java Lopez
2 min readMay 13, 2020


In 2018 I started with a personal project to implement a decentralized token exchange, which can run on Ethereum or RSK. I want in this post to describe the status of the project and the next steps to implement.

The name of the project is Dexert and it is open source. The repository is here. I also collected links to other projects and implementations.


It’s a simple smart contract that manage cryptocurrency (ether or RBTC) and ERC20 tokens:

Users buying and selling ERC20 tokens

The steps:

1- A user (or a smart contract) deposit cryptocurrency into the Dexert exchange smart contract. That deposit could be withdraw at any moment, if it is not locked into any pending order

2- The same user then puts a Buy Order (buying tokens), specifying the token to buy, the amount and the price in cryptocurrency. The total cryptocurrency value (amount by price) is locked until the order is completed or canceled.

3- Another user deposit ERC20 tokens into the Dexert exchange smart contract. As with cryptocurrency, this balance could be withdraw at any moment if it is not locked into an order.

4- Then the same user puts a Sell Order, specifying the token to sell, the amount and the price to accept in cryptocurrency. The token amount is locked from his/her token balance until the order is complete or cancelled.

This is the structure of an order:

struct Order {
address token;
address account;
uint amount;
uint price;
bool buying;

This is the balance structure:

struct Balance {
uint available;
uint reserved;

It represents cryptocurrency balance or ERC20 tokens balance:

mapping (address => Balance) private balances;
mapping (address => mapping (address => Balance)) private tokenBalances;


As usual, guided by simplicity, there are some design decisions:

  • No range of price in orders
  • No date/time to cancel an order
  • No fancy alternative orders
  • No orders to exchange one token by another one
  • When an order is added, it is matched automatically with others. It could be completed, or only executed partially.

Pending Work

The code to improve is the match of the orders. When the smart contract be populated by orders, the current code could employ lot of gas. So, I plan to add some structure to match orders without incurring in linear searchs.

Angel “Java” Lopez