Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more info on Errors #25

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,67 @@ If a function should never be called from another contract, it should be marked

#### 1. Errors

##### A. Prefer custom errors.
##### A. Using Custom Errors Over Require

Custom errors are in some cases more gas efficient and allow passing useful information.
Utilize custom errors instead of `require` statements for clearer and more gas-efficient error handling. Solidity 0.8.26 supports the use of custom errors with `require`.
Aboudjem marked this conversation as resolved.
Show resolved Hide resolved

##### B. Custom error names should be CapWords style.
```solidity
error InsufficientFunds(uint256 requested, uint256 available);

function withdraw(uint256 amount) public {
if (amount > balance) {
revert InsufficientFunds(amount, balance);
}
balance -= amount;
}
```

> [!TIP]
Aboudjem marked this conversation as resolved.
Show resolved Hide resolved
> 💡 Custom errors save gas and provide more detailed error messages compared to traditional `require` strings.

##### B. Require with Custom Error (Solidity 0.8.26+)
Aboudjem marked this conversation as resolved.
Show resolved Hide resolved

Use the new `require(condition, error)` syntax to include custom errors in `require` statements, available in Solidity 0.8.26 and later.

```solidity
error InsufficientFunds(uint256 requested, uint256 available);

function withdraw(uint256 amount) public {
require(amount <= balance, InsufficientFunds(amount, balance));
balance -= amount;
}
```

> [!IMPORTANT]
> This new syntax provides a more efficient way to handle errors directly within `require` statements, enhancing both readability and gas efficiency.

##### C. Limit Require Messages
Aboudjem marked this conversation as resolved.
Show resolved Hide resolved

Prefer using custom errors over `require` with strings for better efficiency. If you must use `require` with a string message, keep it under 32 bytes to reduce gas costs.

> [!IMPORTANT]
> Custom errors are more gas-efficient and provide clearer error handling. Whenever possible, use them instead of `require` with a string message.

**Yes:**

```solidity
require(balance >= amount, "Insufficient funds");
```

> [!CAUTION]
> Keeping `require` messages concise (under 32 bytes) minimizes additional gas costs and improves efficiency.

**No:**

```solidity
require(balance >= amount, "The balance is insufficient for the withdrawal amount requested.");
```

> [!WARNING]
> Longer messages significantly increase gas costs. Avoid using verbose messages in `require` statements.


##### D. Custom error names should be CapWords style.

For example, `InsufficientBalance`.

Expand Down