Solidity - Creating a slot machine (Odds game) using Smart Contract in Part 1.

in #dunkpay6 years ago

Screen Shot 2018-06-06 at 10.33.13 PM.png

First, the result screen is as follows.

Screen Shot 2018-06-06 at 10.34.30 PM.png

Clicking on the betting button will launch a metamask and tell you whether or not to win.

You can actually test it here :

https://www.dunkpay.com/slot-test/

The smart contract source for slot machine games is very simple.

https://ropsten.etherscan.io/address/0x2f9a2b2977ae4557a8d7d545c48d4c0b67dc3ae7#code

pragma solidity ^0.4.8;

contract SlotMachine {
    
    mapping (address => uint) public playerList; 
    uint256 public contractBalance;
    
    function SlotMachine() public {
    }
    
    function () payable public {
        start();
    }
    
    function start() public payable {
        
        uint256 userBalance = msg.value;
        require(userBalance > 0);
        uint randomValue = random();
        playerList[msg.sender] = randomValue;
        contractBalance = address(this).balance;
            
        if(randomValue > 50)
        {    
            uint256 winBalance = userBalance * 2;
            if(contractBalance < winBalance){
                winBalance = contractBalance;
            }
            msg.sender.transfer(winBalance); 
            contractBalance = address(this).balance;        
        }
    }
    
    function random() view returns (uint8) {
        return uint8(uint256(keccak256(block.timestamp)) % 100) + 1; // 1 ~ 100 (Only for testing.)
    }
}

In the first line, an anonymous function is a function that is executed when Ethereum is deposited.

payable is a function that can receive an ethereum.

public is a function that can be executed externally.

The public variables playerList and contractBalance are variables that can be read from outside.

The random () function is a function that divides the current timestamp by 100.

That is, when the user deposits ETH into this contract, the start () function is called.

And then, The random () function will call to generate a random number between 1 and 100.

If the value is greater than 50, the amount you bet on the contract will double for the withdrawal.

Client-side is simpler.

The client simply ask deposits to the contract.

When client make a deposit a ETH, it will generate a hash value.

Later, the client reads the contract and checks to see if it is win or lose.

Is there any problem with this source?

The random () function has a big security issue.

The advantages or disadvantages of smart contracts are revealed.

Anyone can see the principle of the random () function.

This example uses the value of timestamp, but it is the same whether you use the value of sha or the value of block hash.

** In particular, if the user is a miner who can manipulate the block by himself, the problem becomes bigger **

That is, you can use Random () in the practice, but never use in a commercial case.

In Part 1, I will finish the article at a level that understands the operation principle of smart contract.

In Part 2, let's talk about how to use the random () function in a commercial program.

Reference:
https://github.com/josex2r/jQuery-SlotMachine
https://medium.com/@promentol/lottery-smart-contract-can-we-generate-random-numbers-in-solidity-4f586a152b27
https://ethereum.stackexchange.com/questions/41463/a-possible-method-for-safe-random-number-in-lotteries?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
https://ethereum.stackexchange.com/questions/191/how-can-i-securely-generate-a-random-number-in-my-smart-contract?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Coin Marketplace

STEEM 0.20
TRX 0.19
JST 0.034
BTC 91017.04
ETH 3083.40
USDT 1.00
SBD 2.87