Skip to content

Refactor require statements to use Solidity Custom Errors for gas optimization #39

@Atharva0506

Description

@Atharva0506

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.

  1. 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();
  2. Replace the existing require(condition, "message") logic with standard if/revert patterns:
    if (!condition) {
        revert CustomErrorName();
    }
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions