ethereum(以太坊)(十二)--应用(二)__投票(基础总和)
编写应用合约之前,先弄清它的逻辑,有助于我们更好的部署合约

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(以太坊)(十二)--应用(二)__投票(基础总和)的更多相关文章
- ethereum(以太坊)(十二)--应用(一)__集资(构造函数/映射)
pragma solidity ^0.4.4; contract funder{ //0xca35b7d915458ef540ade6068dfe2f44e8fa733c //0x14723a09ac ...
- ethereum(以太坊)(十一)--字节数组(二)
pragma solidity ^0.4.0; contract test { uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 ...
- ethereum(以太坊)(十四)--Delete
pragma solidity ^0.4.10; contract Delete{ /* delete可用于任何变量(除mapping),将其设置成默认值 bytes/string:删除所有元素,其长 ...
- ethereum(以太坊)(十)--函数修饰符
pragma solidity ^0.4.0; contract modifierTest{ uint public v1; uint constant v2 =10; //uint constant ...
- 创建自己的加密货币MNC——以太坊代币(二)
创建一个基于以太坊平台的分红币MNC,根据持有的代币数量,进行分红的算法.github地址: https://github.com/lxr1907/MNC 1.使用以太坊根据比例换购token MNC ...
- 以太坊开发教程(二) 利用truffle发布宠物商店 DAPP 到 以太坊测试环境Ropsten
1.环境安装 1) node安装 设置镜像地址: curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -下载安装 ...
- ethereum(以太坊)(一)
从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...
- ethereum(以太坊)(基础)--容易忽略的坑(二)
pragma solidity ^0.4.0; contract EMath{ string public _a="lin"; function f() public{ modif ...
- ethereum(以太坊)(二)--合约中属性和行为的访问权限
pragma solidity ^0.4.0; contract Test{ /* 属性的访问权限 priveta public internal defualt internal interlnal ...
随机推荐
- IntelliJ IDEA实时模板变量
返回由当前方法返回的值的类型IntelliJ IDEA 实时模板中的模板变量允许用户输入.扩展模板后,变量将作为输入字段显示在编辑器中. IntelliJ IDEA 声明实时模板变量 模板中的变量以下 ...
- react-router + redux + react-redux 的例子与分析
一个 react-router + redux + react-redux 的例子与分析 index.js import React from 'react' import ReactDom fr ...
- js-对象的方法详解
Object.prototype 上的方法: constructor 返回创建该对象的构造函数 var arr = []; arr.constructor == function Array() { ...
- vue 实现二选一列表
<template> <div> <ul> <li :class="{active:classIndex==classNum}" clas ...
- 使用ionic cordova build android --release --prod命令打包报错解决方法
使用ionic cordova build android --release --prod命令打包报有如下错误及解决方法 只要把以下内容添加到build-extras.gradle或(build** ...
- 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification
这个例子介绍了如何在Service中使用Notification,相关的类为NotifyingController和NotifyingService. 在Service中使用Notification的 ...
- KinSlideshow焦点图轮播插件
KinSlideshow默认设置效果代码: *焦点图显示的标题为 img 中 alt 属性中的文字 *当只有一张图片时不显示按钮,但也会有无缝切换效果 * jquery ..以上版本 jvascrip ...
- Azure 进阶攻略 | 上云后的系统,「门禁」制度又该如何实现?
各位办公室白领们,不妨回想一下自己每天去公司上班时的一些细节. 为避免「闲杂人等」进入工作场所,我们需要证明自己是这家公司的员工才能进入,对吧!所有员工,无论所属部门或职位,都必须先证明自己身份,例如 ...
- 建立本地yum源
使用环境 服务器处于内网,需要更新 网络资源紧张,节约带宽 建立yum目录 mkdir -p /opt/opmgmt/yum rsync服务器列表 centos mirrors epel mirror ...
- ansible使用2-命令
并发与shell # bruce用户身份,-m指定模块名称,默认模块名command,all所有目标主机,也可以指定组名或者主机名 ansible all -m ping -u bruce # bru ...