以太坊之dapp例子

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

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

你可能感兴趣的:(区块链,以太坊)