Building a Decentralized Exchange

Bitcoin revolutionized how we think about money. Ethereum took it a step further by allowing money and code to co-exist on the same network. The next logical step is to ensure people can exchange cryptocurrencies and trade digital assets without third-party intervention.

In my last article, Re-Thinking The Exchange Model, I outline a model for a decentralized exchange. Now, my plan is to give some detail on how such an exchange can be created with the following components: messages, smart contracts, cross-network transactions, and interfaces.


Imagine an exchange that is global in scope, always open, virtually anonymous, and free of hyper speculation. One where anyone with a cryptocurrency can buy or sell with a message — entering a symbol, trade type, price, quantity desired, and expiration date. All with decentralized control.

From a user’s perspective, messages are open requests to buy or sell. By setting a price, the participant of the trade is stating how much he or she believes the asset is worth. If the price is within a reasonable rate, as determined by the market, a transaction is highly likely. Otherwise it will expire unfilled, leaving the participant with a choice to alter the price or wait.

A message starts with a symbol such as ETH. Thus stating what the participant wants to trade. It is not a position as in FOREX, but a request to buy or sell with an actual transfer potentially occurring. This parameter is a string of 4 characters.

Trade Type is the bid / ask. Bid if you are buying and ask if you are selling. There is no long or short option or complex order types like in modern exchanges. It is simply a setting of offering to buy or wanting to sell. This parameter is a bit of 0 to buy and 1 to sell.

The quantity selection is required in order to allow both large organizations and individuals to trade within their own limits. In effect creating two markets, one at wholesale bulk pricing and one at retail pricing. This way a large buy or sell order does not flash crash the market. This parameter is a floating point of 5 place precision where 0.00001 is the minimum value and 9999.99999 is the maximum value. If a participant wants to trade more, he or she can do so with multiple messages.

Price includes an floating point amount with 5 place precision and a 4 character string for the currency type. As in 0.04 BTC. The symbol explains what one is willing to buy or sell. The price concludes how much the person is willing to give or accept for the transaction and in what currency.

Setting an expiration date allows participants to protect themselves against volatility. If one is selling for a market price in a volatile market, they are protected against the market sharply changing just before a transaction is made. This parameter is a floating point in minutes of 2 place precision where 0.01 is one-tenth of a minute or 6 seconds as the minimum value and 59.99 is the largest value accepted. Note that this is the time for contract expiration and does not account for network delays in sending messages.

Each message is posted on the Ethereum blockchain as a Whisper Message and sent to every participant watching for the specific exchange topic. The first person to accept the terms of the message is the one who is counter party to the trade. This acceptance generates a smart contract that triggers on the criteria of the message.

It is important to note that there a cost associated with sending messages in the form of proof of work. When a participant sends a message through Whisper, he or she must have poof of work on the network before a message is sealed to be sent. The more work performed for the seal, the higher the importance given to the message.

Smart Contract

A smart contract is a means to exchange items without the services of an escrow agent and is similar to an options contract. These contracts are written in code and stored on the blockchain. A trigger event is set so that the contract is executed once all criteria have been meet. Triggers may include a strike price, expiration date, etc…

“Smart contracts … guarantee a very, very specific set of outcomes. There’s never any confusion and there’s never any need for litigation.” — Jeff Garzik

In the decentralized exchange, each message accepted to buy or sell generates a smart contract. It is written in Solidity for both the Ethereum network and Bitcoin network through RSK. A contract in the sense of Solidity is a collection of code and data that resides at a specific address on the Ethereum blockchain and/or the Bitcoin blockchain.

The smart contract contains all of the parameters from the message as triggers. Meaning that once all criteria have been satisfied, a transaction begins on all networks involved. If the transaction fails, the smart contract must be able to try again for one additional time. If it fails a second time, the contract is cancelled and the parties alerted.

Each party is charged a fee from the network(s) involved in the transaction. In a smart contract, the initiator of a transaction is charged a fee. This initiator is the buyer on one side of the trade and the seller on the other.

Cross-Network Transactions

The most difficult task involved in a decentralized exchange are cross-network transactions. These are the details involved with moving currency on one network in relation to a move on a separate network using smart contracts.

The 2-way peg or 2WP allows the transfer of bitcoins from the Bitcoin blockchain to a secondary blockchain and vice-versa. Bitcoins are not transferred, but temporarily locked on the Bitcoin blockchain while the same amount of equivalent tokens are unlocked in a secondary blockchain. The original bitcoins can be unlocked when the equivalent amount of tokens on the second blockchain are locked again in the secondary blockchain.

For example, if one accepted a message to buy 100 BTC with 1000 ETH, a 2-way peg is created so that 100 BTC are locked on the Bitcoin blockchain and 1000 ETH are locked the Ethereum blockchain. Once the transfer of 1000 ETH on the Ethereum blockchain occurs, the 100 BTC are transfered and the contract marked as complete by RSK.

Since both the Ethereum and Bitcoin networks are supported, it will allow multiple currencies to be exchanged within the decentralized exchange.


The decentralized exchange is a sudo-anonymous message system for the buying and selling of cryptocurrencies and tokens. It offers no listings of what is available and no centralized ticker of current prices. Even standard exchange features such as market price and volume must be pulled directly from the networks involved. Even the base interface is not an interface but a distributed smart contract system with messaging.

None of this is very user friendly and that is the point. The community can build user interfaces, API endpoints, and even complete applications to interact with the decentralized exchange.


Building a decentralized exchange is a really exciting project. My development team has already begun experimenting with using smart contracts across blockchains and hopes to clarify further on the best method to move forward within the coming weeks.

Once that is accomplished, the additional parts will be assembled into a working prototype and tested internally. Afterward, we will release as open source code on GitHub, seeking the help of the Blockchain community to get this project to the finish line.

If you want to help or have feedback, please send a message.

Like what you read? Give Todd Moses a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.