Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions contracts/src/Chainvoice.sol
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ contract Chainvoice {

uint256 totalAmounts = 0;
uint256 totalNativeFee = fee * n;
// Validate and sum
// Validate and mark paid in the same pass so a duplicate id in the
// batch hits AlreadySettled() on its second occurrence, before any
// funds move.
for (uint256 i = 0; i < n; i++) {
uint256 id = invoiceIds[i];
if (id >= invoices.length) revert InvalidInvoiceId();
Expand All @@ -324,11 +326,7 @@ contract Chainvoice {
if (inv.tokenAddress != token) revert MixedTokenBatch();

totalAmounts += inv.amountDue;
}

// Effects: mark all paid & bump fee accumulator BEFORE interactions
for (uint256 i = 0; i < n; i++) {
invoices[invoiceIds[i]].isPaid = true;
inv.isPaid = true;
}
accumulatedFees += totalNativeFee;
// Interactions
Expand Down
17 changes: 17 additions & 0 deletions contracts/test/Chainvoice.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ contract ChainvoiceTest is Test {
assertEq(alice.balance, aliceStart + totalPrincipal);
}

function testPayInvoicesBatch_RevertOnDuplicateId() public {
vm.prank(alice);
chainvoice.createInvoice(bob, 1 ether, address(0), keccak256("dup1"));

uint256 fee = chainvoice.fee();
uint256 totalFee = fee * 2;
uint256 totalPrincipal = 2 ether;

uint256[] memory ids = new uint256[](2);
ids[0] = 0;
ids[1] = 0;

vm.prank(bob);
vm.expectRevert(Chainvoice.AlreadySettled.selector);
chainvoice.payInvoicesBatch{value: totalPrincipal + totalFee}(ids);
}

/* ------------------------------------------------------------ */
/* FUZZ TESTING */
/* ------------------------------------------------------------ */
Expand Down