Solidity includes some built-in features that help us ensure valid conditions in our programs. It has require and revert. These functions can be used to check conditions and throw exceptions if they are not met. Let's learn with an example. If you remember the previous exercise, when we created the function to compare numbers, now let's put the require into practice.
Imagine we want to validate that only numbers greater than zero are valid. So the way we use require is simply by saying require that number one be greater than zero and number two also be greater than zero. And then, require will first accept the condition, but we also have the possibility of specifying a message. And we could say invalid data. Good?
So, I was missing number one here, okay? And now it's okay. Very good. So, by using require, we are validating the data that goes into the function. Let's try it.
Suppose I want to call compare numbers and I pass it zero and zero. And I'm getting and I am getting a revert. And as you can see, we can also see the message invalid data. So, when the condition we specify in require is not met, the system will trigger an exception that will revert all the state changes we are making, in addition to refunding all the remaining gas to whoever calls this function. One thing to note is that when we deploy this smart contract, we should clean up here.
And I'm deploying this smart contract and you can see that the transaction cost was approximately 432,000 gas. OK? Solidity, starting with version 0.8.4, introduced something called custom bugs.
And custom errors are very good compared to require because they consume less gas, and we're going to check that right now. So, custom errors are a way of basically defining, let's say, invalid error numbers. So here I am declaring a mistake. But now, instead of using a require, I'm going to include an if statement, and in this if statement I'm going to say if number one is equal to zero and number two is equal to zero, we're going to revert and pass invalid number . So, it's similar if you remember the for loop example where we called break and the execution stopped.
That was the execution of the for loop. However, here by calling revert, we are explicitly saying: "When this condition occurs, I want you to revert all state changes and trigger an execution." It's very similar to require, but here we don't need any condition. We can simply say: "Revert". For example, and that's all.
The function can never be called by anyone. Yes, you can call her, but it will be reversed. So, the great thing about revert is that you can also pass information, as in this case where we pass the error. When the error occurs, we add more information so users know what the problem was. Remember that we previously spent this much on gas.
Let's deploy again . Let's make a comparison, because custom errors are very gas-efficient. Therefore, instead of using require, I recommend using errors. So, let's deploy. And as you can see, the cost of the transaction is 408,000.
OK? But we're saving more than 30, well, around 24,000 of gas, which is a big improvement. As the contract continues to grow, you'll have many require statements and you'll notice the improvement more thanks to custom errors. Finally, let's call compare with 0 0. We call it let's call compare with 0 0.
We call it . And as you can see, it also reverts and we can see the error provided by the contract: invalid number. As you can see, we can achieve the same goals, but saving a little gas. Alright?
Automatic transcript — names and jargon may be misspelled.