I am a software engineer and technical writer with experience in blockchain, frontend, and backend technologies.
Published on
Have you ever thought about how you can create an application, then forget all about it and possibly still make your money? Have you also wondered how you can build an application that doesn't need maintenance whatsoever? Well, the blockchain allows you to do all of that through decentralised applications (or dApps). dApps allow you to develop applications on a blockchain network without providing any resources or servers. They also don’t require your control or maintenance to operate, and are more secure than regular applications.
In this article, you will learn how to build a decentralised application on the ethereum network with Svelte JS, Ethers and the Truffle framework.
To follow this article, you need the following:
You don’t need to understand Ethers and Truffle, but understanding them helps.
A dApp (decentralised application) is an app that runs on a peer-to-peer network like the blockchain. DApps can often be confused with regular apps because they use a similar interface. The only things that separates a dApp from an app is the lack of a central server, and the use of a blockchain network. A popular example of a dApp is OpenSea. It interacts with several blockchain networks, and it isn’t hosted by a central server.
Most dApps rely on smart contracts to process data on a blockchain. Smart contracts are very important and are a very secure method for dapp to store and process data collectively. Development of smart contracts varies on all blockchain. Ethereum provides a programming language called Solidity, for creating them.
Before building the application, let’s start by setting up our. Make sure you have truffle and ganache installed in your system. If they are not installed, run this command to install them:
Truffle is a framework for building, compiling and deploying smart contracts. And Ganache allows you to create a local development blockchain in your computer system.
You can confirm that the packages were installed with these commands:
Now, start up the local development blockchain with ganache:
The environment allows you to deploy and experiment with smart contracts on a simulated local blockchain.
Now, you can initialize the project with this command:
This will create a svelte-dapp directory with the following tree structure:
Open the svelte-dapp folder in your terminal and create the svelte frontend project with this command:
In your terminal, enter into dapp-frontend and run the command below to install the dependencies:
Finally, install ethers because it allows the dApp to interact with the Ethereum blockchain:
Now that you have your project setup, you can build the smart contract. To build a smart contract, create a new file inside the contracts folder. For this project, create a StoreMessage.sol file with the below in it:
The smart contract is a simple solidity script that:
This smart contract has two interfaces when you deploy it:
Before compiling the contract, make sure that the version of your compiler is the same as the smart contract ( 0.8 ) in the truffle-config.js file:
To compile this contract, run the command below:
Note: you need an active internet connection. Truffle has to download the solidity compiler before compiling.
After compiling, the next thing to do is to deploy the contract. To deploy the contract, create a migration script. The migration script allows you to execute operations before and after deploying the contract. To create the migration, open the migrations folder and create a new 2_deploy_message_contract.js file with the following in it:
Here's a breakdown of the script:
After creating the migration, open the truffle-config.js file and uncomment the development object:
Truffle uses the development entry to connect to the local development node at “http://127.0.0.1:8545”. To deploy the smart contract, run this command:
When you run the deploy command, truffle executes the migration files in the order of their name.
To interact with the smart contract from truffle, run this command:
The truffle console command spins up a console that allows you to interact with smart contracts. To create the instance of the deployed StoreMessage contract, run:
You can get the contract’s address with:
You can also call the contract methods:
With the contract ready, you can start building the front-end for the dApp. Do not close the ganache terminal. This terminal will act as a blockchain network for the frontend.
A dApp’s front-end provides the ability to interact with a blockchain to non-technical users. The front-end is an intuitive interface that doesn’t require any specialized knowledge before use.
Paste the code below into the svelte’s project src/App.svelte file:
You also need to create a contractData.js file inside the src folder, and paste the following into it:
Note: Replace the value of address (“contract address”) with the address of your deployed contract. You can find the contract’s address in the output of truffle deploy.
The App.svelte component provides a basic interface that looks like the below:
To interact with the blockchain network, this dApp needs the users to connect their Ethrereum wallet. The wallet pays the fees required to interact with certain functionalities like calling some types of smart contract functions and making other types of transactions.
When the user clicks the “Connect Wallet” button, the browser calls the connectWallet function. And the following happens:
If Metamask is installed on the user’s browser, there will be a window.ethereum object. You can use this fact to check that Metamask is installed.
One of the operations that happen in a dApp is interacting with smart contracts. The dApp uses a contract instance to interact with a contract. You create this instance by passing its address on the network and its ABI (Application Binary Interface). The ABI is an encoded description of a smart contract’s interface.
When the user clicks the “Call Contract” button, the browser calls the callContract function. And the following happens:
After building your dApp, you need to see how well it runs. To run the dApp, you need a browser with the Metamask extension installed, and a metamask wallet account.
Open http://localhost:8080 in the browser to view the dApp. Then connect Metamask to the development node by opening the extension at the top-right, then Selecting “Localhost 8545” in the network drop-down:
Now you can click on connect wallet, you should see a metamask pop-up requesting to connect your wallet. And after connecting the wallet, click on call contract, to get the contract message.
Note: Make sure that the contract address in contractData.json is equal to the deployed contract address.
In this tutorial, you learned how to build a dApp using Svelte and Ethers JS. There’s more that you can learn about building dApps, and you don’t have to stop here:
You can visit the Truffle docs to learn more about ganache, and truffle. You can also learn how to deploy a smart contract to a public testnet or mainnet. You can learn more about Metamask in its documentation. You can get the final project in this GitHub repo.
Thanks for reading and happy coding!
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop