Solidity Error Handling

Hey @Florian_P

The EVM refunds you the gas fee that you have not spent yet .
The instructions that are executed before the require are not refunded.

Cheers,
Dani

1 Like

HI ,

I am a bit confused with = and ==

Is = used when key points to value ?
== is used when value equal to value ?
I am not sure.

Anyone can advise me so i can understand deeper ?
Thanks

Hi @M.K

You assign a value with =, you compare two values with ==.

Cheers,
Dani

1 Like

Hey Filip,

I have a problem doing the quiz: For the last question I cannot choose an answer. I already checked in the dev tools:

Uncaught TypeError: _.contains is not a function
    at App.AssessmentQuestion.choiceActive (application-aab0dfd604bd70fc0bd0cf183432d4fe3f0613476d71956eb50cedec03bba99a.js:72)
    at HTMLDivElement.eval (eval at T (application-aab0dfd604bd70fc0bd0cf183432d4fe3f0613476d71956eb50cedec03bba99a.js:18), <anonymous>:3:37)
    at Object.refresh (application-aab0dfd604bd70fc0bd0cf183432d4fe3f0613476d71956eb50cedec03bba99a.js:18)
    at b (application-aab0dfd604bd70fc0bd0cf183432d4fe3f0613476d71956eb50cedec03bba99a.js:18)
    at Object.Twine.refreshImmediately (application-aab0dfd604bd70fc0bd0cf183432d4fe3f0613476d71956eb50cedec03bba99a.js:18)
    at HTMLButtonElement.r (application-aab0dfd604bd70fc0bd0cf183432d4fe3f0613476d71956eb50cedec03bba99a.js:18)
    at HTMLButtonElement.dispatch (application-aab0dfd604bd70fc0bd0cf183432d4fe3f0613476d71956eb50cedec03bba99a.js:2)
    at HTMLButtonElement.g.handle (application-aab0dfd604bd70fc0bd0cf183432d4fe3f0613476d71956eb50cedec03bba99a.js:2)

This error message comes every time I try to choose an answer.
Btw I’m using the latest version of the brave browser (already turned off brave shields for that site).

Thanks already
xela

1 Like

Hey @xela, hope you are ok.

I have tested the quiz and is working great for me, maybe you could try to test it again but with another browser? which one are you using?

Carlos Z

Hi Filip.
This is the first time I message you. I would like to tell that I like your Solidity lectures very much.
I have a question about require.
I wonder if we can use require on two conditions simultaneously. I have looked on google and I have found that we can require condition1 && condition2. but is there a require condition1 OR condition2?
I couldn’t find anything about that. or is there another way to express it?
I believe that i could be very useful to require that if either condition1 or 2 or maybe 3 is met, then run the function.

2 Likes

Some of these quizes are not working with the brave browser. But then it worked with Microsoft edge.

1 Like

Hey @Meriem, hope you are ok.

yeah it is complete possible to have an OR instead of AND.

Example:

// cond1 AND cond2 must be true
require(condition1 == true && condition2 == true);
// cond1 OR cond2 must be true
require(condition1 == true || condition2 == true);

Carlos Z

2 Likes

Some times is the brave shields, you could try to turn them off for the academy.

Carlos Z

