-
-
Notifications
You must be signed in to change notification settings - Fork 37
Gas fee optimization #92
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,15 +236,17 @@ contract Chainvoice { | |
|
|
||
| // ========== Pay single invoice ========== | ||
| function payInvoice(uint256 invoiceId) external payable nonReentrant { | ||
| if (invoiceId >= invoices.length) revert InvalidInvoiceId(); | ||
| InvoiceDetails storage invoice = invoices[invoiceId]; | ||
|
|
||
| if (msg.sender != invoice.to) revert NotAuthorizedPayer(); | ||
| if (invoice.isPaid) revert InvoiceAlreadyPaid(); | ||
| if (invoice.isCancelled) revert InvoiceCancelledError(); | ||
| require(invoiceId < invoices.length, "Invalid invoice ID"); | ||
|
|
||
| InvoiceDetails storage invoiceStorage = invoices[invoiceId]; //(read once from storage) | ||
| InvoiceDetails memory invoice = invoiceStorage; // now read all from this invoice(memory) | ||
| require(msg.sender == invoice.to, "Not authorized"); | ||
| require(!invoice.isPaid, "Already paid"); | ||
| require(!invoice.isCancelled, "Invoice is cancelled"); | ||
|
Comment on lines
+239
to
+245
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify revert style mismatch between contract and tests.
rg -n -C2 'function payInvoice|require\(|revert ' contracts/src/Chainvoice.sol
rg -n -C2 'expectRevert\(Chainvoice\..*selector\)' contracts/test/Chainvoice.t.solRepository: StabilityNexus/Chainvoice Length of output: 9894 Revert ABI changed from custom errors to strings (breaks selector-based tests and increases gas). Lines 239–245 replace custom-error reverts with 🔧 Proposed fix- require(invoiceId < invoices.length, "Invalid invoice ID");
+ if (invoiceId >= invoices.length) revert InvalidInvoiceId();
InvoiceDetails storage invoiceStorage = invoices[invoiceId]; //(read once from storage)
InvoiceDetails memory invoice = invoiceStorage; // now read all from this invoice(memory)
- require(msg.sender == invoice.to, "Not authorized");
- require(!invoice.isPaid, "Already paid");
- require(!invoice.isCancelled, "Invoice is cancelled");
+ if (msg.sender != invoice.to) revert NotAuthorizedPayer();
+ if (invoice.isPaid) revert AlreadySettled();
+ if (invoice.isCancelled) revert AlreadySettled();Additionally, lines 241–242 copy the entire struct from storage to memory, including unused dynamic fields ( 🤖 Prompt for AI Agents |
||
|
|
||
| // Effects first for CEI (mark paid, bump fees), then interactions | ||
| invoice.isPaid = true; | ||
| invoiceStorage.isPaid = true; | ||
|
|
||
| if (invoice.tokenAddress == address(0)) { | ||
| if (msg.value != invoice.amountDue + fee) revert IncorrectPaymentAmount(); | ||
| accumulatedFees += fee; | ||
|
|
||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: StabilityNexus/Chainvoice
Length of output: 20284
Cache only required fields instead of copying the entire struct from storage to memory.
Line 242 copies the entire
InvoiceDetailsstruct (including unused fields and dynamic strings) from storage to memory. This introduces unnecessary SLOAD operations forid,encryptedInvoiceData, andencryptedHash—none of which are used bypayInvoice. Extract only the six fields actually needed:to,isPaid,isCancelled,tokenAddress,amountDue, andfrom.♻️ Proposed refactor (cache only used fields)
🤖 Prompt for AI Agents