编写应用合约之前,先弄清它的逻辑,有助于我们更好的部署合约 

pragma solidity ^0.4.21;
pragma experimental ABIEncoderV2; contract voter1{
//voter candidate //["eilinge", "lin" ,"meimei"] //admin 0x4B0897b0513fdC7C541B6d9D7E929C4e5364D2dB //0x14723a09acff6d2a60dcdf7aa4aff308fddc160c 2
//0x583031d1113ad414f02576bd6afabfb302140225 1
//0xdd870fa1b7c4700f2bd7f44238821c26f7392148 0
//投票人
struct Voter{
uint _voterAddr;
uint _weight;
bool _voted;
address delegate;
}
//候选人
struct Candidate{
string candidateName;
uint voterNum;
} address public admin;
//候选人集合
Candidate[] public candidates;
//投票人集合
mapping(address=>Voter) public voters;
//添加候选人
constructor(string[] candidateNames)public{
admin = msg.sender; for(uint i;i<candidateNames.length;i++){
Candidate memory canNum = Candidate(candidateNames[i],0);
candidates.push(canNum);
}
} modifier OnlyAdmin(){
require(admin == msg.sender);
_;
}
//赋予投票权
function giveVoteRight(address addr) OnlyAdmin() public{
if (voters[addr]._weight > 0){
revert();
}
voters[addr]._weight = 1;
} //进行投票
function vote(uint voteNum) public{ Voter storage voter = voters[msg.sender]; if(voter._weight <= 0 || voter._voted ==true){
revert();
} voter._voted = true;
voter._voterAddr = voteNum; candidates[voteNum].voterNum += voter._weight;
} //设置代理人
function delegateFunc(address to) public{
Voter storage voter = voters[msg.sender]; if(voter._weight <= 0 || voter._voted ==true){
revert();
} //设置代理人的地址不为空,且不能是自己
while (voters[to].delegate != address(0) && voters[to].delegate !=msg.sender){
to = voters[to].delegate;//新代理人地址
} //代理人的地址不能是自己
require(msg.sender != to); voter._voted = true;
voter.delegate = to; //代理人
Voter storage finalDelegateVoter = voters[to];
//代理人投过票,则在代理人投票的候选人票数上加上自己的权重
if(finalDelegateVoter._voted){
candidates[finalDelegateVoter._voterAddr].voterNum += voter._weight;
}
//代理人未投过票,则在代理人权重加上自己的权重
else{
finalDelegateVoter._weight += voter._weight;
}
}
//查看谁胜出
function whoWin() public constant returns(string,uint){
string winner;
uint winnerVoteCounts; for (uint i;i>candidates.length;i++){
if (candidates[i].voterNum > winnerVoteCounts){
winnerVoteCounts = candidates[i].voterNum;
winner = candidates[i].candidateName;
}
}
return(winner,winnerVoteCounts);
}
}

最新文章

  1. 【.NET MF】.NET Micro Framework USB移植
  2. 64位下pwntools中dynELF函数的使用
  3. Notepad++ HTML格式化
  4. Java学习记录-注解
  5. 编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,最后再在主类E 的main方法中创建Print的对象并赋值,运行方法
  6. 基于jQuery的网站首页宽屏焦点图幻灯片
  7. [转]Win7下Eclipse中文字体太小
  8. 应用程序打包(ipa)
  9. sharePoint常用命令
  10. HTTPS那个东西(一)-HTTPS原理
  11. BZOJ3963: [WF2011]MachineWorks
  12. 关于xmlHttp.status最新统计
  13. socket端口外网无法连接解决方法
  14. 使用VIM将文件的其中的连续几行注释删除或者给其中的连续几行添加注释
  15. Echarts图表legend的排版问题(转载)
  16. python中经典类和新式类的区别
  17. Nginx+Keepalived部署
  18. Swift5 语言指南(五) 基本运算符
  19. BETA 版冲刺前准备
  20. Hiero_FnNukeShotExporter的解析与修改

热门文章

  1. db2-表处于暂挂状态
  2. Bootstrap学习笔记(四)
  3. java.lang.OutOfMemoryError: Failed to allocate a 3110419 byte allocation with 741152 free bytes and
  4. android ContentProvider共享数据
  5. CloudWAN
  6. linux基础命令-chgrp/chown/chomd
  7. 笨办法学Python(三十四)
  8. 概念:静态static相关知识
  9. Oracle编程入门经典 第11章 过程、函数和程序包
  10. Android 中间白色渐变到看不见的线的Drawable