问题描述
When the electoral process is over,how can the Voter verify that the candidate’s choice has not changed? 我想在合同中添加一个函数,但是我不知道如何构造这个函数 I want an example when the Voter puts his address,the system returns the name of the candidate who has been chosen since the election process. 我需要智能合约中的一个函数来做到这一点
pragma solidity ^0.6.0;
contract Voting {
// Model a Candidate
struct Candidate {
uint256 id;
string name;
uint256 VoteCount;
}
mapping(address => bool) public Voters;
mapping(uint256 => Candidate) public candidates;
// Store Candidates count
uint256 public candidatesCount;
// Voted event
event VotedEvent(uint256 indexed _candidateId);
constructor() public {
addCandidate("Candidate #1");
addCandidate("Candidate #2");
addCandidate("Candidate #3");
}
function addCandidate(string memory _name) private {
candidatesCount++;
candidates[candidatesCount] = Candidate(candidatesCount,_name,0);
}
function Vote(uint256 _candidateId) public {
// require that they haven't Voted before
require(!Voters[msg.sender]);
// require a valid candidate
require(_candidateId > 0 && _candidateId <= candidatesCount);
// record that Voter has Voted
Voters[msg.sender] = true;
// update candidate Vote Count
candidates[_candidateId].VoteCount++;
// trigger Voted event
emit VotedEvent(_candidateId);
}
function getCondidateName(address adr) pure public returns( string memory ){
// here i need to create thecode of the verify }}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)