0

OK, I'm baffled guys. maybe it's the amount of gas the function is using up that is causing this not to work.

when I call the function:

I get the following in response but no movement of Eth from my contract to the account:

{
  tx: '0xf091c81e89d1291e3c8a379a4686e3c424f5f34d1afad290d93c2fc2e1bd5775',
  receipt: {
    transactionHash: '0xf091c81e89d1291e3c8a379a4686e3c424f5f34d1afad290d93c2fc2e1bd5775',
    transactionIndex: 0,
    blockHash: '0x03f7a3dd8933d55a34ae6f0d93a7183cc245b28377292384e40dc5c0b2e257cb',
    blockNumber: 9,
    from: '0x016dce0a4cfb747914bb462e8e0bd56f0835cdc5',
    to: '0xa4d845f0f65f3cb9738f45552d16a94621033c30',
    gasUsed: 34388,
    cumulativeGasUsed: 34388,
    contractAddress: null,
    logs: [ [Object] ],
    status: true,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000002000000000000000000000000000000008000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000001000000000000000000000000000000000000000000000',
    rawLogs: [ [Object] ]
  },
  logs: [
    {
      logIndex: 0,
      transactionIndex: 0,
      transactionHash: '0xf091c81e89d1291e3c8a379a4686e3c424f5f34d1afad290d93c2fc2e1bd5775',
      blockHash: '0x03f7a3dd8933d55a34ae6f0d93a7183cc245b28377292384e40dc5c0b2e257cb',
      blockNumber: 9,
      address: '0xA4D845f0f65f3CB9738F45552D16a94621033c30',
      type: 'mined',
      id: 'log_7de9a41a',
      event: 'Message',
      args: [Result]
    }
  ]
}

here is my solidity code:

pragma solidity >=0.4.21 <0.7.0;

//import "github.com/Arachnid/solidity-stringutils/strings.sol";

//This contract sets the owner
contract Owned{
  event MyGuess(address indexed from, uint myguess);
  event Message(string message);
  address payable public owner;

  constructor() internal{
    owner = msg.sender;
  }

  //Access control Modifier
  modifier onlyOwner() {
    require(msg.sender == owner,
            "Only the owner of this contract can call this function.");
    _;
  }
}

contract Mortal is Owned{

  function destroy() public onlyOwner {
    selfdestruct(owner);
  }
}

contract Slot_Machine is Mortal{

  // 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 haveguessed;
  }

  mapping (address => Player) public player;

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

  constructor() public{
    //player[msg.sender].wallet = msg.sender;
    newPlayer(10,5);
  }

  //create a new player using the players address as
  //the key in the key-value pair mapping table.
  function newPlayer(uint pdeposit, uint pguess) public {

    player[msg.sender] = Player(msg.sender, pdeposit, pguess, true);

  }


  function generateRandomNumber(uint rand) public {
    randomNumber = rand;
  }

  
  //pay out double the players deposit if they guess
  //correctly the randomly generated number.

  function payOut(address to) public {

    //check if players guess == randomNumber and pay pay out
    //if it does.

    if(player[to].guess == randomNumber){

      player[to].wallet.transfer(player[to].deposit*2);
      emit Message("You Win!");

    } else {

        emit Message("Unlucky this time! try again when you are feeling lucky!");

    }
  }

  function getBalance() public view returns(uint) {
    return address(this).balance;
  }

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


}
  • my bad the funtion I call in the truffle console is await instance.payOut(accounts[2]) and my contract has a balance... – oBlox Jun 22 at 21:31
  • You can edit your own question if you want to fix some details. – Ismael Jun 22 at 23:46
  • Without inspecting the event Message generated we can't say much, both branches within payOut generate a Message event. You said the balances didn't change in that case it should have emitted Message("Unlucky this time!...) – Ismael Jun 22 at 23:50
  • @Ismael thanks. I will inspect the message and come back with my findings. I assumed that the balances would change because my tests made sure both the guess and the random number are the same... – oBlox Jun 23 at 5:39
  • Well, my first thought would be that player[to].guess != randomNumber. – goodvibration Jun 23 at 5:43
0

OK, so the problem wasn't in my solidity code (although I made changes to arrive at a solution) but rather in the call I was making in the truffle console. my payOut function now looks like this:

function payOut(uint amount) public {
    //check if players guess == randomNumber and pay pay out
    //if it does.

    if(player[msg.sender].guess != randomNumber){
      emit Message("Unlucky this time! try again when you are feeling lucky!");


    } else {

            msg.sender.transfer(amount);
            emit Message("You Win!");

    }
  }

I will revert a few things back to what they were but the uint ammount parameter stays.

My call in the truffle console is:

result = await instance.payOut(web3.utils.toWei("5","ether"),{from: accounts[0]})

this produced a successful balance transfer and the emission of the the "You Win" message. I'm finally fully understanding that when communicating with the local blockchain you must do so in the way you would if you were using a Web3.js object in your browser. seems obvious now.

EDIT: OK, so it turns out the issue was with my solidity code. You must include "ether" after the value amount to be transferred. so

player[to].wallet.transfer(player[to].deposit*2 ether);

So I'll be going back to using my original code from the question with this fix.

| improve this answer | |
  • You did not quite explain what the problem was (you probably need to add your previous call in the truffle console). – goodvibration Jun 23 at 8:23
  • @goodvibration quite right. the payOut function took a different format (see solidity code in question). In that version I expected that, given the address as an argument, the transfer of ether would be made seeing as the condition was met (player[to].guess == randomNumber). However, when I deployed the contract to the local blockchain and interacted with it through the truffle console, I would get the "You Win!" message but no transfer of ether. I think the transfer argument at the time player[to].depsoit*2 isn't converted into the form truffle considers a value of ether... – oBlox Jun 23 at 8:58

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.