TransWikia.com

Please look over my contract for re-entrancy

Ethereum Asked by oBlox on August 26, 2021

I know the random number generator isn’t secure and is susceptible to manipulation from miners and any advice on the method to generate random numbers will be appreciated.

However, the reason I’m posting my code is to ask if you (the community) could take a look and see if it’s safe from re-entrancy. I’ve done what I think is necessary to secure my code but I just want to double check before deploying on the Rinkeby network.

pragma solidity >=0.6.2 <0.7.0;
//SPDX-License-Identifier: UNLICENSED

contract Slot_Machine is Mortal{

  event MyGuess(address indexed from, uint myguess);
  event Message(address indexed from, string  message, uint guess, uint random_number, uint  deposit, uint  winnings);

  // A struct type that defines state variables of a player.
  //Every player has a wallet, amount they are willing to
  //deposit and a guess.
  struct Player{
      address payable wallet;
      uint deposit;
      uint guess;
  }

  bool reEntrancyMutex = false;
  bool isWinner;
  mapping (address => Player) public player;

  //The random number returned from the generator.
  uint randomNumber;

  constructor() public{}

  //psuedo-random number generator.
  function generateRandomNumber() internal returns(uint) {
    uint balancer = uint(keccak256(abi.encodePacked(block.number,block.difficulty,msg.sender))) % 100;
    randomNumber = uint(keccak256(abi.encodePacked(block.number,block.number-balancer,msg.sender))) % 100;
    return randomNumber;
  }

  //get random number. Used to display results on UI.
  function getRandomNumber() public view returns(uint){
    return randomNumber;
  }

  //create new player.
  function newPlayer(uint pdeposit, uint pguess) private{
    player[msg.sender] = Player(msg.sender, pdeposit, pguess);
  }

  //initiate bool to determine how player wins.
  function playResult() public{
    isWinner = player[msg.sender].guess == randomNumber;
  }

  //transfer players ether is they win.
  function transferWinnings() private{
    player[msg.sender].wallet.transfer(player[msg.sender].deposit*2 wei);
  }


  //Pay out double the players deposit if they guess correctly the randomly generated number.
  function getBalance() public view returns(uint) {
    return address(this).balance;
  }

  fallback () external payable{}

  receive () external payable{
    require(msg.sender.balance >= msg.value,
          "Insufficient balance to complete transaction.");
  }

  function main(uint pguess) public payable{
    require(!reEntrancyMutex);
    //create new player with deposit and guess.
    newPlayer(msg.value,pguess);
    //generate random number.
    generateRandomNumber();
    //initiate result checking mechanism.
    playResult();
    reEntrancyMutex = true;
    //check if player is a winner.
    if(!isWinner){
      emit Message(player[msg.sender].wallet,"Loss",pguess,getRandomNumber(),msg.value, 0);

    } else {
          transferWinnings();
          emit Message(player[msg.sender].wallet,"Win",pguess,getRandomNumber(),msg.value, msg.value*2);
    }

    reEntrancyMutex = false;

  }
}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP