I have created a contract on remix.ethereum.org and started the Ganache(http://truffleframework.com/ganache/) as private blockchain.
On the Remix I have configured Remix to work with Ganache through custom web3 provider which is my localhost.
It's a simple voting Dapp:
pragma solidity ^0.4.11;
contract VotingDapp {
mapping (bytes32 => uint8) public votesReceived;
bytes32[] public candidateList;
function VotingDapp(bytes32[] candidateNames) public {
// constructor
candidateList = candidateNames;
}
function totalVotesFor(bytes32 candidate) public view returns (uint8) {
if(validCandidate(candidate) == false) revert();
return votesReceived[candidate];
}
function voteForCandidate(bytes32 candidate) public {
if(validCandidate(candidate) == false) revert();
votesReceived[candidate] += 1;
}
function validCandidate(bytes32 candidate) public view returns (bool) {
for(uint i = 0; i < candidateList.length; i++){
if(candidateList[i] == candidate){
return true;
}
}
return false;
}
function totalVotesReceived() external view returns(uint){
uint sum = 0;
for(uint i = 0; i < candidateList.length; i++){
sum += votesReceived[candidateList[i]];
}
return sum;
}
}
When I try to call function voteForCandidate I get this error from Remix :
Gas estimation failed
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
VM Exception while processing transaction: revert
When I try to force sending nothing happens.
Thanks
Best Answer
From a quick look at your code it looks like the only reason why
voteForCandidate
wouldrevert
would be if the candidate you're passing it isn't in yourcandidateList
, so it fails thevalidCandidate
test. Check the content ofcandidateList
is what you expect, and check the parameter is being passed correctly intovoteForCandidate()
.