Popular Posts

How to Build a Simple dApp with Ethereum and Solidity

Ethereum encryption concept


Decentralized applications (dApps) are transforming the way we interact with digital platforms—offering transparency, security, and user empowerment. In this guide, we’ll walk you through building a simple dApp using Ethereum and Solidity: a basic blockchain-based voting system.


What is a dApp?

A decentralized application (dApp) is an application that runs on a blockchain network rather than a centralized server. This design ensures transparent and tamper-proof operations, driven by community consensus rather than a single authority.


Why Ethereum and Solidity?

  • Ethereum is one of the most widely adopted platforms for dApp development.
  • Solidity is Ethereum’s primary smart contract language, purpose-built for creating decentralized logic on the blockchain.


Project Overview: A Simple Voting dApp

We'll build a dApp that allows users to:

  • Cast a vote for a candidate.
  • View the current vote count for any candidate.


Step 1: Set Up the Development Environment

To begin developing on Ethereum, you’ll need a few essential tools:

1. Install Node.js and npm

2. Install Truffle 

npm install -g truffle

3. Install Ganache


Step 2: Create the Smart Contract

Solidity Code for Voting

// contracts/Voting.sol

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;


contract Voting {

    mapping (bytes32 => uint256) public votes;


    function vote(bytes32 candidate) public {

        votes[candidate]++;

    }


    function getVotes(bytes32 candidate) public view returns (uint256) {

        return votes[candidate];

    }

}

Compile and Deploy with Truffle

  1. Run truffle init to scaffold a project.
  2. Create the Voting.sol file in the contracts directory.
  3. Add a migration script in the migrations/ folder.
  4. Start Ganache, then run:

truffle compile

truffle migrate

 

Step 3: Build the Frontend Interface

Connect Web3 to the Smart Contract


// frontend.js

const Web3 = require('web3');

const web3 = new Web3(window.ethereum); // MetaMask provider


const contractAddress = '0x...'; // Replace with your deployed contract address

const contractAbi = [...]; // Paste your contract ABI here


const contract = new web3.eth.Contract(contractAbi, contractAddress);


async function vote(candidate) {

    const accounts = await web3.eth.getAccounts();

    await contract.methods.vote(web3.utils.asciiToHex(candidate)).send({ from: accounts[0] });

}


async function getVotes(candidate) {

    const result = await contract.methods.getVotes(web3.utils.asciiToHex(candidate)).call();

    console.log(`Votes for ${candidate}:`, result);

}

Basic HTML for UI

<!DOCTYPE html>

<html>

<head><title>Voting dApp</title></head>

<body>

  <input type="text" id="candidate" placeholder="Candidate Name">

  <button onclick="voteCandidate()">Vote</button>

  <button onclick="checkVotes()">Get Votes</button>


  <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

  <script src="frontend.js"></script>

</body>

</html>


Step 4: Test and Deploy

1.Test the dApp Locally

  • Interact with your contract via the frontend and verify all functionalities work using Ganache.

2. Deploy to a Testnet

  • Configure Truffle to use a testnet like Goerli or Sepolia.
  • Use Infura or Alchemy for provider access.
  • Fund your wallet with test ETH.
  • Run:

truffle migrate --network goerli


Conclusion

Congratulations! You've built a simple but functional dApp using Ethereum and Solidity. This project introduced you to the basics of smart contract development, frontend integration, and blockchain deployment.

With this foundation, you can now explore more advanced features such as:

  • User authentication with MetaMask
  • Persistent state updates
  • Enhanced security practices
  • Voting session management


Frequently Asked Questions (FAQs)

What is Ethereum?

Ethereum is a decentralized blockchain platform that enables the creation of smart contracts and dApps.


What is Solidity?

Solidity is a contract-oriented programming language used to write smart contracts on Ethereum.


How do I get started with blockchain development?

Begin by learning blockchain fundamentals, then move on to Ethereum and Solidity. Tools like Truffle and Ganache are ideal for local development.


What are the benefits of dApps?

dApps provide transparency, security, resistance to censorship, and decentralized ownership.


Can I deploy my dApp to the Ethereum mainnet?

Yes. After thorough testing, you can deploy to the Ethereum mainnet using the appropriate configuration and deployment tools.

Powered by Blogger.