The Standard

The Standard
DeFiHardhat
20,000 USDC
View results
Submission Details
Severity: low
Invalid

[G-#] Consider Using custom errors instead of `require()`

Description: Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)

Source: https://blog.soliditylang.org/2021/04/21/custom-errors/:

Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.

Custom errors are defined using the error statement, which can be used inside and outside of contracts (including interfaces and libraries).

Impact: Changing this will make your code more gas efficient.

Recommended Mitigation: Let's take an example of your code to suggest a cool (and ordered) way to create this custom errors. Let's take the onlyManager() modifier of the LiquidationPool contract:

modifier onlyManager() {
require(msg.sender == manager, "err-invalid-user");
_;
}

We can make it more efficient. First, let's define a custom error for this particular message. To make it more specific, we'll start the name of our error with the name of the contract that this error is located in, followed by double underscore. So, we'll make something like

error LiquidationPool__InvalidUserError();

Then, we'll modify the condition of our modifier by using an if statement

modifier onlyManager() {
if (msg.sender != manager) {
revert LiquidationPool__InvalidUserError();
}
_;
}

This is more gas efficient than using strings (and looks cool, right?)

Updates

Lead Judging Commences

hrishibhat Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

informational/invalid

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.