How to Make a dApp
Published May 30, 2022.
Decentralized applications (dApps) are programmed to execute specific instructions in isolated environments. DApps can take any frontend, but the application's backend is stored on the blockchain. This means that data on a dApp cannot be changed and remains forever. The logic of a dApp is written as a smart contract in a Turing complete programming language. The most common of these languages is Solidity. DApps interact with decentralized blockchains using a JSON-RPC layer called Ethereum Web 3.0 API. Most browser-based dApps use Web 3.0 SDKs like Web3.js.
No central authority can control a dApp, which makes it censorship-free. Anyone with the right skill and resources can deploy and launch a dApp on a blockchain. This person only incurs transaction fees on computations.
DApps work as soon as they are deployed on the blockchain. Therefore, developers test dApps on a testnet before deploying them on the main network of the blockchain. Unlike centralized applications, dApps work perfectly without a trusted third-party facilitating transactions.
Factors to Consider Before Building a dApp
Before building your dApp, do some research to understand what you want and what potential users need. Simple considerations include the devices and platforms used by your target users, the problem you intend to solve, and how your dApp will be different. It is also important to consider the legal implications properly before launching a dApp.
The first technical point to consider when building a dApp is the preferred blockchain. The blockchain where a dApp is built can impact the application's userbase. Most developers prefer platforms like Ethereum because it has a large userbase, which means a large number of potential customers. Other considerations like scalability, interoperability, and programing language are also on the list.
Next, you need to consider the application's user interface (UI). The UI allows users to interact with your dApp. The frontend of most dApps is built using popular Javascript frameworks, such as React, for one-page websites.
How to Build a dApp
Your dApp needs to be connected to a blockchain like Ethereum. To achieve this, you need to write a smart contract that defines the business logic of the dApp and deploy it on Ethereum.
We will need a few dependencies to build our dApp. They are:
- Node Package Manager This is a dependency from Node.js. Type $ node -v in the command prompt or windows terminal to see if it is installed.
- Truffle Developers build the client-side of the dApp in Truffle. It provides an environment for writing codes and testing smart contracts, and you can install it using the $ npm install -g truffle command.
- Ganache It comes with over ten external account addresses preloaded with 100 test ETH.
- Metamask A Web 3.0 wallet and extension allows us to interact, settle fees, and deploy our contracts on Ethereum.
Your IDE should highlight syntax. If not, download the Ethereum syntax highlighting dependency.
Next, we need to install the truffle pet-shop-box and create a directory for contracts, node modules, migration, and truffle.js files. After that, you can start writing the smart contract. The following examples are adapted from an election dApp tutorial by DApp University. The code snippets are simple illustrations of some crucial steps in dApp development.
Next, number the file inside the migration directory in order of execution and create a variable called an election. Add it to the manifest of deployed contracts as shown in the next image. After that, run your migration from the command line and open the Truffle console to interact with the smart contract.
Next, we create a public mapping and function. Assign candidates to the mapping. Then, add two candidates by calling addCandidate. The resulting smart contract should look like this.
Next, test the contract and start building the client-side of the application. To do this, set up Web 3.0 by configuring it in the initWeb3 function. Fetch the deployed instances of the smart contract inside the Web 3.0 function. Find the value of the smart contract inside the function and assign some value to interact with it. Finally, use the render function to lay out content on the dApp user side page using smart contract data.
Add the voting function and check that the value is incremented when the function is called. Build the client-side of the dApp by adding an HTML form with corresponding Javascript code. Now, test all aspects of the dApp. The final step is to deploy on a live blockchain, but make sure there is no bug in your smart contract code.
Note that the complete codes are not shown here, as this is meant to give you an idea of how a smart contract is built. It is a process that starts from writing the smart contracts in the native programming language to building the client-side of the application. After that, test your dApp properly before deploying it on a live blockchain.
The cost of building a fully functional dApp varies. The average cost of building a DApp is about $38,000 to $91,000. Simple dApps are the least expensive, costing between $70,000 and $100,000. Medium dApps cost between $120,000 and $170,000. Complex dApps cost between $200,000 and $250,000. It takes approximately 3 to 6 months to build a dApp.
Top Platforms for Building dApps
Ethereum
Ethereum is a decentralized public blockchain designed to support the deployment of smart contracts in a runtime environment called the EVM. As the first blockchain that supports the development of dApps, Ethereum remains a force in the decentralized applications space. Even though the programming language isn’t easy, there are many developers able to write and deploy smart contracts and dApps on Ethereum.
Solana
Solana is a webscale blockchain that revolutionizes the idea of consensus in blockchains using a proof-of-history mechanism. Solana has been dubbed the Ethereum killer. It is most popular among developers because of its high scalability, with up to 700,000 transactions per second. Solana also uses a well-developed architecture, and its dApp programming language, Rust, is more secure, complex, and reliable than others.
Binance Smart Chain
Developers launch new projects on Binance Chain or Binance Smart Chain at a breathtaking pace. BSC is a proof-of-stake authority blockchain and fork of Ethereum. It uses a virtual environment similar to EVM and the same programming languages. Hence, Ethereum developers can also build projects on BSC. The platform's popularity is tied to one of the largest centralized exchanges globally. Over $75 billion is traded daily on Binance. Binance is the parent company behind BSC.
Cosmos
Cosmos is the internet of blockchains and is fast becoming a hub for developers. It features an ever-expanding ecosystem of dApps, primarily due to its IBC protocol that lets developers exchange assets across blockchains. Cosmos also provides Tendermint core as its consensus engine and an application layer known as the Cosmos SDK.