0

I am able to deposit eth into this contract using the remoteAdd() function, and I can confirm that the eth has been added by using the getBalance() function. However, I am unable to withdraw the contract balance to msg.sender using the withdraw() function. Could someone please help me out, thanks!

pragma solidity 0.5.1;

contract tictactoe {

    //Making explicit all the variables
    uint256 public playerCount;
    uint public betAmount;
    uint public totalBets;
    mapping(uint => Player) public player;
    address owner;

    //This is the state of the game
    enum State { Waiting, InProgress, Finished }
    State public state;

    //You can only send requests to the original contract address
    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    //This is the structure of a player
    struct Player {
        uint _id;
        uint _betAmount;
        address _owner;
    }

    constructor() public {
        owner = msg.sender;
        state = State.Waiting;
        playerCount = 0;
    }

    function remoteAdd() public payable {
        require(msg.value >= 0);
        incrementCount();
        betAmount = msg.value;
        owner = msg.sender;
        player[playerCount] = Player(playerCount, betAmount, owner);
        totalBets += betAmount;
    }

    function () external payable{
        remoteAdd();
    }

    //See how much ether the contract is currently holding
    function getBalance() public view returns(uint){
        return totalBets;
    }


    //Function that increases player count
    function incrementCount() internal {
        playerCount += 1;
    }

    //Function that signals the start of the game once two players have
    //placed bets.
    function beginGame() public {
        require(playerCount == 2);
        state = State.InProgress;
    }

    function endGame() public {
        require(state == State.InProgress);
        state = State.Finished;
    }

    function payout() public returns (bool success){
       require(state == State.Finished); 
       msg.sender.transfer(totalBets);
       msg.sender.call.value(totalBets).gas(7000)("");
       return true;
    }

    function withdraw() public {
        uint bal = address(this).balance;
        msg.sender.transfer(bal);
    }

}
  • 1
    Please explain what you mean by "unable". – goodvibration Jan 30 at 6:05
2

Well, I replicated your code in Remix, and it is working fine. The withdrawal functionality is sending the Ether to the msg.sender. The problem here is your getBalance(). It is returning the totalBets, which you arent deducting anywhere after an whithdrawl. Therefore, I assume you are always incrementing the totalBets, but never subtracting it when someone withdrawal, hope this helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.