1

I would like to be able to log a result of a condition:

if(time > 5) {
    emit Result("time is larger than 5");
} else {
    emit Result("time is less than 5");
}

This will work just fine, but when using a require statement:

  require(time > 5);
  emit Result("time is larger than 5");

I can only log the event in case the require succeeds.

In case the require appears in a modifier:

modifier OnlyLargerThan5() {
  require(time > 5);
  emit Result("time is larger than 5");
  _;
}

Is there a way to log both cases (Larger > 5 > Less) other then the if..else statements?

0

The if...else statement is a completely valid option, and is one I would suggest.

Something you can do, if desired, is include a message in the require statement. This would not be an event, but would looks something like this:

require(time > 5, "time is less than 5");
emit Result("time is larger than 5");
  • 1
    Is the message from the require statement logged to the blockchain and can be traced (like an event)? – forhas Nov 16 '18 at 20:34
  • I do not believe so. The most logical solution would be to use the if...else statement. You will not be able to log any data if the transaction reverts. – shane Nov 16 '18 at 20:37
  • 1
    So it seems. Problem is that my function (that emits the events) gets called by another contract which in turn can revert.. so nothing can guarantee that I'll actually emit my events. – forhas Nov 16 '18 at 20:45
  • 1
    Correct. One of the biggest discussions in Solidity relates to external calls to other contracts. You can write a perfect function, but if there is another contract that is relying on it or it is relying on, then there is now a possibility of failure/attack vector. – shane Nov 16 '18 at 20:47

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.