Reputation: 1
i don't know where is the problem and how to solve, i tryed too much solutions but no one helps
let web3;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider('HTTP://127.0.0.1:7545'));
}
var bytecode = fs.readFileSync('./voting_sol_Ballot.bin').toString()
var abi = JSON.parse(fs.readFileSync('./voting_sol_Ballot.abi').toString())
const listOfCandidates = ['George W. Bush', 'Barack Obama', 'Joe Biden']
const votingContract = new web3.eth.Contract(abi);
(async function () {
const accounts = await web3.eth.getAccounts();
let candidates = new Map();
for (let i = 0; i < listOfCandidates.length; i++) {
candidates[web3.utils.asciiToHex(listOfCandidates[i])] = listOfCandidates[i]
}
votingContract.deploy({
data: bytecode,
arguments: [listOfCandidates.map(name => web3.utils.asciiToHex(name)),
[accounts[1], accounts[2], accounts[3], accounts[4], accounts[5], accounts[6], accounts[7]],
"1622371495", "1622371495"]
}).send({
from: accounts[0],
gas: '0xF4240', gasPrice: '0x4A817C800'
}).then((newContractInstance) => {
votingContract.options.address = newContractInstance.options.address
console.log("Contract address: " + newContractInstance.options.address)
})
})();
This is the .sol file
// SPDX-License-Identifier: GPL-3.0
pragma experimental ABIEncoderV2; pragma solidity >=0.7.0 <0.9.0;
contract Ballot {
struct Voter {
uint token;
bool voted; // if true, that person already voted
uint vote; // index of the voted proposal
address voterAddress;
}
struct Proposal {
bytes32 name; // short name (up to 32 bytes)
uint voteCount; // number of accumulated votes
}
address private government;
mapping(address => Voter) public voters;
Proposal[] public proposals;
uint private winningVoteCount = 0;
uint private tmpWinnerCount = 0;
uint public totalVote;
uint public voterCount;
uint private voteFinishTime;
uint private statViewFinishTime;
modifier alreadyVoted() {
require(!voters[msg.sender].voted, "Already voted.");
_;
}
modifier multipleWinner() {
_;
require(winningVoteCount != 0, "No vote submitted.");
require(tmpWinnerCount < 2, "Two or more candidate has been voted equally");
}
modifier onlyVoters() {
require(msg.sender != government, "Government cannot check vote.");
_;
}
modifier onlyValidVoters() {
require(voters[msg.sender].voted == false && voters[msg.sender].token != 0, "Only valid voters can cast a vote!");
_;
}
constructor(bytes32[] memory proposalNames, address[] memory voterArr, uint voteFinishTime_, uint statViewFinishTime_) {
government = msg.sender;
for (uint i = 0; i < proposalNames.length; i++) {
// 'Proposal({...})' creates a temporary
// Proposal object and 'proposals.push(...)'
// appends it to the end of 'proposals'.
proposals.push(Proposal({
name: proposalNames[i],
voteCount: 0
}));
}
totalVote = 0;
voterCount = voterArr.length;
voteFinishTime = voteFinishTime_;
statViewFinishTime = statViewFinishTime_;
for (uint i = 0; i < voterArr.length; i++) {
voters[voterArr[i]].token = 1;
}
}
function vote(uint proposal) public alreadyVoted() onlyValidVoters() {
Voter storage sender = voters[msg.sender];
require(sender.token != 0, "Has no right to vote");
require(block.timestamp < voteFinishTime, "Election has been finished! You cannot vote.");
sender.voted = true;
sender.vote = proposal;
proposals[proposal].voteCount += sender.token;
totalVote += sender.token;
sender.token -= 1;
}
function winningProposal() private multipleWinner()
returns (uint winningProposal_) {
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
tmpWinnerCount = 1;
}
if (proposals[p].voteCount == winningVoteCount) {
tmpWinnerCount += 1;
}
}
}
function winnerName() public
returns (bytes32 winnerName_)
{
require(block.timestamp > voteFinishTime, "Election has not been finished yet.");
winnerName_ = proposals[winningProposal()].name;
}
function checkMyVote() public view onlyVoters() returns (bytes32 proposalName) {
require(voters[msg.sender].voted, "You have not voted yet!");
uint vote_index = voters[msg.sender].vote;
return proposals[vote_index].name;
}
function getElectionResult() external view returns (bytes32[] memory names, uint[] memory voteCounts) {
require( block.timestamp > statViewFinishTime, "Election has not been finished! You cannot view the results.");
bytes32[] memory namesArr = new bytes32[](proposals.length);
uint[] memory voteCountsArr = new uint[](proposals.length);
for (uint i = 0; i < proposals.length; i++) {
Proposal storage proposal = proposals[i];
namesArr[i] = proposal.name;
voteCountsArr[i] = proposal.voteCount;
}
return (namesArr, voteCountsArr);
}
}
Iam connecting to ganache v2.7.1 it's not relaying to a public network
Upvotes: 0
Views: 1487
Reputation: 11
You are using pragma solidity >=0.7.0 <0.9.0
, which will raise a problem if your truffle-config.js
file containing solc : "0.8.20"(and above)
.
Change your solc version to "0.8.19" and try again because I did the same when faced this problem
Upvotes: 1