-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coin-Flip With Owner-Withdraw Function
56 lines (43 loc) · 1.46 KB
/
Coin-Flip With Owner-Withdraw Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract CoinFlip {
address private owner;
uint256 public contracBalance;
event CoinFlipped(address indexed player,bool result, uint256 winAmount);
constructor(){
owner = msg.sender;
}
function FlipCoiin() public payable {
require(msg.value > 0, "Please Pay Some Ethers!" );
uint256 betAmount = msg.value;
uint256 winings = 0;
//Function For Decide Head Or Tell OR Random Number Generate in 0 Or 1
uint256 randomNumber = uint256(keccak256(abi.encodePacked(block.timestamp,msg.sender))) % 2;
bool playerwins;
if (randomNumber == 0){
playerwins = true;
}
else{
playerwins = false;
}
if (playerwins){
winings = betAmount * 2;
payable(msg.sender).transfer(winings);
contracBalance -= betAmount;
}
else {
contracBalance += betAmount;
}
emit CoinFlipped(msg.sender, playerwins, winings);
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function");
_;
}
function withdrawBalance() external onlyOwner {
uint256 contractBalance = address(this).balance;
require(contractBalance > 0, "Contract balance is zero");
// Transfer the contract balance to the owner
payable(owner).transfer(contractBalance);
}
}