Building Decentralized Applications (Dapps)

Building Decentralized Applications (Dapps)

A Guide to Building Your First Dapp

So, you want to build a decentralized application on the Ethereum blockchain and you are wondering how to go about it? This article will explain the distinct steps to building a decentralized application. After this article, you will understand the entire development process of building a decentralized application.

You know how your phone can have a bunch of different applications, there's Facebook, YouTube, Gmail, and so on. They are all applications that run code that was created by the company so that you can interact with their company. Well, a decentralized application is very similar in how it works, except that instead of reporting back to the Facebook servers or YouTube servers, this application reports back to the blockchain. You are simply interacting with the blockchain through the application.

A decentralized application has some similarities with a web application, so we could reuse some of the development techniques of the web application, but we will need to adapt them a little.

In the rule of software, there are two ways to approach application development: The first way is to start from the data layer and progressively build up to the user interface (UI), and the other way is to start from the UI and progressively go down to the data layer.

If your application is data-intensive with a complex data layer, you probably want to start from the data layer, otherwise, you can start from the UI. It also depends on whether you have a background in front-end or back-end development. But with a decentralized application, there is an extra difficulty, because we have to split the data between a smart contract on the blockchain and some centralized server outside the blockchain. So, we would be constrained by what we can do with the blockchain. For this reason, I recommend starting with the data layer.

1. Understanding Your Data Model

** The first step is to understand your data model. ** Your primary task will be to decide what you put on a smart contract and what you put outside of the blockchain. Anything critical to your application should be on the blockchain. For example, if you have any economy or payment part, it should be on the blockchain. Also, if you have content you want to protect against any censorship, then it should be on the blockchain. However, for things like user settings, you can put them outside the blockchain. But there is a caveat; because storing data on the blockchain is very expensive, we try to minimize the data that we put on the blockchain to a bare minimum. For instance, let's say that you are building a decentralized Facebook and you need to store all the posts of all the users-with texts and images. The problem is that texts and images can take up a lot of space and it would be way too expensive to put this on a smart contract. So, what you would do in this case is to put the metadata on your smart contract, and the actual content-the text and images, outside the blockchain.

But you still want to link up these contents to the smart contract. The way to do this is by calculating a data fingerprint of these contents by using some hashing function and storing these hashes inside your smart contract. These hashes allow you to compare the data that is retrieved from the outside to what you have in the smart contract. So, if anyone tried to tamper with these contents outside the blockchain, you could figure it out, and these hashes do not take up too much space on the blockchain.

2. Create A Smart Contract

Once you figure out your data model and you know which data goes where, ** the next step is to code your smart contract. ** For this, you are going to use a few tools. The first tool you will use is Remix, which is an online IDE for solidity. To use Remix, you do not have to do any installation. Just visit the website of remix https://remix.ethereum.org/ and right away, you can start developing your solidity smart contract.

Solidity is the most popular programming language for smart contracts in Ethereum. Remix is good for beginners and as a playground. But when you do a more advanced smart contract project, usually you will want to use a framework called Truffle.

Truffle is the most popular development framework for Ethereum. We use truffle to compile our smart contract locally and deploy the smart contract to either a network local to our machine, a testnet, or the mainnet. We also use truffle for testing our smart contract—to write unit tests to make sure that the logic in our smart contract works exactly the way we want it to before we deploy it to the mainnet. To get started with truffle, make sure you have the node package manager installed. If you install node, you will get node package manager. Next, install truffle using npm by running npm install truffle -g in the terminal of the code editor of your choice. This installs truffle globally and you can use it in your project. In a new empty directory, you can run truffle init and this would initialize your directory with the basic stuff you need to set up a truffle work environment.

Another tool you will need is a blockchain simulator for blockchain development. The most popular one is called Ganache. Fortunately, if you use Truffle, it already includes Ganache. You don't need to install it separately.

To interact with your smart contract, you can hit directly the API of Ethereum but this is quite complicated, so it is better to use a library like web3. Web3 allows you to interact with your smart contract easily, without knowing all the details of the Ethereum API.

3. Test Your Smart Contract

