- You’ll build a simple voting DApp on Celo: a Solidity contract (Voting.sol) deployed with Foundry, and a React/Next.js front-end with web3.js to interact via MetaMask.
- The Solidity contract defines two initial candidates (Luna, Orion) and basic voting logic, tested with Foundry’s test suite.
- The front end fetches candidate data, checks the chain/network, and allows users to cast votes.
- This serves as a baseline for deeper improvements (security, Sybil protection, etc.) before production.
Introduction to Celo
Celo is an open-source blockchain ecosystem that makes decentralized financial (DeFi) tools and services accessible to anyone with a smartphone. It is designed to support financial inclusion and provide a platform for decentralized applications (DApps) with a particular emphasis on mobile usability.NFP
Not for production (NFP) obviously. Feel free to take the source and modify to your needs.We assume no responsibility for the code. Moreover, this is a very rough unaudited contract.Project overview: Simple voting DApp
In this tutorial, we’ll build a simple voting DApp on the Celo blockchain. The project involves deploying a smart contract using Foundry and creating a simple user interface with Next.js and web3.js to interact with MetaMask.Prerequisites
- Chainstack account to deploy a Celo node
- Foundry for smart contract development
- Next.js for the front-end framework
- web3.js for interacting with the blockchain
- Some Celo tokens
Step-by-step
Get a Celo node
Get you own node endpoint today
Start for free and get your app to production levels immediately. No credit card required.You can sign up with your GitHub, X, Google, or Microsoft account.Smart contract development
Use Foundry to develop, compile, and deploy a simple voting smart contract. Install Foundry on your machine; you can follow the instructions in the Foundry book. Once installed, create a new directory for your project and initialize a Foundry project.src
directory, rename the sample smart contract to Voting.sol
and paste the following contract:
Disclaimer
This Solidity smart contract is provided as an educational example and is not intended for production use. The code is simplified for clarity and lacks several critical features required for a secure and efficient production-grade application.TL;DR smart contract breakdown
This Solidity smart contract implements a simple voting system. Here’s a concise breakdown:-
Contract Name:
Voting
- Implements a basic voting mechanism.
-
Key Components:
-
Candidate Struct: Represents a candidate with a
name
andvoteCount
. -
Mappings:
candidates
: Maps a candidate ID to aCandidate
struct.voters
: Maps an address to a boolean indicating if the address has voted.
-
Candidate Struct: Represents a candidate with a
-
State Variables:
candidatesCount
: Tracks the number of candidates.
-
Events:
CandidateAdded
: Emitted when a new candidate is added.Voted
: Emitted when a vote is cast.
-
Constructor:
- Initializes the contract by adding two default candidates: “Luna” and “Orion”.
-
Functions:
-
addCandidate
:- Private function to add a new candidate.
- Increments
candidatesCount
and updates thecandidates
mapping. - Emits the
CandidateAdded
event.
-
vote
:- Allows a user to vote for a candidate.
- Check if the voter has already voted and the candidate’s ID is valid.
- Updates the
voters
mapping to mark the address as having voted. - Increments the candidate’s
voteCount
. - Emits the
Voted
event.
-
getCandidate
:- Returns the name and vote count of a candidate by ID.
-
Functionality summary
- Adding Candidates: Candidates can only be added internally via the
addCandidate
function, which is called in the constructor to add initial candidates. - Voting: Users can vote once for a candidate by providing the candidate’s ID. The contract ensures each user votes only once and only for valid candidates.
- Retrieving Candidate Info: Users can get a candidate’s name and vote count by providing the candidate’s ID.
Test smart contract
Now that we have a contract let’s implement a simple test script within Foundry. In thetest
directory, rename the current sample script to Voting.t.sol
and paste the following script:
Deploy the smart contract
We have a tested smart contract; let’s deploy it on Celo using Foundry and your Chainstack node. If you haven’t yet, ensure you have some Celo tokens. Let’s compile the smart contract:forge create
:
Make sure to add your RPC url and your private key to the command editing
YOUR_CELO_CHAINSTACK_RPC
and YOUR_PRIVATE_KEY
.Also note that this is a quick way to deploy and test, but exposing endpoints and private keys in your terminal is not a good security practice; ensure the wallet is used for testing only.We’ll need the contract ABI and the address where it was deployed for the front end. You can find the ABI in the Foundry project in
out/Voting.sol/Voting.json
. If you didn’t make any edits to the contract, you’ll find the proper ABI already implemented in the front-end code we’ll review in the next section.Developing the front end
Now that we have deployed the smart contract, we can create a simple front end so that users can interact with it and vote. Let’s initiate a Next.js project. You can do this in a different directory.src/app/page.js
and paste the following:
Remember to add your node URL and smart contract address in:Also note that exposing your endpoint in the front end like this is not good security practice, but it works for a prototype.
TL;DR front-end code breakdown
This React component implements a frontend interface for a simple voting application that interacts with a Celo blockchain smart contract. Here’s a concise breakdown:-
Dependencies:
Web3
library for blockchain interaction.- React hooks (
useEffect
,useState
) for managing state and side effects.
-
Key Variables:
nodeUrl
: URL of the Celo blockchain node.chainId
: Chain ID for the Celo Mainnet.contractAddress
: Address of the deployed voting smart contract.contractAbi
: ABI (Application Binary Interface) of the smart contract.
-
State Variables:
candidates
: Array to store candidate details.account
: Stores the user’s blockchain account address.isCorrectNetwork
: Boolean indicating if the user is connected to the correct blockchain network.loading
: Boolean indicating if candidate data is being loaded.error
: String for storing error messages.
-
Lifecycle Hooks:
useEffect
: Loads the user’s account on the component mount and checks the network status. If the user is connected to the correct network, it loads candidates.
-
Functions:
checkNetwork
: Checks if the user is connected to the correct blockchain network and switches networks if necessary.switchNetwork
: Switches the user’s MetaMask network to the Celo Mainnet.loadAccount
: Requests the user’s account address from MetaMask and checks the network status.disconnectAccount
: Resets the account and network status.loadCandidates
: Loads candidate details from the smart contract and updates the state.vote
: The user can vote for a candidate by using the smart contract.
-
UI Elements:
- Displays the application title and instructions.
- Connect button to log in with MetaMask.
- Displays the connected account address and log-out button.
- Displays a list of candidates with their names, vote counts, and vote buttons.
- Error messages and network status messages.
Run the DApp
All the pieces are together now. If you do not change the contract, the ABI in the front end will work. Otherwise, you’ll need to add the updated ABI. Run the project with:http://localhost:3000
: