如何解决Hardhat编译Solidity投票合约时的声明错误?
Solidity投票合约编译错误修复方案
核心错误修复步骤
- 结构体定义顺序问题:
mapping(address=>Voter)public voters;写在了Voter结构体定义之前,Solidity要求类型必须先定义再引用,需将Voter结构体移到该mapping上方。 - 计数器变量名不匹配:代码中调用
_voterId.increment(),但实际定义的变量是_votingId,需把变量名改为_voterId保持一致。 - 类型不匹配:
setCandidate函数的_age参数是string类型,但Candidate结构体里的age是uint类型,需将参数类型改为uint。CandidateCreate事件的age参数为uint,但触发事件时传入的是string类型值,需同步修正事件参数类型。
- 笔误修正:
alreday改为already,candiate改为candidate。
修复后的完整合约代码
//SPDX-License-Identifier: MIT pragma solidity ^0.8.9; import "@openzeppelin/contracts/utils/Counters.sol"; import "hardhat/console.sol"; contract Create{ using Counters for Counters.Counter; Counters.Counter public _voterId; Counters.Counter public _candidateId; address public votingOrganizer; //Candidate for voting struct Candidate{ uint256 candidateId; uint age; string name; string image; uint256 voteCount; address _address; string ipfs; } event CandidateCreate( uint256 indexed candidateId, uint age, string name, string image, uint256 voteCount, address _address, string ipfs ); address [] public candidateAddress; mapping (address => Candidate)public candidates; //end of candidate data //voter details struct Voter{ uint256 voter_voterId; string voter_name; string voter_image; address voter_address; uint256 voter_allowed; bool voter_voted; uint256 voter_vote; string voter_ipfs; } address []public votedVoters; address []public votersAddress; mapping(address=>Voter)public voters; event VoterCreated( uint256 indexed voter_voterId, string voter_name, string voter_image, address voter_address, uint256 voter_allowed, bool voter_voted, uint256 voter_vote, string voter_ipfs ); //end of voter data constructor() { //to deploy contract votingOrganizer=msg.sender; } function setCandidate(address _address,uint _age,string memory _name, string memory _image,string memory _ipfs)public{ require(votingOrganizer==msg.sender,"only organizer can create candidate"); _candidateId.increment(); uint256 idNumber=_candidateId.current(); Candidate storage candidate=candidates[_address]; candidate.age=_age; candidate.name=_name; candidate.candidateId=idNumber; candidate.image=_image; candidate.voteCount=0; candidate._address=_address; candidate.ipfs=_ipfs; candidateAddress.push(_address); emit CandidateCreate( idNumber, _age, _name, _image, candidate.voteCount, _address, _ipfs ); } //address is state variable but we want copy of that why we taking memory function getCandidate() public view returns(address[] memory){ return candidateAddress; } //gives length of candidate function getCandidateLength() public view returns(uint256){ return candidateAddress.length; } ///for getting user data function getCandidatedata(address _address) public view returns(uint,string memory, uint256,string memory,uint256,string memory,address){ return ( candidates[_address].age, candidates[_address].name, candidates[_address].candidateId, candidates[_address].image, candidates[_address].voteCount, candidates[_address].ipfs, candidates[_address]._address ); } ///-----voters section------------------------------------------------------------------------ //to create voters function voterRight(address _address,string memory _name, string memory _image,string memory _ipfs)public{ require(votingOrganizer==msg.sender,"only organizer can create voter"); _voterId.increment(); uint256 idNumber=_voterId.current(); Voter storage voter = voters[_address]; require(voter.voter_allowed==0, "user is already registered");//user not registered for contest voter.voter_allowed=1; voter.voter_name=_name; voter.voter_image=_image; voter.voter_address=_address; voter.voter_voterId=idNumber; voter.voter_vote=1000; voter.voter_voted=false; voter.voter_ipfs=_ipfs; votersAddress.push(_address); emit VoterCreated( idNumber, _name, _image, _address, voter.voter_allowed, voter.voter_voted, voter.voter_vote, _ipfs ); } function vote(address _candidateAddress,uint256 _candidateVoteId)external{ Voter storage voter = voters[msg.sender]; require(!voter.voter_voted,"you have already voted"); require(voter.voter_allowed!=0,"no right to vote"); voter.voter_voted = true; voter.voter_vote = _candidateVoteId; votedVoters.push(msg.sender); candidates[_candidateAddress].voteCount += voter.voter_allowed; } function getVoterLength() public view returns(uint256){ return votersAddress.length; } //get voters data function getVotersdata(address _address) public view returns(uint256,string memory,string memory, address,string memory,uint256,bool){ return ( voters[_address].voter_voterId, voters[_address].voter_name, voters[_address].voter_image, voters[_address].voter_address, voters[_address].voter_ipfs, voters[_address].voter_allowed, voters[_address].voter_voted ); } //list of who has voted function getVotedVoterList() public view returns(address[] memory){ return votedVoters; } //list of all voters function getVoterList() public view returns(address[] memory){ return votersAddress; } }
内容的提问来源于stack exchange,提问作者sahil shaikh
相关产品推荐
相关产品推荐

