Skip to content

refactor: replace require strings with custom errors for gas optimiza…#42

Open
Atharva0506 wants to merge 1 commit into
StabilityNexus:mainfrom
Atharva0506:feat/custom-errors
Open

refactor: replace require strings with custom errors for gas optimiza…#42
Atharva0506 wants to merge 1 commit into
StabilityNexus:mainfrom
Atharva0506:feat/custom-errors

Conversation

@Atharva0506
Copy link
Copy Markdown
Member

@Atharva0506 Atharva0506 commented May 15, 2026

Addressed Issues:

Fixes #39

Screenshots/Recordings:

Not applicable. This is a smart contract optimization PR that does not affect the frontend UI.

Additional Notes:

This PR implements a major gas optimization refactor across the VouchMe smart contract.

  • Replaced 12 require(condition, "string") statements with if (!condition) revert CustomError(); patterns.
  • Defined 11 semantic custom errors at the contract level (e.g., InvalidSignature(), TestimonialDoesNotExist()).
  • Using custom errors is significantly cheaper (often ~60% less gas for reverts) because it avoids storing string messages in the contract bytecode.
  • Updated 20 vm.expectRevert() statements across 5 test files to properly catch the new error selectors.
  • Verification: All 65 tests in the Hardhat/Foundry suite are passing successfully. No merge conflicts with the struct-packing changes.

AI Usage Disclosure:

We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact. AI slop is strongly discouraged and may lead to banning and blocking. Do not spam our repos with AI slop.

Check one of the checkboxes below:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: Claude opus 4.6 via Antigravity

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions
  • If applicable, I have made corresponding changes or additions to the documentation
  • If applicable, I have made corresponding changes or additions to tests
  • My changes generate no new warnings or errors
  • I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contribution Guidelines
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
  • I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

Summary by CodeRabbit

  • Refactor

    • Updated contract error handling to use custom error codes instead of string-based messages for signature validation, testimonial management, payment processing, and treasury operations.
  • Tests

    • All test suites updated to validate custom error codes across signature verification, testimonial operations, payment handling, and monetization scenarios.

Review Change Stack

…tion

- Define 11 custom errors: InvalidSignature, TestimonialDoesNotExist,
  TestimonialHasBeenDeleted, TokensAreNonTransferrable, OnlyRecipientCanDelete,
  TestimonialAlreadyDeleted, InsufficientFeePayment, FeeTransferFailed,
  RefundFailed, SetTreasuryBeforeEnablingFees, TreasuryCannotBeZeroAddress
- Replace all 12 require(condition, string) with if/revert patterns
- Update 20 expectRevert calls across 5 test files to use error selectors
- All 65 tests pass
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 37015665-0d74-4a2a-864e-b81e7583a514

📥 Commits

Reviewing files that changed from the base of the PR and between 621a09b and 563beb8.

📒 Files selected for processing (6)
  • contracts/src/VouchMe.sol
  • contracts/test/VouchMe.t.sol
  • contracts/test/VouchMeFuzz.t.sol
  • contracts/test/VouchMeIntegration.t.sol
  • contracts/test/VouchMeMonetization.t.sol
  • contracts/test/VouchMeSignature.t.sol

Walkthrough

This PR replaces 11 string-based require statements in VouchMe.sol with custom Solidity errors. Error declarations are added, and revert logic across signature validation, payment checks, testimonial state verification, transfer blocking, deletion authorization, and admin controls is refactored. All five test files are updated to assert custom error selectors instead of literal revert strings.

Changes

Custom Error Migration

Layer / File(s) Summary
VouchMe.sol error definitions and implementation
contracts/src/VouchMe.sol
Eleven custom errors (InvalidSignature, TestimonialDoesNotExist, TestimonialHasBeenDeleted, TokensAreNonTransferrable, OnlyRecipientCanDelete, TestimonialAlreadyDeleted, InsufficientFeePayment, FeeTransferFailed, RefundFailed, SetTreasuryBeforeEnablingFees, TreasuryCannotBeZeroAddress) are declared and used in place of require-string checks across createTestimonial, getTestimonialDetails, _update, deleteTestimonial, setFee, and setTreasury functions.
Test assertion updates for custom error selectors
contracts/test/VouchMe.t.sol, contracts/test/VouchMeSignature.t.sol, contracts/test/VouchMeMonetization.t.sol, contracts/test/VouchMeIntegration.t.sol, contracts/test/VouchMeFuzz.t.sol
All test files updated to expect custom error selectors (e.g., VouchMe.InvalidSignature.selector, VouchMe.TestimonialHasBeenDeleted.selector) instead of literal revert reason strings across signature validation, testimonial state, transfer, deletion, payment, and admin-function tests.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • StabilityNexus/VouchMe#14: Newly added Foundry tests that currently expect literal revert strings like "Invalid signature" and "Testimonial has been deleted" now require updates to match the custom error selectors introduced in this PR.
  • StabilityNexus/VouchMe#15: Earlier testimonial flow changes to createTestimonial and deleteTestimonial behavior are now refactored here to use custom errors instead of string-based reverts.

Suggested labels

Solidity Lang

Suggested reviewers

  • ceilican

Poem

🐰 A rabbit hops through gas-optimized fields,
Where custom errors yield cleaner shields—
No strings, just selectors, precise and neat,
Making each revert a lightweight feat!

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: replacing require strings with custom errors for gas optimization, which is the primary focus of the PR.
Linked Issues check ✅ Passed All requirements from issue #39 are met: six required custom errors defined, require statements replaced with if/revert patterns, and test suite updated to expect new custom error selectors.
Out of Scope Changes check ✅ Passed All changes are directly related to replacing require strings with custom errors as specified in issue #39; no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Atharva0506 Atharva0506 marked this pull request as ready for review May 15, 2026 11:09
@Atharva0506
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor require statements to use Solidity Custom Errors for gas optimization

1 participant