Skip to content

fix: approve the batch total for a shared ERC20, not one order's share - #975

Merged
ryanio merged 1 commit into
mainfrom
fix/batch-cumulative-checks
Aug 22, 2026
Merged

ryanio merged 1 commit into
mainfrom
fix/batch-cumulative-checks

Conversation

@ryanio

@ryanio ryanio commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

fulfillOrders with exactApproval: true under-approves whenever two orders in a batch are priced in the same ERC20.

The mechanism

addApprovalIfNeeded dedups approvals on token:operator, keeping the first entry. getApprovalActions then issues approve(operator, requiredApprovedAmount) from that single entry. So for two listings priced 10 and 20 in the same token, the SDK emits one approval for 10 and the fulfillment reverts with panic 0x11 on the arithmetic.

The default path is unaffected: without exactApproval the approval is MAX_INT.

Why summing the entries would not fix it

This was the trap worth avoiding. requiredAmount in getInsufficientBalanceAndApprovalAmounts is an order's full requirement, not its shortfall, and an order whose allowance already covers it is filtered out before it ever becomes an entry. So with a partial pre-existing allowance, one order drops out of the list entirely and summing what remains still falls short.

The fix therefore accumulates the requirement from each order's summed consideration, which is the same map already being computed for msg.value, and raises each approval to that total. It only ever raises, never lowers.

Token keys are matched lowercased, because the summation map lowercases to merge two casings of one address while InsufficientApprovals.token deliberately reports the order's own casing. That distinction is already documented in balanceAndApprovalCheck.ts and a previous fix existed for getting it wrong.

Verification

npm run build, npm run lint (tsc and Biome), and npm test all clean at 210 passing.

Tripwire: reverting only the raise step, with the test left in place, fails on the assertion it is named for:

AssertionError: expected 10000000000000000000 to equal 30000000000000000000

That is the under-approval exactly. No other test changes, so the existing ERC721 per-identifier exact-approval test is undisturbed.

Related, not in this PR

Two findings from the same audit are deliberately held back:

fulfillOrders also checks the fulfiller's balance per order against the same un-decremented balances, so a fulfiller holding 25 can be handed a transaction needing 30 with no error at all. Making it throw is very likely right, but the per-order check credits offer items the fulfiller receives, and an order in a batch can offer ERC20, so a naive cumulative check would ignore incoming currency and falsely reject batches that actually balance. A false throw blocking a working batch is worse than the current silence, so that needs its own analysis and its own does-not-false-throw test.

createBulkOrders has this same root cause on the offerer side. A clean fix needs _formatOrder to return its insufficient approvals so the check can be cumulative, which is order-creation surface rather than the fulfill path.

`fulfillOrders` with `exactApproval: true` under-approves whenever two orders
in a batch are priced in the same ERC20. `addApprovalIfNeeded` dedups approvals
on `token:operator`, keeping the first entry, and `getApprovalActions` then
issues `approve(operator, requiredApprovedAmount)` from that one entry. For
listings of 10 and 20 the SDK approved 10 and the fulfillment reverted.

Summing the insufficiency entries would not fix it. `requiredAmount` in
`getInsufficientBalanceAndApprovalAmounts` is an order's FULL requirement, and
an order whose allowance already covers it is filtered out and contributes no
entry at all, so with a partial pre-existing allowance the sum still falls
short. Accumulate the requirement from each order's summed consideration
instead, which is the same map already computed for `msg.value`, and raise each
approval to that total, never lowering it.

Keys are matched on the lowercased token because the summation map lowercases
to merge two casings of one address, while `InsufficientApprovals.token`
deliberately reports the order's own casing.

Only `exactApproval: true` was affected; the default path approves MAX_INT.

Verified as a tripwire: reverting just the raise step fails the new test with
"expected 10000000000000000000 to equal 30000000000000000000" on the allowance
assertion, and no other test changes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ryanio
ryanio merged commit a5a0792 into main Aug 22, 2026
8 checks passed
@ryanio
ryanio deleted the fix/batch-cumulative-checks branch August 22, 2026 18:55
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.

1 participant