** The next step is to test your smart contract. ** Once you deploy your smart contract to the blockchain, it is not possible to change it anymore. On top of it, it might manipulate money. So, if there is a bug, the consequences can be catastrophic. For this reason, you need to test your smart contract thoroughly and make sure your smart contract is bug-free before you deploy it to the actual blockchain. Fortunately, you can do this with the truffle framework.

To test your smart contract, you will go over it and test all the functions, and make sure that you cover all the various use cases. You can use the library school OpenZeppelin Test Helpers with a couple of very useful utility functions for testing your smart contract. To use the OpenZeppelin library, simply run npm install @openzeppelin/test-helpers on your terminal.

When you want to run your test, first, you will run it on your local development blockchain called Ganache. Ganache is very convenient for local development, but this is not a very realistic testing environment. From time to time, it is also best practice to run your test on a testnet. A testnet is a test blockchain network that works to run and test blockchains or blockchain projects before they are ready to be launched. It is a simple way for developers to create, modify, and test the functionalities of their dapp, as well as monitor its performance before making it accessible to the public. Here, developers can troubleshoot any issues and fix bugs. In a testnet environment, multiple tests can be run repeatedly, which allows for performance comparison. This means that consistency can be checked.

By running independently to the mainnet, testnets allow for the full testing of a blockchain project without interfering with the transactions on the mainnet. This sandbox allows developers to take risks, experiment, and thus create the best model to be launched. A testnet is a sort of mirror of the actual Ethereum blockchain that we call the mainnet. But on testnet, you can break things. It's like a safe playground to experiment on. As a prototype, a testnet should never be used to transfer anything of value, and thus testnets use fake money, or tokens without value, to run their protocols. All ether on this testnet is not real ether, so you can make a mistake. Testnets allow for faster and safer launching on the mainnet.

Why are Testnets important?

Everyone can agree that running checks on any system, especially a financial system, is paramount before its launch. But why can't this simply be run on the mainnet? First, running tests on a mainnet can be very expensive. Blockchain fees will need to be paid for every transaction made, every change, and each time a dapp is launched—which during a trial phase are multiple and repeated. This would mean that to test a dapp efficiently and explore its plausible options, it would incur incredibly high fees. If an issue occurs during the trial phase on the mainnet, it could disrupt the entire network, its user's assets, and transactions. This means that users could lose their money. This could be very expensive—both for developers, and the reputation of cryptocurrency.

The second is a compatibility issue. Testnets use nothing of real value and instead use fake money as valueless tokens. Testnet coins are not compatible with mainnets, and vice versa, meaning that new coins or methods would need to be minted, resulting in a much longer, complicated, and expensive process. For this reason, all new dapps should first be tested on an independent blockchain with its very own genesis block, such as a testnet. In this way, those operating in the cryptocurrency market can remain separate from any "trial and error" operations, and developers can be free to test all aspects of the blockchain before its launch.

To deploy on a testnet, you need to run your Ethereum node that is connected to this public testnet. This can be challenging, so most people use an API service called Infura.

What is Infura?

Infura is a set of tools used to create an application that connects to the Ethereum blockchain. It interacts with the Ethereum blockchain and runs nodes on behalf of its users, making it easy for the developers. It is used by MetaMask, CryptoKitties, Opera, 0x protocol, Lucidity, Brave, and more. Infura is easy to use. You create a free account; they give you an API key. After that, you can deploy your smart contract.

Some advantages of Infura include;

  • Fast - Access to the Ethereum blockchain becomes much quicker.

  • Scalable - By managing the nodes, developers do not have to worry about infrastructure limits.

  • Data Storage - Instead of storing it all on-chain, data can be stored separately, with just a hash stored on the blockchain.

Some major drawbacks of Infura include;

  • Infura is a centralized service and is therefore vulnerable to attacks that may limit its functionality and it could be used to censor transactions by governments or third parties

  • Recently, MetaMask stopped working because of a service outage of infura, thereby affecting a lot of transactions.

Currently, running an Ethereum node is not feasible for an average user, so Infura is creating a giant on-ramp for people to use Ethereum. Until Ethereum becomes faster and easier to use, Infura's role as a centralized service provider will continue.

