以太坊 猜单双游戏

猜单双,赔率1:1

pragma solidity 0.5.16;


library GuessOracle{

    function oracle() external returns(uint){
        return uint(1);
    }
}
pragma solidity 0.5.16;

import "./GuessOracle.sol";

contract GuessParity{

    address private fundPoolAddress;    //平台资金地址
    uint private periods;               //期数
    struct Guess{
        address player;     //玩家地址
        uint amount;        //投注金额
        uint number;        //玩家预言的数字, 0:单, 1:双
        bool isSucc;        //是否预言成功

    }

    constructor(address _fundPoolAddress) public{
        fundPoolAddress = _fundPoolAddress;
    }

    mapping (uint => Guess) public guessList;//玩家预言历史记录
    mapping (uint => uint) public oracleList;//往期记录

    //玩家投递预言
    function prophecy(uint _num) public payable{
        uint _periods = periods;
        uint newOracle = GuessOracle.oracle();

        oracleList[_periods] = newOracle;

        if(_num == newOracle){
            //预言命中
            
            require(address(this).balance > (msg.value * 2), "余额不足");
            
            guessList[_periods] = Guess({player:msg.sender, amount:msg.value, number:_num, isSucc:true});
            //给用户转账,金额为用户发送额*2
            msg.sender.transfer(msg.value * 2);

        }else{
            //没有命中,
            guessList[_periods] = Guess({player:msg.sender, amount:msg.value, number:_num, isSucc:false});
        }
        periods++;
    }


    function getFundPoolAddress() public view returns(address){
        return fundPoolAddress;
    }
    
    function getBalance() public view returns(uint){
        return address(this).balance;
    }

}

你可能感兴趣的:(区块链)