Description
The VouchMe smart contract currently relies on require(..., "String") statements for error handling. Upgrading these to Solidity Custom Errors (e.g., error TestimonialDoesNotExist();) will noticeably reduce both deployment size and execution gas costs, as custom errors are much cheaper than storing and reverting with string messages.
Currently, there are 6 instances of require statements with string messages in contracts/src/VouchMe.sol:
"Invalid signature"
"Testimonial does not exist"
"Testimonial has been deleted"
"Tokens are non-transferrable"
"Only recipient can delete"
"Testimonial already deleted"
Proposed Solution
Refactor the contract to use Custom Errors instead of require statements with string messages.
- Define custom errors at the contract level (or in a shared interface/library), for example:
error InvalidSignature();
error TestimonialDoesNotExist();
error TestimonialAlreadyDeleted();
error TokensAreNonTransferrable();
error OnlyRecipientCanDelete();
- Replace the existing
require(condition, "message") logic with standard if/revert patterns:
if (!condition) {
revert CustomErrorName();
}
- Update the corresponding tests in the Hardhat/Foundry test suite to expect the new custom errors instead of the old revert strings.
Benefits
- Gas Optimization: Reverting with a custom error is cheaper than reverting with a string.
- Smaller Deployment Size: Removing large revert strings keeps the contract bytecode smaller.
- Better Developer Experience: Custom errors appear explicitly in the ABI, making it easier for frontends and indexers to properly catch and handle specific revert reasons.
Description
The
VouchMesmart contract currently relies onrequire(..., "String")statements for error handling. Upgrading these to Solidity Custom Errors (e.g.,error TestimonialDoesNotExist();) will noticeably reduce both deployment size and execution gas costs, as custom errors are much cheaper than storing and reverting with string messages.Currently, there are 6 instances of
requirestatements with string messages incontracts/src/VouchMe.sol:"Invalid signature""Testimonial does not exist""Testimonial has been deleted""Tokens are non-transferrable""Only recipient can delete""Testimonial already deleted"Proposed Solution
Refactor the contract to use Custom Errors instead of
requirestatements with string messages.require(condition, "message")logic with standardif/revertpatterns:Benefits