4. Create Front-end Application

Once you have tested your smart contract, the next step is to ** create the front-end application. ** Strictly speaking, a front-end application is unnecessary because it is possible to interact with a smart contract with your command line, but realistically, your end-users will never do this. For most dapps, the front-end is a web front-end but you can also build a mobile application.

To create the front-end application, you can use any front-end framework that you usually use for web applications such as React, Vue, Angular, and so on. React, however, is the most compatible with other tools. Another framework you can look at for the front end is Drizzle. Drizzle allows you to keep the UI of your dapp constantly up to date with your smart contract. Internally, it monitors the state of the blockchain, and whenever there is a transaction that interests you, it will update an internal redux store, and after that, all you have to do is connect your UI to this redux store, and you will constantly stay up to date.

5. Deploy on Mainnet

A mainnet is the "main" "net", or network, that a dapp is running on. A mainnet comprises a fully launched net, where cryptocurrency transactions can be processed efficiently, verified accurately, and recorded securely. As a fully functioning blockchain, mainnets can send and receive any transaction, such as cryptocurrencies, non-fungible tokens (NFTs), or transfer information. Mainnet can run a project that requires a specific blockchain protocol. For example, dapps on the Ethereum mainnet. Mainnet is the actual blockchain of Ethereum, where you manipulate real ether, so you do not want to make any mistake on this blockchain. For the mainnet, you also need to run your own Ethereum node and this is even harder for the mainnet than for the testnet. So again, most people use Infura for this.

For deploying on the mainnet, you need to use real ether. If there is a mistake during the deployment and something goes wrong, you will lose real ether. This means that you will lose real money, so you have to be very careful.

6. Manage Your Dapp in Production

After you have deployed your decentralized application, is this all you have to do? Well, no. Because you have to manage it. First, you need to monitor errors. Some users might misuse your dapp and send some transactions that fail, so you need to be aware of this. For the web application, there is a very famous service called New Relic that monitors the errors that happen in the front-end application. For decentralized applications, there is a service called Moesif. Moesif injects some JavaScript code into your dapp front-end, reports any error that happens, and you can inspect all of this data.

You also need to manage the evolution of your smart contract protocol. I said earlier that once you deploy your smart contract to the blockchain, it is not possible to update its code—and this is true. But there are some techniques to go around this limitation and still be able to update your smart contract by using a system of proxy smart contracts. If you are interested in this, you can check out the OpenZeppelin SDK. This differs from the OpenZeppelin solidity library.

If you introduce an update, you also have to think about governance, because in a decentralized application, you cannot have any centralized part as that kind of breaks the whole promise of decentralization. Governance is a mechanism where you allow the community to vote on the evolution of a protocol. Typically, your users will own some token of your project and at regular intervals, they will vote, and based on the result, the smart contract will automatically update to another version.

Benefit of Dapps

The evolution of applications into dapps has several advantages which include:

  • We can only build Dapps on smart contract networks. This means that a blockchain like bitcoin cannot use dapps. Bitcoin was designed to only allow the sending of its native coin. Ethereum, however, can use smart contracts. They allow two people to make an agreement written in code so that they do not have to trust a third party to follow through. They can easily trust the code.

  • Dapps are open source. Since the code is out there to be run, anyone can look at the code. This allows a lot more trust to be given to the dapp. We cannot look at how Facebook recommends friends to us to interact with, or how google knows what we ate for dinner. But with any smart contract, we can look at the code and see how it works. This is an enormous benefit over centralized applications.

  • Dapps are also censorship-resistant and this is a fancy way of saying that they do not have to answer the government or corporations or even specific people. If a smart contract is coded to do something and is triggered, nobody can stop it. Naturally, this is good and bad, but it is really important for financial applications. This way, nobody can control your money.

  • Dapps never go offline. There have been times when Facebook or even YouTube went down. Maybe their servers had a bug, or there was an outage, or they simply just did not work. It is usually for a short time because there is a lot of money involved, but technically, the government could just come in and shut them down.

Since dapps run on the blockchain, they will never go down because hundreds of thousands of computers run them all over the world. It would be infeasible to turn them all off—if not, impossible.