-1

I asked a stupid question, sorry guys! :(

I used remit to deploy this contract on the test network successfully. It can be executed at the beginning, but will return error after I executed this contract times.

this is my contract on ROPSTEN : https://ropsten.etherscan.io/address/0x6191930e3cd4e48bbc6c0385f48325d26bc1d6a5

Is there any problem with my code?

pragma solidity^0.4.24;

contract Dicegame {

    address owner =  0x62a0bbA644863c09cB97Bda35b946c27C359A8F3;

    mapping(address => string)resultt;
    mapping(address => uint)randnumber;
    mapping(address => uint)winmoneyy;

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function payforstartbet(uint _undernumber,string _name) public payable {

        require(msg.value < 100000000000000000000);
        require(msg.value > 100000000000000000);
        require(_undernumber <= 98);
        require(_undernumber >= 1);

        string memory betresult;
        uint winmoney;
        uint ownerget;
        uint randnum = uint(keccak256(abi.encodePacked(now,msg.sender,_name,blockhash(block.number-1),block.difficulty))) % 100; 
        uint winchance = _undernumber;

        if(_undernumber > randnum){
            betresult = "win!";
        } else {
            betresult = "lose!";
        }

        if(keccak256(abi.encodePacked(betresult)) == keccak256(abi.encodePacked("win!"))) {
            winmoney = msg.value + ((msg.value * 100 / winchance) - msg.value) * 24 / 25 ;
            ownerget = ((msg.value * 100 / winchance) - msg.value) * 1 / 25 ;
            msg.sender.transfer(winmoney);
            owner.transfer(ownerget);
        } else {
            winmoney = 0;
            ownerget = 0;
        }

        resultt[msg.sender] = betresult;
        randnumber[msg.sender] =  randnum;
        winmoneyy[msg.sender] = winmoney;

    }

    function withdraw() external onlyOwner {
        owner.transfer(this.balance);
    }
    function winorlose() external view returns(string) {
        return resultt[msg.sender];
    }
    function showtheend() external view returns(uint, uint) {
        return (randnumber[msg.sender],winmoneyy[msg.sender]);
    }

}
Achala Dissanayake
  • 5,819
  • 15
  • 28
  • 38
asd54578
  • 3
  • 2

1 Answers1

0

Looking at the block explorer your transactions appear to be going through correctly. They fail when you send 0.1 ETH or less, because that's the minimum you set with require(msg.value > 100000000000000000);.

Note that your contract is insecure: You make your "random" number based on

  • now
  • msg.sender
  • _name
  • blockhash(block.number-1)
  • block.difficulty

...but all of these are knowable to any user before they send the except now, and that is known to the miner, but also to any contract, which can check it to see if it would win the bet and only send the bet if it does. It could also keep trying different values of name until it found one that won the bet when used in combination with the rest of the parameters, then send that.

See this answer for more information on randomness in contracts: How can I securely generate a random number in my smart contract?

Edmund Edgar
  • 16,897
  • 1
  • 29
  • 58
  • I asked a stupid question.....thank you. I tried to use oracle to get random numbers, but after the deployment, I can only transfer the transaction once, and the second time will fail. I can't find the reason. Later I have to use the less secure way. – asd54578 Aug 25 '18 at 12:28