第52篇 笔记-彩票类智能合约示例

本文所有智能合约源码来自网络。

1.lottery01.sol

特点:限定人数,随机数与合约拥有者无关;

合约编译通过:

pragma solidity ^0.4.23;

contract Lottery10Users {
    address[10] participants;        //限制为10个用户
    uint8 participantsCount = 0;     //本轮已参与用户数
    uint  randNonce = 0;             

    function join() public payable {
	    //用户必须支付 0.1 ETH 才能加入游戏
        require(msg.value == 0.1 ether, "Must send 0.1 ether");
        require(participantsCount < 10, "User limit reached");
        require(joinedAlready(msg.sender) == false, "User already joined");
		
        participants[participantsCount] = msg.sender;
        
		//第10个用户进入后,选择获胜者
		participantsCount++;
		if (participantsCount == 10) {
            selectWinner();
        }
    }
    
	//查询用户加入游戏状态,同一用户每轮只能加入一次
    function joinedAlready(address _participant) private view returns(bool) {
        bool

猜你喜欢

转载自blog.csdn.net/wonderBlock/article/details/113819459
今日推荐