1

When I try to excute the deposit function it shows Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution error: revert

{
    mapping (string => mapping(string => int)) gameResult;
    address player1;
    bool hasRight1; 

    address player2;
    bool hasRight2; 
    address public rec = 0x6F40147fE553b2Fb16A41C57CC87Ab72678d85C2;
    string public choice1;
    string public choice2;
    function deposit1(address reciver) public payable {
        require(msg.value == 5 ether);
        require(reciver == rec);
        reciver.transfer(msg.value);
        player1 = msg.sender; 
        hasRight1 = true;
     }
     function deposit2(address reciver) public payable {
        require(msg.value == 5 ether);
        require(reciver == rec);
        reciver.transfer(msg.value);
        player2 = msg.sender; 
        hasRight2 = true;
     }
    function rps() public
    {   // constructor
        gameResult["rock"]["rock"] = 0;
        gameResult["rock"]["paper"] = 2;
        gameResult["rock"]["scissors"] = 1;
        gameResult["paper"]["rock"] = 1;
        gameResult["paper"]["paper"] = 0;
        gameResult["paper"]["scissors"] = 2;
        gameResult["scissors"]["rock"] = 2;
        gameResult["scissors"]["paper"] = 1;
        gameResult["scissors"]["scissors"] = 0;
    }

    function play (string choice) public 
    {     
        if (msg.sender == player1&&hasRight1 == true)
            choice1 = choice;
        else if (msg.sender == player2 &&hasRight2 == true)
            choice2 = choice;

    }
    function sendReward()  public payable
    {
        if (bytes(choice1).length != 0 && bytes(choice2).length != 0)
        {

            int winner = gameResult[choice1][choice2];
            if (winner == 1)

                player1.transfer(10 ether);
            else if (winner == 2)
                player1.transfer(10 ether);
            else
            {
                player1.transfer(5 ether);
                player2.transfer(5 ether);

            }


        }
    }
}

Update: I followed a suggestion and deleted parameter in deposit 1 and 2 function. Now both deposit functions work. But when I click sendReward function it says Gas estimation errored with the following message (see below). The transaction execution will likely fail

pragma solidity ^ 0.4.13;
contract exercise3
{
    mapping (string => mapping(string => int)) gameResult;
    address player1;
    bool hasRight1; 

    address player2;
    bool hasRight2; 
    address public rec = 0x6F40147fE553b2Fb16A41C57CC87Ab72678d85C2;
    string public choice1;
    string public choice2;
    function deposit1() public payable {
        require(msg.value == 5 ether);
        rec.transfer(msg.value);
        player1 = msg.sender; 
        hasRight1 = true;
     }
     function deposit2() public payable {
        require(msg.value == 5 ether);
        rec.transfer(msg.value);
        player2 = msg.sender; 
        hasRight2 = true;
     }
    function rps() public
    {   // constructor
        gameResult["rock"]["rock"] = 0;
        gameResult["rock"]["paper"] = 2;
        gameResult["rock"]["scissors"] = 1;
        gameResult["paper"]["rock"] = 1;
        gameResult["paper"]["paper"] = 0;
        gameResult["paper"]["scissors"] = 2;
        gameResult["scissors"]["rock"] = 2;
        gameResult["scissors"]["paper"] = 1;
        gameResult["scissors"]["scissors"] = 0;
    }

    function play (string choice) public 
    {     
        if (msg.sender == player1&&hasRight1 == true)
            choice1 = choice;
        else if (msg.sender == player2 &&hasRight2 == true)
            choice2 = choice;

    }
    function sendReward()  public payable
    {
        if (bytes(choice1).length != 0 && bytes(choice2).length != 0)
        {

            int winner = gameResult[choice1][choice2];
            if (winner == 1)

                player1.transfer(10 ether);
            else if (winner == 2)
                player1.transfer(10 ether);
            else
            {
                player1.transfer(5 ether);
                player2.transfer(5 ether);

            }


        }
    }
}
  • "When i try to excute deposit function": 1. There are two deposit functions, which one of them are you referring to? 2. How exactly are you trying to execute it? (show your code and specify the value of each parameter in case it's not obvious from your code). 3. I think I've already mentioned this on one of your previous posts - the reciver parameter is completely redundant, and you may as well use rec (i.e., no need to pass reciver just in order to make sure that it's identical to rec). – goodvibration Nov 28 '19 at 7:25
  • BTW, I have also previously noted that you've misspelled "reciver", so I kinda get the feeling that you're not even bothering to read the comments that you're given! – goodvibration Nov 28 '19 at 7:26
  • @goodvibration thanks for your suggestions, i updated my code. After i deleted parameter in deposit 1 and 2, it works now. but it shows error when i call play function – chen Crush Nov 28 '19 at 15:19

Your Answer

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

Browse other questions tagged or ask your own question.