Building DApps with React and Solidity on Ethereum
Jan 13, 2024
0
A decentralized application (DApp) is a type of software application that operates on a decentralized network rather than being controlled by a single entity or authority. Unlike traditional applications that rely on central servers, DApps leverage blockchain technology to ensure decentralization, transparency, and immutability.
DApps are built on top of blockchain platforms like Ethereum, Solana, and Avalanche. Ethereum is the most popular blockchain platform for building DApps.
In this tutorial, we will learn how to build a DApp using React and Solidity on Ethereum. We will also learn how to deploy the DApp on the Ethereum blockchain and interact with it using Metamask.
Setting up the Remix IDE#
Remix IDE is an online IDE that allows you to write, compile, and deploy smart contracts on the Ethereum blockchain. It is a great tool for beginners to get started with smart contract development. Since it has everything you need to get started with smart contract development, you don't need to install anything on your computer.
To get started, head over to the Remix IDE and create a new project. You will see a screen like this :
Remix IDE |
Delete the default contract files and create a new contract called Storage.sol
. We will use this contract to store and retrieve number from the Ethereum blockchain.
Writing the Smart Contract#
Let's write a simple smart contract that will allow us to store and retrieve number from the Ethereum blockchain.
The Storage
contract has two functions: setData
and getData
. The setData
function allows us to store a number on the blockchain, and the getData
function allows us to retrieve the number from the blockchain.
Web3 Wallets connection using WalletConnect in Next.js
WalletConnect allows to connect mobile wallet to different decentralized applications and other wallets. This allows to interact with dapps and sign transactions securely from wallet.
Read Full PostDeploying the Smart Contract#
Now that we have written the smart contract, let's deploy it on the Ethereum blockchain. To deploy the smart contract, click on the Deploy & run transactions
icon on the left sidebar.
On the Top you can see
Environment
dropdown, selectInjected provider - metamask
from the dropdown. This will allow you to interact with the smart contract using Metamask.Make sure you have Metamask installed on your browser and you are connected to the
Sepolia Test Network
.You can get free test ETH from here.
Now that everything is set up, click on the Deploy
button to deploy the smart contract.
Once the smart contract is deployed, you will get the address of the smart contract. Copy the address and save it somewhere as we will need it later.
We need to copy the ABI (Application Binary Interface) of the smart contract as well. To copy the ABI, click on solidity compiler
icon on the left sidebar and click on the ABI
and copy and store it.
Building the Frontend#
We will use Create React App to set up the React app. To get started, run the following command :
Once the React app is created, navigate to the project directory and install the following dependencies:
Setting up Metamask SDK#
Create a utils
folder in the src
directory and create a file called MetamaskProvider.jsx
inside the utils
folder. We will use this file to initialize the Metamask SDK.
Now that we have initialized the Metamask SDK, let's wrap the Index
component with the MetamaskProvider
component. Index
component is the entry point of the React app.
Setting up Contract Instance#
Within the utils
folder, create a file called constant.js
. We will use this file to initialize the contract instance.
We will add our contract address and ABI in this file.
To initialize the contract instance, we will setup a Context
with following code :
Explanation:
- We are using the
useSDK
hook from the@metamask/sdk-react
package to initialize the Metamask SDK. Which will allow us to interact with the Metamask wallet. - We are using the Web3 package to initialize the contract instance. We are passing the
provider
to theWeb3
instance. Theprovider
is provided by the Metamask SDK. Theprovider
allows us to interact with the Ethereum blockchain. - We are passing our
ABI
andcontract address
to theWeb3
instance to initialize the contract instance.
Now that we have initialized the contract instance, let's wrap the App
component with the AppContextProvider
component. as we wrapped the Index
component with the MetamaskProvider
component.
Building the UI#
Now that we have set up the React app, let's build the UI. We will create a components
folder in the src
directory and create a file called Home.jsx
inside the components
folder.
As you can see, we have created a simple UI that allows us to add and retrieve the number from the blockchain.
I have created two functions: getNumber
and handleAddNumber
. The getNumber
function allows us to retrieve the number from the blockchain, and the handleAddNumber
function allows us to add the number to the blockchain.
To Learn more about Web3, check out the official documentation.
Now that we have created the UI, let's add the Home
component to the App
component.
Now that we have added the Home
component to the App
component, let's run the app and see if everything is working fine.
To run the app, run the following command:
You will be able to Add and Retrieve the number from the blockchain.
Make sure you are connected to the Sepolia Test Network on Metamask since we are using seploia for this tutorial.
You can find the complete code here
Conclusion#
Building DApps is fun and exciting. In this article, we learned how to build a DApp using React and Solidity on Ethereum. We also learned how to deploy the DApp on the Ethereum blockchain and interact with it using Metamask.
There are so many things that we can do with DApps. I hope you enjoyed this tutorial. If you have any questions, feel free to comment below. I will try to answer them as soon as possible.
Comments (3)