TransWikia.com

Why are there errors in the code I copied in a book called Ethereum smart contracts development”

Ethereum Asked by dummy dummy on November 8, 2020

pragma solidity ^0.4.11;
contract simpleVotingDapp {
/*
The key of the mapping is candidate name stored as type bytes32 and value is an unsigned integer to store the vote count
*/
mapping (bytes32 => uint8) public votesReceived;
/*
We use an array of bytes32 instead to store the list of candidates
*/
bytes32[] public candidateList;
/* This is the constructor which will be called once when we deploy the contract to the blockchain.
When we deploy the contract, we will pass an array of candidates who will be contesting in the election
e.g.["Tom","Dick","Harry"]
*/

function simpleVotingDapp(bytes32[] candidateNames) {
    candidateList = candidateNames;
}

// This function returns the total votes a candidate has received so far
function totalVotesFor(bytes32 candidate) returns (uint8) {
    if (validCandidate(candidate) == false) revert();
    return votesReceived[candidate];
}

// This function increments the vote count for the specified candidate. This
// is equivalent to casting a vote
function voteForCandidate(bytes32 candidate) {
    if (validCandidate(candidate) == false) revert();
    votesReceived[candidate] += 1;
}

function validCandidate(bytes32 candidate) returns (bool) {
    for(uint i = 0; i < candidateList.length; i++ ) {
        if (candidateList[i] == candidate) {
            return true;
        }
    }
    return false;
}

}

I copied this code on a book by by Mukhopadyay, M. “Ethereum smart contracts development”;
function simplevotingDapp says
Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(…) { … }" instead.
function simpleVotingDapp(bytes32[] candidateNames) {
^ (Relevant source part starts here and spans across multiple lines).
Every function says Warning: No visibility specified. Defaulting to "public".
function validCandidate(bytes32 candidate) returns (bool) {
^ (Relevant source part starts here and spans across multiple lines).
Lastly function validCandidate says Warning: Function state mutability can be restricted to view
function validCandidate(bytes32 candidate) returns (bool) {
^ (Relevant source part starts here and spans across multiple lines).
What is wrong with it?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP