2

Question 1) Let's imagine I have this code

require(userVotes[msg.sender] == 0);
presidents[_id-1].voteCount++;
userVotes[msg.sender] = _id;

so If I make a call to this function, and it doesn't pass the require, what really happens is in metamask it says : contract threw exception. then It still lets me to click on submit button. I'm curious is it still gonna take gas from my account?

Question 2) If the answer from the previous question is yes, then how much? the gas that it took require(userVotes[msg.sender] == 0); doing this command? I am still curious even though there was an exception in require , it still got included in a block and status was fail. What exactly got in block? nothing has changed.

Question 3) I tried to write something like require(userVotes[msg.sender] == 0,"error go away"); I want to catch this in front-end. What i did is after sending the transaction, I used event

.on('error', function(error){
console.log(error);
}

but it was not those words That I wrote in a solidity like a "error go away".

Thank you so much

1 Answer 1

3

All the informations I'll give you now can be found in the solidity docs (https://solidity.readthedocs.io/en/v0.4.24/control-structures.html?highlight=require#error-handling-assert-require-revert-and-exceptions)

The require(...) function calls revert(), which halts the execution and reverts the state of EVM, so that you contract call has effectively changed nothing. revert() does send the remaining gas to the users. That means any gas used until your require(...) call has been used up and cannot be rebated.

  1. The answer is yes
  2. The amount of gas until the require(...) call was executed is lost. Your transaction is included in a block, but it had no effect other than to increment the nonce and to decrement the balance of the sending account. Basically it just takes disk space forever from this point on and costs you ether without having any other effect.
  3. As far as I know it is not possible to get the error message from your front end yet. Discussion to this topic: Returning an error code with revert
2
  • 1
    So If I have a function and before require keyword, I have code that does change the variables (state variables). After require fails, It's gonna still take the gas that was needed to execute commands before the require, but when the require happens, whatever change happened before the require is also lost and also the gas is taken from my account. I just wanna know exactly that if something I change in state variables before the require and when the require happens, no change will be made at all even though it happened before meeting the require?
    – Chemistry
    Commented Aug 26, 2018 at 13:33
  • @GiorgiLagidze exactly
    – sea212
    Commented Aug 26, 2018 at 13:47

Your Answer

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

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