yes I know that brave shields causes trouble on some websites. Therefore I already turned it off, but I still get that error at some quizes. I than just copy-paste the quiz-url into my microsoft edge and do it there (:

Hey @Meriem,

As you can see from @thecil’s example, the syntax in Solidity for coding conditions with logical operators is very much like JavaScript. I’m hesitant to say it is exactly the same, as I have never done a thorough comparison test. But as you can see you can use Logical AND && and Logical OR || just like in JavaScript. You can also use Logical NOT ! in the same way too. In fact there is a huge overlap with the operators in general *.

I’m assuming, of course, that you’ve already familiar with JavaScript. If not, then before moving on too much more with Solidity, you should definitely consider doing our JavaScript course, as in Solidity there is a lot of overlap with Javascript in terms of syntax, structure and concepts. There are obviously key differences, but knowing JavaScript certainly helps a lot. :smiley:


* One good example of a difference is with the equality operators. JavaScript has both  === and == ,  and !== and != ,  but Solidity only has  == and !=  (there is a logical reason why, but I’ll leave you to work that out for yourself :wink: )

2 Likes

modifier error

I wonder why the function transfer is not working?? it throws an error on the require statement as shown in picture. Thanks!

1 Like

Try to move the assert to the end of the function, you should _transfer first, and then the assert will check the condition.

Carlos Z

1 Like

Thank you for the reply! I’ll look at those details more carefully next time :slight_smile:

1 Like

Hi @juanmcba,

If you’re still having problems with this…

I can’t see your code very clearly from the fuzzy screen shot, or the error message, but maybe your first require statement is throwing because it’s actually doing its job? Does the error message you’re getting include the message you’ve added to the require statement, “Not sufficient balance”? If so, then are you calling transfer from an address which hasn’t added any balance yet? If you are, then require() is preventing the caller from transferring funds they don’t have… which is good. Check the address that is calling the transfer function in the Account field, and make sure this same address has already added a balance that is more than the amount input into the transfer function.

If that’s not the problem, and you haven’t resolved it yet, then you’ll need to post your full code, and not a screen shot, so that we can deploy and test it ourselves.

… but if your error message doesn’t include “Not sufficient balance”, then that means it isn’t your first require statement that’s throwing at all, but the assert statement. You need to make the modification that @thecil has already told you about, anyway, otherwise assert() will still throw after you’ve resolved any other issues.

Can someone please help me understand if the gas fees get returned to the user when the contract gets reverted? On one blog it says that they do not (blog one here) and in two blog it says that they do ( blog two here) . I hope someone with sufficient understanding of the topic and or experience can clarify this.

Thank you in advance.

Joseph G

1 Like

Hi @JosephGarza,

It depends on whether it is require() or assert() which fails and causes the transaction to revert…

When a function is called, a gas limit has to be allocated to the transaction. If require() throws, only the gas consumed up to that point is spent (including the gas required to execute the require function itself) , and the gas remaining in the “budget” for that transaction (up to the gas limit) is refunded to the caller (effectively unspent). Because of this functionality in terms of gas consumption, you can proabably see that the earlier in the function require() is placed, the less the cost of gas will be if it throws and the transaction reverts. This seems logical when we consider that require() is meant to throw. While we want to ensure that there is at least some cost associated with calling a function (whether it is successful or not) as a disincentive to spam the network, we don’t necessarily want to penalise those who call the function with invalid inputs by mistake.

In contrast, if assert() throws, it causes all of the remaining gas in the “budget” allocated to that transaction (up to the gas limit) to be consumed, and so can result in a considerably higher cost.

I hope that clarifies things, but do let us know if anything is unclear, or if you have any further questions :slight_smile:

2 Likes

Thank you, it actually does. Thank you very much. My reasoning was the actual block hash but I can see that my logic was far off. I can see how transaction costs are designed to incentivize and impact.

Thank you.

1 Like

Great, I’m glad that has helped :slight_smile:

The situation with gas costs and refunds etc. is pretty difficult to wrap your head around to start with. It took me a while to piece together what was really happening, as I think it does most people. The problem is that many articles and blogs about the subject either don’t tell you the full story, or they are far too technical for beginners.

Since replying to you, I have also read the two blogs you attached, and, in my opinion, this one from EthereumDev, is the more helpful in terms of what happens with require(), because it does clarify that it is only the remaining gas which is refunded. But it doesn’t mention assert(). What is actually meant by remaining gas is also something that often seems to be missed out of many explanations, and I think to understand what this really means, you need to understand that remaining gas is linked to the gas limit.

This blog from Halborn, doesn’t mention either assert() or require(), and I also think it could easily cause confusion and could potentially be misleading, because it doesn’t explain that it is only the gas consumed for the operations up to and including the execution of require() , which is the cost to the caller when require() triggers revert.