Reader small image

You're reading from  Blockchain Development for Finance Projects

Product typeBook
Published inJan 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781838829094
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Ishan Roy
Ishan Roy
author image
Ishan Roy

Ishan Roy leads the Blockchain initiative at the Centre of Excellence for Emerging Technologies (CEET) at the Tamil Nadu e-Governance Agency (TNeGA). He is currently working on the Tamil Nadu Blockchain Backbone project. His foray into blockchain began in the year 2016, at the blockchain team at ICICI Bank. This team carried out the first blockchain remittance in India. Since then he has worked as the Head of Products at HashCash Consultants where he built blockchain-enabled financial solutions for global clients. He has also mentored students and industry veterans as a blockchain trainer with Edureka. He is extremely passionate about technology and loves to keep himself abreast of new developments in the field through the community
Read more about Ishan Roy

Right arrow

Building a Decentralized Trading Exchange

This chapter deals with building a decentralized trading exchange using a private Ethereum blockchain. Traditionally, trading exchanges require an authority who will facilitate the trade between the buyers and the sellers. This authority will post the buy offers and sell offers from all the traders in a single platform, where they are visible to all the participants. This platform is referred to as the orderbook. Traders can trade against the offers that are posted on the orderbook.

In this chapter, we attempt to decentralize or basically remove the authority that manages the orderbook from the picture. The decentralized exchange will still have an orderbook, except it will be managed by a smart contract and will be available for all the participants to view and trade against in real time. We will be building an exchange on top of this...

Technical requirements

The code files of this chapter are available at https://github.com/PacktPublishing/Blockchain-Development-for-Finance-Projects/tree/master/Chapter%208.

We'll be using the following sources to develop our project:

To install Ganache on Ubuntu, you might need to change some settings. Click on the drop-down menu next to the directory name. Select Preferences. Navigate to the Behavior tab. Under Executable Text Files, set the option to Ask what to do. Navigate back to the file downloaded from the Ganache download link. Right-click on the file and click on properties. Select the Permissions tab. Select the Allow executing files as program option. Now...

Decentralized trading exchanges

Blockchains can play an important role in the disintermediation of financial systems. Most financial systems today include a number of middlemen and organizations that facilitate the smooth working of the entire ecosystem. Obviously, this means placing your trust with these intermediary players, and believing that they will not try to carry out any fraudulent activity, or relay incorrect information to you. This chapter is an example of a system that attempts to reduce the number of middlemen and authorities in the existing model, in order to build a faster and more transparent system. A decentralized trading exchange is one where the orderbook is not controlled by a central authority.

As the name suggests, the entire trading process is decentralized, meaning it is without a central authority or organization to facilitate the trades. Removing a...

Issuing the trading assets

In our exchange, we'll allow the user to buy or sell the gold asset using US dollars. Thus, gold will be our counter asset, that is, the asset that is being bought or sold. US dollars will be our base asset, that is, the asset that is being used to buy or sell.

Before we can build a trading exchange around these assets, we need to create these assets on our blockchain. To do so, we'll create an ERC20 token that represents gold, and an ERC20 token that represents USD. The tokens are fungible and fixed in nature, and can go up to two decimal places. One gold token represents one gram of gold, and one USD token represents one dollar.

Writing the contracts

We'll be using the OpenZeppelin...

Orderbook smart contract

The orderbook smart contract is the core component of our entire project.

It carries out the following functions:

  • Records incoming buy/sell offer requests from the frontend and moves the base or counter asset from the user's Ethereum account to the smart contract account.
  • Allows traders to view the recorded buy/sell offers on the smart contract.
  • Accepts trade requests against already recorded buy/sell offers and executes the trade. As part of the trade, the orderbook updates the available amount in an offer, and transfers the base or counter asset both from the trader's account and to the trader's account.
  • Emits an event to indicate when a new buy or sell offer is updated, or a new trade is carried out. This event will be subscribed to by our frontend app to update and display the orderbook in real time.

Now, let's write our orderbook...

Building the exchange app

The exchange app that we're going to build needs to perform the following functions:

  1. Keep a track of the Gold and USD token balance for the user.
  2. Allow the user to view orders on the orderbook contract.
  1. Update the orderbook in real time by listening to any buy/sell/trade events that are triggered by the orderbook smart contract.
  2. Allow the user to submit buy and sell requests.
  3. Match the buy/sell requests against existing orders in the orderbook if matching orders are available.
  4. If no matching orders are available, submit a new buy/sell order to the orderbook.

Alright, now let's start building our app. This section assumes basic-to-intermediate knowledge of React.js from the user. The entire React interface can be downloaded from the GitHub repository as follows:

https://github.com/PacktPublishing/Blockchain-Development-for-Finance-Projects...

Running the exchange app

Alright, so now that our exchange is complete, let's try running the app and see how it works:

  1. Make sure that your Ganache blockchain and Truffle console are online. If not, bring your Ganache blockchain online, and connect your Truffle console to the blockchain.
  2. Navigate to your Truffle project workspace, and enter truffle console in the Terminal in order to bring the console online.
  3. Enter the migrate command in order to migrate all your contracts to the Ganache test blockchain:
truffle(development)> migrate
  1. Once the contracts are deployed, note the contract address and keep them safe.
  2. Make sure that the contract address for the Gold, USD, and orderbook contracts are mapped correctly to the Gold.js, USD.js, and OrderbookABI.js contract interfaces in the exchange app code.
  3. Next, set up MetaMask to work with our project.
  4. Navigate to the main...

Summary

That brings us to the end of this chapter. This chapter should help you design decentralized trading exchanges, or exchanges on a distributed blockchain network in general. It should also help you to port concepts of trading, such as orderbooks and matching engines, to blockchain very easily. The example case that we looked at uses only a single orderbook pair, gold versus US dollars, but it can be very easily expanded to support multiple orderbook pairs and assets, and can be scaled into a full-sized exchange with additional components and security checks in place. You can run this on a private blockchain for an enterprise, or deploy it on a public blockchain. Bear in mind that public blockchains will require gas for each transaction, and you might want to club transactions together. Also, your orderbook will be visible to everyone on the public blockchain.

We started...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Blockchain Development for Finance Projects
Published in: Jan 2020Publisher: PacktISBN-13: 9781838829094
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ishan Roy

Ishan Roy leads the Blockchain initiative at the Centre of Excellence for Emerging Technologies (CEET) at the Tamil Nadu e-Governance Agency (TNeGA). He is currently working on the Tamil Nadu Blockchain Backbone project. His foray into blockchain began in the year 2016, at the blockchain team at ICICI Bank. This team carried out the first blockchain remittance in India. Since then he has worked as the Head of Products at HashCash Consultants where he built blockchain-enabled financial solutions for global clients. He has also mentored students and industry veterans as a blockchain trainer with Edureka. He is extremely passionate about technology and loves to keep himself abreast of new developments in the field through the community
Read more about Ishan Roy