Solidity - Creating a slot machine (Odds game) using Smart Contract in Part 1.
First, the result screen is as follows.
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
Leave Solidity - Creating a slot machine (Odds game) using Smart Contract in Part 1. to:
Read more #dunkpay posts
Best Posts From DunkPay.com
We have not curated any of bitcoinyo's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From DunkPay.com
- Solidity - Voting with a Smart Contract Part 1
- Solidity - 스마트컨트랙트로 투표 해보기 1편
- Solidity - Creating a slot machine (Odds game) using Smart Contract in Part 1.
- Solidity - 스마트컨트랙트를 이용한 슬롯머신(확률게임) 만들기 1편
- DunkPay - NBA 챔피언 결정전 이벤트 '3점슛 컨테스트'
- DunkPay - WordPress Free Shopping Mall For Cryptocurrency
- DunkPay - WordPress 암호화폐 무료 쇼핑몰 만들기
- DunkPay - The Cryptocurrency payment service is now completely free - XRP, DNK support
- DunkPay - 암호화폐 결제서비스 라이브러리 완전 무료화 - XRP, DNK 지원
- DunkPay - Cancel Etherium pending transaction