美文网首页
投票Demo

投票Demo

作者: 蜗牛也有梦想 | 来源:发表于2019-04-28 17:13 被阅读0次

pragma solidity ^0.4.22;

contract Ballot{

// 这里声明了一个新的复合类型用于稍后的变量
// 它用来表示一个选民
struct Voter{
    uint weight;// 计票的权重
    bool voted;
    address delegate;// 被委托人
    uint vote;// 投票提案的索引
    
}

// 提案的类型
struct Proposal{
    bytes32 name;// 简称(最长32个字节)
    uint voteCount;// 得票数
}

address public chairperson;

// 这声明了一个状态变量,为每个可能的地址存储一个 `Voter`。
mapping(address => Voter) public voters;

// 一个 `Proposal` 结构类型的动态数组
Proposal[] public proposals;

constructor(bytes32[] proposalNames) public{
    chairperson = msg.sender;
    voters[chairperson].weight = 1;
    for(uint i = 0; i < proposalNames.length; i++){
        proposals.push(Proposal({ name:proposalNames[i],voteCount:0 }));
    }
}

function giveRightToVote(address voter) public{
    
    require(msg.sender == chairperson,"Only chairperson can give right to vote.");
    
    require(!voters[voter].voted,"The voter already voted.");
    
    require(voters[voter].weight == 0,"you do not have vote weight");
    
    voters[voter].weight = 1;
}

function delegate(address to) public{
    Voter storage sender = voters[msg.sender];
    require(!sender.voted,"The voter already voted.");
    require(to != msg.sender,"Self-delegation is disallowed.");
    
    while(voters[to].delegate != address(0)){
        to = voters[to].delegate;
        require(to != msg.sender, "Found loop in delegation.");
    }
    
    sender.voted = true;
    sender.delegate = to;
    Voter storage delegate_ = voters[to];
    if(delegate_.voted){
        proposals[delegate_.vote].voteCount += sender.weight;
    }else{
        delegate_.weight += sender.weight;
    }
}

function vote(uint proposal) public{
    Voter storage sender = voters[msg.sender];
    require(!sender.voted,"Already voted.");
    sender.voted = true;
    sender.vote = proposal;
    proposals[proposal].voteCount += sender.weight;
}

function winningProposal() public view returns(uint winningProposal_){
    uint winningVoteCount = 0;
    for(uint p = 0; p < proposals.length; p++){
        winningVoteCount = proposals[p].voteCount;
        winningProposal_ = p;
    }
}

function winnerName() public view returns(bytes32 winnerName_){
    winnerName_ = proposals[winningProposal()].name;
}

}

相关文章

  • 投票Demo

    pragma solidity ^0.4.22; contract Ballot{ }

  • 纯js实现双向数据绑定

    需求 对于投票页的选择与取消类似于外卖订单中的选择与取消 前提 本demo没有用到MVC框架,纯js完成。 开发 ...

  • 投票投票

    今年四月份,先生在妇联顾主席的推荐下,用我写的一篇旧文《陪伴是最长情的告白》,参加省妇联开展的“我爱我家 同悦书香...

  • 投票,投票

    网络多媒体平台的发展,催生的各类评选,都借助网络媒体来增加知名度,大到全球知名企业品牌,小到幼儿园,都在利用互联网...

  • EOS初探(3) - 智能合约投票

    本章节利用EOS的智能合约实现了简单的投票,功能包括 创建投票 发起投票 投票/委托投票 投票的winner提案 ...

  • [源码和文档分享]基于JSP实现的在线投票系统

    一、设计方案 1.1 投票系统的功能组成 投票系统功能有:选择投票和个人操作及设置,投票设置,投票结果分析,投票操...

  • [源码和文档分享]基于JSP实现的在线投票系统

    一、设计方案 1.1 投票系统的功能组成 投票系统功能有:选择投票和个人操作及设置,投票设置,投票结果分析,投票操...

  • 吴猛强:坚持真理有多难(新《西游记》89)

    (1) 来!! 各位小子们!! 投票啦!! 投票啦!! 投票啦!!!! 投票啦!!!! . (2) 来来!! 你们...

  • 投票管理页面的详细分析

    投票页面简要概述 新建投票页面包括投票发起人、投票主题、参与人数、截止时间、投票选项。(此外还可以包括投票参与人)...

  • 区块链 智能合约 投票应用

    智能合约实现了一个自动化且透明的投票应用。 投票发起人可以发起投票,将投票权赋予投票人;投票人可以自己投票,或将自...

网友评论

      本文标题:投票Demo

      本文链接:https://www.haomeiwen.com/subject/dgjjnqtx.html