Smart-Contract-Vulnerabilities-DB / Base Sample / weak_rand / weak_rand_2.sol
weak_rand_2.sol
Raw
// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

contract applications{

    address public owner;
    uint public pot = 0;
    uint public lucky_number = 0;
    uint private fee = 0;
    bool public game_open = false;

    uint256[] seeBets;

    constructor(){
        owner = msg.sender;
    }

    function start_game() public payable{
        require(msg.sender == owner);
        require(msg.value > 0);

        pot = msg.value;
        lucky_number = uint(keccak256(abi.encode(block.timestamp)));
        game_open = true;
    }

    function bet() public payable returns(uint256[] memory){
        require(game_open == true, "Sorry! This game have not yet started!");
        require(msg.value >= 5 wei, "Value too to to purchase a ticket!");

        uint value = msg.value;
        fee = msg.value;

        while(value > 5){
            uint ticket = uint(keccak256(abi.encode(block.timestamp)));
            seeBets.push(ticket);
            if (ticket == lucky_number){
                payable(msg.sender).transfer(pot);
                pot = 0;
                game_open = false;
                break;
            }

            value -= 5;
        }

        if(value > 5){
            payable(msg.sender).transfer(value);
        }

        return seeBets;
    }

    function collect_fee() public {
        require(msg.sender == owner);

        payable(msg.sender).transfer(fee);
        fee = 0;
    }
}