I have finally figured out how to get to the events from the receipt and use them to return the result of flipping a coin.
contractInstance.methods.coinflip().send(bet_to_ether)
.on("transactionHash", function(hash){
console.log(hash);
})
.on("confirmation", function(confirmationNr){
console.log(confirmationNr);
})
.on("receipt", function(receipt){
console.log(receipt);
})
.then(function(res){
console.log("Result is: " + (res.events.coinFlipped.returnValues.win_loose ? "Win" : "Lose"));
//alert("The result is: " + (res.events.coinFlipped.returnValues.win_loose ? "Yes" : "No"));
$("#win_output").text(res.events.coinFlipped.returnValues.win_loose ? "Yes" : "No");
$("#bet_result").text((res.events.coinFlipped.returnValues.win_loose ?("You won "+ 2*bet) :("You lost "+ bet)) + " Ether" );
fetchBalance();
})
};
Side question. Is there a way to get a return value from a smartcontract function when you use send()
function? Or is it possible only with the call()
function?