I am coding a coin flip contract. I have an issue with the amount sent by the smart contract to a player who wins the game.
What I want to do is: when you loose the coin flip, you loose your bet and when you win, you get 2 x the bet amount - our royalty. Yet, when I deploy the contract on Remix and test the PayWinner() function, I do not get 2x what I bet - royalty.
Thanks for your help!
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
contract CoinFlip {
uint roundId;
uint betAmount;
uint royalty;
address owner;
address payable[] players;
struct round {
address payable player;
uint betAmount;
bool playerChoice; // 0 = heads; 1 = tails
bool draw;
bool win;
uint winAmount;
}
round public myRound;
mapping (uint => round) public flipHistory; // map the roundId with the round data
constructor() {
owner = msg.sender;
betAmount = 0;
roundId = 0;
}
function setRoyalty(uint _royalty) public onlyOwner returns (uint) {
return royalty = _royalty / 100;
}
function getRandomNumber(uint _bet, bool _playerChoice) public payable {
// minimum amount to be bet
require(msg.value > .001 ether);
// update the array players with the latest player
players.push(payable(msg.sender));
// update roundId
roundId = roundId+1;
// update player in Struct
myRound.player = payable(msg.sender);
// update betAmount in Struct
myRound.betAmount = _bet;
// update playerChoice in Struct
myRound.playerChoice = _playerChoice;
myRound.draw = uint256(keccak256(abi.encodePacked(
msg.sender,
)
)
)
% 2 == 1;
payWinner();
}
function payWinner() private {
if (myRound.draw == myRound.playerChoice) { // if else statement
myRound.win = true;
// compute amount to be transferred to winner
myRound.winAmount = myRound.betAmount * (2 - royalty);
// update mapping
flipHistory[roundId] = myRound;
payable(myRound.player).transfer(myRound.winAmount);
}
else {
myRound.win = false;
// update mapping
flipHistory[roundId] = myRound;
}
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}