контракты простых лотереи на Etherium/Solidity

популярно эт гавно в последнее время, чет. и раз уж за него сиравно не заплатили. пусть буит тут. пользуйте если кому надо.

 

pragma solidity ^0.4.17;

contract Lottery{
    address public manager;
    address[] public players;
    address[] public winners;
    uint pot;


    constructor () public {
        manager = msg.sender;
    }

    function enter() public payable{
        require(msg.value > .01 ether);
        pot+=msg.value;
        players.push(msg.sender);
    }

     function random(uint seed) public view returns (uint){
        uint arg=  uint(blockhash(block.number-1)) & 0xfff;
        return uint( arg | now) + seed;
    }

    function pickWinner() public payable {
        uint winningAmount = pot/10;
        address winner;
        uint i=0;
        //delete new_players;
        
       
        
        while(i < players.length){
            winner = players[random(i+1) % players.length];
            
            for(uint j=0; j< winners.length ;j++)
            {
                if(winner == winners[j] )
                {
                    continue;
                }
            }
            
            winner.transfer(winningAmount); 
            winners.push(players[i]);
            i=i+1;
            return;
        } 
    }
    

    modifier restricted() {
        require(msg.sender == manager);
        _;
    }

    function getPlayers() public view returns(address[]) {
        return players;
    }
}

pragma solidity ^0.4.0;

contract Lottery {
    address owner = 0x000000000000000000000000004510471 ;
    uint public pot;
    uint public winningsLimit=10000000000000;
    uint public participantLimit=5;
    address[] public participants;
    uint[] public participants_cash;
    uint starttime;
    uint gameTime=20;

    event Payout(address target, uint amount, uint nrOfParticipants);
    
    modifier onlyBy(address _account)
    {
        require(msg.sender == _account);
        _;
    } 
    
    function Lottery(uint _winningsLimit, uint _participantLimit)payable public onlyBy(owner)
    {
        owner = msg.sender;
        winningsLimit = _winningsLimit;
        participantLimit = _participantLimit;
	starttime = now;
    }
    
    function () payable public 
    {
        pot += msg.value;
        participants.push(msg.sender);
        participants_cash.push(msg.value);
        if (this.balance > winningsLimit || participants.length > participantLimit || (now >= starttime + gameTime * 1 seconds) ) 
        {
            terminate();
            starttime = now;
        }
    }
    
    function terminate() private 
    {
        uint totalPayout = pot;
        uint ownerFee = totalPayout / 20;
        uint payoutToWinner = totalPayout - ownerFee;
        int winnerHeight = int(uint(block.blockhash(block.number-1)) % uint(pot));
        uint i = 0;
        address winner = participants[i];
        pot = 0;
        while(winnerHeight >= 0){
            winnerHeight = winnerHeight - int(participants_cash[i]);
            winner = participants[i];
            i++;
        } 
        winner.transfer(payoutToWinner);
        owner.transfer(ownerFee);
        Payout(winner, pot, participants.length);
        delete participants;
        delete participants_cash;
    }
    
    function murder() public onlyBy(owner) 
    {
        if (msg.sender == owner) 
        {
            terminate();
            selfdestruct(owner);
        }
    }
}

 

Добавить комментарий