Full Stack Hello World Voting Ethereum Dapp Tutorial — Part 3

  1. Learn to use new data types like struct to organize and store data on the blockchain.
  2. Learn the concept of tokens and its usage.
  3. Learn to make payments using Ether, the currency of the Ethereum blockchain platform.
/* When the contract is deployed on the blockchain, we will 
initialize the total number of tokens for sale, cost per token and
all the candidates
*/
function Voting(uint tokens, uint pricePerToken, bytes32[] candidateNames) public {
candidateList = candidateNames;
totalTokens = tokens;
balanceTokens = tokens;
tokenPrice = pricePerToken;
}
deployer.deploy(Voting, 1000, web3.toWei('0.1', 'ether'), ['Rama', 'Nick', 'Jose']);// 1000 is the total number of tokens for sale and the price of each token is set to 0.1 ether.
// We will revisit this section again later in this tutorial.
/* This function is used to purchase the tokens. Note the keyword 
'payable' below. By just adding that one keyword to a function,
your contract can now accept Ether from anyone who calls this
function. Accepting money can not get any easier than this!
*/
function buy() payable public returns (uint) {
uint tokensToBuy = msg.value / tokenPrice;
if (tokensToBuy > balanceTokens) throw;
voterInfo[msg.sender].voterAddress = msg.sender;
voterInfo[msg.sender].tokensBought += tokensToBuy;
balanceTokens -= tokensToBuy;
return tokensToBuy;
}
truffle(development)> Voting.deployed().then(function(contract) {contract.buy({value: web3.toWei('1', 'ether'), from: web3.eth.accounts[1]})})
  1. To vote for a candidate, you now have to specify the no. of tokens you want to vote with.
  2. There is a new section where you can purchase tokens.
  3. You can now look up voter information — how many tokens they own and no. of votes they have cast to each candidate.
  4. The candidates are not hardcoded anymore. We fetch the candidates from the blockchain and populate it.
var Voting = artifacts.require("./Voting.sol");
module.exports = function(deployer) {
deployer.deploy(Voting, 1000, web3.toWei('0.1', 'ether'), ['Rama', 'Nick', 'Jose']);
};
maheshmurthy|~/dev/ethereum_voting_dapp/chapter3$ truffle migrateUsing network 'development'.Compiling Migrations.sol...
Compiling Voting.sol...
Writing artifacts to ./build/contracts
Running migration: 1_initial_migration.jsDeploying Migrations...Migrations: 0xc9249947010675b8a3b1defb12334148f7f59010Saving successful migration to network...Saving artifacts...Running migration: 2_deploy_contracts.jsDeploying Voting...Voting: 0x795d6d1f7cf467f27e48181da5f1ebd5bbd0a8dfSaving successful migration to network...Saving artifacts...

Building www.boringcrypto.xyz - a community owned forum for non price crypto discussions.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

StaFi Protocol’s Liquid staking platform — A case for Protocol Security

The Team Behind The Digital Horse Racing Dream: Metahorse

Ethereum Summer Camp 2020 Week 2: Pop Culture and Blockchain

Reflections on 2018

Web3.0 in insurance: Futuristic or panacea?

Everything I Know About Ethereum — Part 1

Blockchain techs to watch for enterprises — promising privacy and performance

NGK’s unique blockchain ecological value advantage

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mahesh Murthy

Mahesh Murthy

Building www.boringcrypto.xyz - a community owned forum for non price crypto discussions.

More from Medium

KillSwitch joins hands with evry — decentralized exchange on BSC

Uniswap

Production Contract Security — Part 2

Encryption v Obfuscation for Privacy in Blockchain