-
Notifications
You must be signed in to change notification settings - Fork 32
Smart Contract Optimization via Custom Errors & Structural Refactoring Recducing String "requires" #102
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
base: main
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthroughReplaces string-based require messages in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
contracts/src/Chainvoice.sol (1)
107-123:⚠️ Potential issue | 🟡 MinorInconsistent validation:
amountDueis not validated for zero in single invoice creation.
createInvoicesBatchvalidatesif (amt == 0) revert InvalidAmount()(line 176), butcreateInvoiceallows zero-value invoices. This inconsistency could lead to unnecessary storage consumption and confusing user experience.🛡️ Proposed fix to add zero-amount validation
function createInvoice( address to, uint256 amountDue, address tokenAddress, string memory encryptedInvoiceData, string memory encryptedHash ) external { if (to == address(0)) revert ZeroAddress(); if (to == msg.sender) revert SelfInvoicing(); + if (amountDue == 0) revert InvalidAmount(); if (tokenAddress != address(0)) {
🧹 Nitpick comments (4)
contracts/src/Chainvoice.sol (3)
52-54: Minor formatting issue: inline comment placement.The trailing comment for
encryptedHashis awkwardly placed on a separate line. Consider moving it inline.✨ Suggested formatting fix
- string encryptedHash; - // Content hash or integrity ref + string encryptedHash; // Content hash or integrity ref }
162-168: Minor: extra leading space at line 163.There's an extra space before the
ifstatement on line 163, causing inconsistent indentation.✨ Formatting fix
if (tokenAddress != address(0)) { - if (tokenAddress.code.length == 0) revert NotContract(); + if (tokenAddress.code.length == 0) revert NotContract(); (bool ok, ) = tokenAddress.staticcall(
267-274: Optional: First invoice is validated twice (here and again in the loop).The validation at lines 271-274 for the first invoice (
invoiceIds[0]) is repeated wheni=0in the loop at lines 279-290. This is correct but slightly gas-inefficient.⛽ Consider starting loop at i=1 and including first invoice's amount
InvoiceDetails storage inv0 = invoices[firstId]; if (msg.sender != inv0.to) revert NotAuthorizedPayer(); if (inv0.isPaid || inv0.isCancelled) revert AlreadySettled(); address token = inv0.tokenAddress; - uint256 totalAmounts = 0; + uint256 totalAmounts = inv0.amountDue; uint256 totalNativeFee = fee * n; // Validate and sum - for (uint256 i = 0; i < n; i++) { + for (uint256 i = 1; i < n; i++) {contracts/test/Chainvoice.t.sol (1)
95-115: Consider expanding test coverage for new custom errors.Only
NotAuthorizedPayerandIncorrectPaymentAmountare tested. Many new error paths are untested:
ZeroAddress,SelfInvoicing(increateInvoice)InvalidInvoiceId,NotInvoiceCreator,InvoiceNotCancellable(incancelInvoice)InvoiceAlreadyPaid,InvoiceCancelledError(inpayInvoice)- Batch-specific errors (
ArrayLengthMismatch,InvalidAmount,MixedTokenBatch)Would you like me to generate additional test cases covering these error paths?
What is one by this PR:
Refactored Chainvoice.sol to replace expensive string revert messages with gas-efficient Custom Errors across all functions.
Optimized contract modifiers and internal naming conventions to align with best practices and resolve linting warnings.
Updated Chainvoice.t.sol test suite to verify specific custom error selectors and cleaned up unused imports.
@kumawatkaran523 Please review
Summary by CodeRabbit
Refactor
Tests
✏️ Tip: You can customize this high-level summary in your review settings.