首先,我们在合约中添加winningProposal
函数。此函数将遍历所有提案并返回票数最多的提案。此外,我们将创建一个winnerName
函数,用于返回获胜提案的名称。合约的最终版本如下:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Voter {
struct Person {
bool voted;
uint vote;
}
struct Proposal {
string name;
uint voteCount;
}
Proposal[] public proposals;
mapping(address => Person) public voters;
function registerVoter() public {
voters[msg.sender].voted = false;
}
function addProposal(string memory _name) public {
proposals.push(Proposal(_name, 0));
}
function vote(uint _proposal) public {
require(_proposal < proposals.length, "Invalid proposal index."); // This is the added check
Person storage sender = voters[msg.sender];
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = _proposal;
proposals[_proposal].voteCount += 1;
}
function winningProposal() public view returns (uint winningProposal_) {
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
}
}
}
function winnerName() public view returns (string memory winnerName_) {
winnerName_ = proposals[winningProposal()].name;
}
}
我们来解释一下新添加的两个函数:
Winning Proposal函数:此函数将遍历所有提案并找到投票数最高的那个。它返回该提案在proposals
数组中的索引。此函数是一个view
函数,只读取数据,不修改合约状态。
Winner Name函数:此函数会调用winningProposal
函数获取获胜提案的索引,然后返回获胜提案的名称。
单击左侧边栏上的Solidity编译器图标,然后单击“Compile”按钮,来编译您的合约。
在“Deploy & Run Transactions”选项卡中部署并运行合约。注册投票人,添加提案,进行投票,最后查看获胜者。
在下一章中,我们将探讨合约交互与事件记录。我们将学习不同的合约如何交互,以及如何在区块链上记录和监控活动。恭喜大家在以太坊上构建了自己的第一个去中心化投票系统!
首先,我们在合约中添加winningProposal
函数。此函数将遍历所有提案并返回票数最多的提案。此外,我们将创建一个winnerName
函数,用于返回获胜提案的名称。合约的最终版本如下:
Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Voter {
struct Person {
bool voted;
uint vote;
}
struct Proposal {
string name;
uint voteCount;
}
Proposal[] public proposals;
mapping(address => Person) public voters;
function registerVoter() public {
voters[msg.sender].voted = false;
}
function addProposal(string memory _name) public {
proposals.push(Proposal(_name, 0));
}
function vote(uint _proposal) public {
require(_proposal < proposals.length, "Invalid proposal index."); // This is the added check
Person storage sender = voters[msg.sender];
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = _proposal;
proposals[_proposal].voteCount += 1;
}
function winningProposal() public view returns (uint winningProposal_) {
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
}
}
}
function winnerName() public view returns (string memory winnerName_) {
winnerName_ = proposals[winningProposal()].name;
}
}
我们来解释一下新添加的两个函数:
Winning Proposal函数:此函数将遍历所有提案并找到投票数最高的那个。它返回该提案在proposals
数组中的索引。此函数是一个view
函数,只读取数据,不修改合约状态。
Winner Name函数:此函数会调用winningProposal
函数获取获胜提案的索引,然后返回获胜提案的名称。
单击左侧边栏上的Solidity编译器图标,然后单击“Compile”按钮,来编译您的合约。
在“Deploy & Run Transactions”选项卡中部署并运行合约。注册投票人,添加提案,进行投票,最后查看获胜者。
在下一章中,我们将探讨合约交互与事件记录。我们将学习不同的合约如何交互,以及如何在区块链上记录和监控活动。恭喜大家在以太坊上构建了自己的第一个去中心化投票系统!