Blockchain 16 min read

Implementing a Weighted Voting DApp with ERC20 Tokens on Ethereum

This article guides developers through extending a simple Ethereum voting DApp by introducing ERC20 token-based weighted voting, covering token concepts, Solidity contract design with structs and mappings, functions for buying tokens, voting, and fund transfer, as well as deployment, testing, and front‑end integration steps.

Hujiang Technology
Hujiang Technology
Hujiang Technology
Implementing a Weighted Voting DApp with ERC20 Tokens on Ethereum

The tutorial begins by reviewing basic token concepts on Ethereum, such as ERC20 tokens that can represent assets like gold, company shares, or loyalty points, and explains how these tokens are implemented using smart contracts.

It then introduces a weighted voting system where each voter’s voting power is proportional to the number of tokens they hold. The Solidity contract is redesigned to include a struct voter that stores the voter’s address, total tokens purchased, and an array of tokens used per candidate.

Key contract functions are presented:

function buy() payable public returns (uint) – allows users to purchase tokens by sending Ether, calculates the amount of tokens based on the token price, updates balances, and returns the purchased amount.

function voteForCandidate(bytes32 candidate, uint votesInTokens) public – enables weighted voting by specifying the candidate and the number of tokens to allocate, checks that the voter has enough available tokens, updates the candidate’s vote count, and records the tokens used.

function transferTo(address account) public – demonstrates how the contract’s Ether balance can be transferred to an external account (note: the example lacks access control).

The article also shows how to initialize the contract with total token supply, token price, and a list of candidates via the constructor, and provides a migration script that deploys the contract using Truffle with parameters such as 10000 total tokens and a price of 0.01 ether .

Deployment steps are detailed, including compiling with truffle compile , migrating with truffle migrate , and interacting via the Truffle console to query votes, total tokens, tokens sold, and to execute token purchases and weighted votes.

Finally, the guide outlines the front‑end integration using a Webpack‑based project. JavaScript functions create a contract instance, populate candidate names and vote counts, display token statistics, and implement buyTokens and lookupVoterInfo to interact with the contract from a web page.

BlockchainsolidityDAppEthereumERC20Weighted Voting
Hujiang Technology
Written by

Hujiang Technology

We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.