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

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);
}
}

ethereum(以太坊)(十二)--应用(二)__投票(基础总和)的更多相关文章

  1. ethereum(以太坊)(十二)--应用(一)__集资(构造函数/映射)

    pragma solidity ^0.4.4; contract funder{ //0xca35b7d915458ef540ade6068dfe2f44e8fa733c //0x14723a09ac ...

  2. ethereum(以太坊)(十一)--字节数组(二)

    pragma solidity ^0.4.0; contract test { uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 ...

  3. ethereum(以太坊)(十四)--Delete

    pragma solidity ^0.4.10; contract Delete{ /* delete可用于任何变量(除mapping),将其设置成默认值 bytes/string:删除所有元素,其长 ...

  4. ethereum(以太坊)(十)--函数修饰符

    pragma solidity ^0.4.0; contract modifierTest{ uint public v1; uint constant v2 =10; //uint constant ...

  5. 创建自己的加密货币MNC——以太坊代币(二)

    创建一个基于以太坊平台的分红币MNC,根据持有的代币数量,进行分红的算法.github地址: https://github.com/lxr1907/MNC 1.使用以太坊根据比例换购token MNC ...

  6. 以太坊开发教程(二) 利用truffle发布宠物商店 DAPP 到 以太坊测试环境Ropsten

    1.环境安装 1) node安装 设置镜像地址: curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -下载安装 ...

  7. ethereum(以太坊)(一)

    从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...

  8. ethereum(以太坊)(基础)--容易忽略的坑(二)

    pragma solidity ^0.4.0; contract EMath{ string public _a="lin"; function f() public{ modif ...

  9. ethereum(以太坊)(二)--合约中属性和行为的访问权限

    pragma solidity ^0.4.0; contract Test{ /* 属性的访问权限 priveta public internal defualt internal interlnal ...

随机推荐

  1. js面向对象之属性

    1.属性的设置和获取,方式有两种:   .和[ ]   .是取自身属性   [ ]可以是变量 var obj={}; obj.name="sonia"; obj['age']=22 ...

  2. 前端性能优化-keep-alive

    什么是Keep-Alive Keep-Alive是浏览器端和服务器端约定的一种提高传输效率的协议.我先举个例子吧,我现在搬家,有10个箱子,如果我自己来搬的话,每次只能带一个箱子,那么搬到目的地,需要 ...

  3. jQuery UI dialog 隐藏默认关闭按钮

    var O_dialog = $("#dialog-modal"); O_dialog.dialog({ closeOnEscape: false, width: 250, hei ...

  4. EasyUI 数据网格 - 设置排序并自定义排序

    数据网格(DataGrid)的所有列可以通过点击列表头来排序.您可以定义哪列可以排序.默认的,列是不能排序的,除非您设置 sortable 属性为 true. 当排序时,数据网格(DataGrid)将 ...

  5. idea 插件描述(转)

    提供些idea插件的干货.干货.干货! 如何安装idea plug,方法如下图:如果安装不了考虑下是否代理问题: 1,maven helper 以前查看maven依赖都比较麻烦,需要用命令maven ...

  6. 《ArcGIS Runtime SDK for Android开发笔记》——问题集:使用TextSymbol做标注显示乱码

    1.前言 在14年的时候写过一篇博客关于ArcGIS for Android 10.1.1API 中文标注导致程序异常崩溃问题,但是当时并没有很好的解决这样一个问题,也并没有深入研究分析这样的一个异常 ...

  7. react-native与原生界面相互跳转

    一.添加MyIntentModule类,并继承ReactContextBaseJavaModule实现其方法和构造函数.在该类中添加方法,注意:方法头要加@ReactMethod public cla ...

  8. python 生成随机图片验证码

    1.安装pillow模块 pip install pillow (1)创建图片 from PIL import Image #定义使用Image类实例化一个长为400px,宽为400px,基于RGB的 ...

  9. IDEA下通过Git实现代码管理

    IDEA下通过Git实现代码管理 1.介绍 1.1 Git概述 Git是类似于SVN等代码管理软件,使用分布式技术实现.Github是互联网代码仓库,每个人可以在上面创建自己的仓库,使用git完成同g ...

  10. 如何处理Eclipse错误消息 The declared package does not match the expected package

    我从github下载了一个开源项目后,导入到自己Eclipse之后,遇到了这个烦人的错误消息: The declared package "com.sap.smartService" ...