智能合约案例|简单代币合约、投票合约、简单投票合约

智能合约案例

简单代币合约

//SPDX-License-Identifier: SimPL-2.0
pragma solidity >0.4.20;

contract Coin {
    address public minter;
    mapping (address=>uint) public balance;
    event Sent(address from, address to, uint amount);
    
    constructor() public {
        minter = msg.sender;
    }
    
    // minter 给 user 铸币
    function mint(address user, uint amount) public {
        require (msg.sender == minter);
        balance[user] += amount;
    }
    
    // minter 给 receiver 发币
    function send(address receiver, uint amount) public {
        require (balance[msg.sender] >= amount);
        balance[msg.sender] -= amount;
        balance[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }
}

投票合约

//SPDX-License-Identifier: SimPL-2.0
pragma solidity >0.4.20;
contract Ballot {
    // 投票人
    struct Voter {
        uint weight;
        bool isVoted;
        uint vote;
        address delegate;
    }
    // 提案
    struct Proposal {
        bytes32 name;
        uint voteCnt;
    }
    address public chairPerson; // 主持人
    mapping(address=>Voter) voters; // 投票人列表
    Proposal[] public proposals; // 提案数组

    // 初始化合约
    constructor(bytes32[] memory _proposals) {
        chairPerson = msg.sender; // 将第一个部署合约的人设置为主持人
        voters[chairPerson].weight = 1;

        // 提案数组存储
        for (uint i = 0; i < _proposals.length; i++) {
            proposals.push(Proposal({
                name: _proposals[i],
                voteCnt: 0
            }));
        }
    }

    // 主持人分发选票
    function giveRightToVote(address voter) public {
        require(chairPerson == msg.sender);
        require(!voters[voter].isVoted);
        require(voters[voter].weight == 0);
        voters[voter].weight = 1;
    }

    // 将选票委托给他人投票
    function delegate(address to) public {
        Voter storage sender = voters[msg.sender];
        require(!sender.isVoted);
        while(voters[to].delegate != address(0) && 
                voters[to].delegate != msg.sender) {
            to = voters[to].delegate;
        }
        require(to != msg.sender);
        sender.isVoted = true;
        sender.delegate = to;

        Voter storage delegateTo = voters[to];
        if (delegateTo.isVoted) {
            proposals[delegateTo.vote].voteCnt += sender.weight;
        } else {
            delegateTo.weight += sender.weight;
        }
    }

    // 投票
    function vote(uint proposal) public{
        Voter storage sender = voters[msg.sender];
        require(!sender.isVoted);
        sender.isVoted = true;
        sender.vote = proposal;
        proposals[proposal].voteCnt += sender.weight;
    }

    // 找到得票最高的提案
    function winningProposal() public view returns(uint _winning) {
        uint winningCnt = 0;
        for (uint8 i = 0; i < proposals.length; i++) {
            if (winningCnt < proposals[i].voteCnt) {
                winningCnt = proposals[i].voteCnt;
                _winning = winningCnt;
            }
        }
    }
}

简单投票合约

//SPDX-License-Identifier: SimPL-2.0

pragma solidity >0.4.20;

contract Voting {
    bytes32[]  public candidateList;
    mapping(bytes32 => uint8) votesReceived;
    
    constructor(bytes32[] memory names) public {
        //candidateList = candidateListName;
        //candidateList =[bytes32("Alice"),"Bob","Cary"];
        candidateList = names;
    }
    
    function isCandidate(bytes32 candidateName) internal view returns(bool) {
        for (uint8 i = 0; i < candidateList.length; i++) {
            if(candidateName == candidateList[i]) {
                return true;
            }
        }
        return false;
    }
    
    function vote(bytes32 candidateName) public {
        require(isCandidate(candidateName));
        
        votesReceived[candidateName] += 1;
    }
    
    function getVotes(bytes32 candidateName) public view returns(uint8) {
        require(isCandidate(candidateName));
        return votesReceived[candidateName];
    }
} 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值