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
vue
app and it's working fine.headsOrTails.events.GameResult( { fromBlock: 0 }, function(error, event) { console.log(event.returnValues); } );
true
returntrue
, the ones withfalse
returnnull
.