以太坊之dapp例子

以太坊之dapp例子,第1张

此处是上篇博文中的完整以太坊智能合约

pragma solidity >=0.4.22<0.8.0;
contract Voting{
    bytes32[] public candidateList;
    mapping(bytes32 => uint8) public votesRece;
    constructor(bytes32[] memory candidateListName) public{
        candidateList = candidateListName;
    }
//检查候选人
    function hefacandidate(bytes32 candidateName) internal view returns (bool){
        for(uint8 i =0;i<candidateList.length;i++){
            if(candidateName ==candidateList[i]){
                return true;
            }
        }
        return false;
    }

    function voteForCandidate (bytes32 candidateName)public{
        require (hefacandidate(candidateName));
        votesRece[candidateName] +=1;
    }
    function LookForCandidate (bytes32 candidateName) view public returns(uint8){
        require (hefacandidate(candidateName));
        return votesRece[candidateName];
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/1324179.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存