Repository navigation
fix: approve the batch total for a shared ERC20, not one order's share - #975
Merged
Merged
Conversation
`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>
This was referenced Aug 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fulfillOrderswithexactApproval: trueunder-approves whenever two orders in a batch are priced in the same ERC20.The mechanism
addApprovalIfNeededdedups approvals ontoken:operator, keeping the first entry.getApprovalActionsthen issuesapprove(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 withpanic 0x11on the arithmetic.The default path is unaffected: without
exactApprovalthe approval isMAX_INT.Why summing the entries would not fix it
This was the trap worth avoiding.
requiredAmountingetInsufficientBalanceAndApprovalAmountsis 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.tokendeliberately reports the order's own casing. That distinction is already documented inbalanceAndApprovalCheck.tsand a previous fix existed for getting it wrong.Verification
npm run build,npm run lint(tsc and Biome), andnpm testall clean at 210 passing.Tripwire: reverting only the raise step, with the test left in place, fails on the assertion it is named for:
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:
fulfillOrdersalso 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.createBulkOrdershas this same root cause on the offerer side. A clean fix needs_formatOrderto return its insufficient approvals so the check can be cumulative, which is order-creation surface rather than the fulfill path.