1

A Solidity event is supposed to emit a boolean. In case of won = true the transaction receipt in web3 logs true as expected, but if won = false the emit logs null instead false. I have no idea why. First I thought it is because of the same variable name, but that is not the cause.

I have this Solidity function which either emits "true" or "false"

event GameResult(bool won);

//My simplified contract
function lottery(uint8 guess) public payable returns(bool){
  uint8 result = 1;
  bool won = false;
  if (guess == result) {
    //Won!
    msg.sender.transfer(msg.value * 2);
    won = true;
  }
  emit GameResult(won);
}

UPDATE: Here is my web3 function to call the contract (only the relevant part. Full code can be found on Github):

async function play() {
  let headsOrTailsSelection = 1;

  //A test to get the emit values from the contract: true "works", "false" not
  headsOrTails.once('GameResult', function(error, event){ console.log(event.returnValues); });

  returnValue = await headsOrTails.methods.lottery(headsOrTailsSelection).send({ from: account, value: amountToBetWei });
  console.log(returnValue);
}

General infos:

  • Currently using Ganache as a local blockchain for development
  • Solidity version: ^0.5.0
  • IDE: VS Code
6
  • Show your web3 code please. Nov 8, 2019 at 7:27
  • @goodvibration Just added it. Thanks for your help.
    – René K
    Nov 8, 2019 at 13:00
  • can you try to load all past events and see if false appears there? I tried your code in vue app and it's working fine. headsOrTails.events.GameResult( { fromBlock: 0 }, function(error, event) { console.log(event.returnValues); } );
    – Yahya
    Nov 8, 2019 at 15:07
  • @Yahya Same result. The events with true return true, the ones with false return null.
    – René K
    Nov 8, 2019 at 15:57
  • can you please switch to another network? like Rinkeby Ganache is not always good when it comes to the events.
    – Yahya
    Nov 8, 2019 at 16:21

2 Answers 2

1

For me it works fine on remix. I have run your code as is with no changes on remix and it shows the emitted value as true(Input: 1).

True: when given 1

Also it works prefectly well for false too(Input: 5). False when given 5

So maybe if you gave more information like the version of solidity compiler, IDE you are using and the network the contract is being deployed, then maybe there would be a better shot at understanding the problem.

New here! Any suggestions would be appreciated!!!

2
  • Hi vteja711, thanks for your support. In Remix I don't have issues either. Just added my web3 code above.
    – René K
    Nov 8, 2019 at 13:02
  • 1
    The problem was related to my outdated version of web3.js. Thanks to you and everyone else for helping.
    – René K
    Nov 8, 2019 at 16:42
1

The problem was related to my outdated version of web3.js. After updating to V1.2.2 (from 1.0.0-beta.34) the problem was solved. There was a Github issue describing the bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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