Skip to content

fix: cap unitsToFill at what is left of a partially filled order - #977

Closed
Xowiek wants to merge 1 commit into
ProjectOpenSea:mainfrom
Xowiek:fix/clamp-units-to-remaining
Closed

Xowiek wants to merge 1 commit into
ProjectOpenSea:mainfrom
Xowiek:fix/clamp-units-to-remaining

Conversation

@Xowiek

@Xowiek Xowiek commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

The comment sitting over this code already says what should happen — the order is adjusted by "the minimum of the amount to fill and the remaining order left to be fulfilled". No minimum is ever taken. unitsToFill reaches mapOrderAmountsFromUnitsToFill and getAdvancedOrderNumeratorDenominator exactly as the caller passed it.

Seaport does take that minimum. On an order with 4 of 10 units left, a request for 8 transfers 4 and closes the order out. The SDK meanwhile sizes the offer side for 8 units the offerer no longer holds, and its own balance check stops the fill:

The offerer does not have the amount needed to create or fulfill.

Which reads as the offerer's problem. The offerer is fine — the request just ran past the remainder. Send the same fill straight to the contract and it settles.

Repro — mint 10 units, let someone fill 6, then ask for 8:

contract, fulfillAdvancedOrder 8/10 transfers 4, order now fully filled
SDK, unitsToFill: 8 throws before building anything

Patch — take the minimum, in the single and batch paths both. Amounts, tips and numerator/denominator then describe a fill that will really happen, and value covers the units that actually move.

I went with capping rather than a new error because the contract caps, and because fulfillOrder with no unitsToFill at all already fills whatever is left — capping keeps an explicit request consistent with that. Worth a second opinion if you'd rather it raised.

This only kicks in once an order is partially filled. Asking for more units than the order was ever divisible into is a caller mistake rather than a race, and getAdvancedOrderNumeratorDenominator still rejects that with the message it has today — there's a test pinning it.

Tests — four unit cases on the cap in fulfill-units.spec.ts, and fulfill-remaining-units.spec.ts drives the real thing end to end on both paths, asserting value is 4 ETH rather than 8 and that the fulfiller ends up holding all 10 units. Both integration cases fail on main with the error above. Suite 215.

The comment over this code already describes the intended behaviour -- the
order is adjusted by "the minimum of the amount to fill and the remaining
order left to be fulfilled" -- but no minimum is ever taken. unitsToFill
goes into mapOrderAmountsFromUnitsToFill and
getAdvancedOrderNumeratorDenominator exactly as passed.

Seaport caps the fill itself. On an order with 4 of 10 units left, asking
for 8 transfers 4 and closes the order. The SDK instead sizes the offer
side for 8 units the offerer no longer holds, so its own balance check
rejects the fill:

  The offerer does not have the amount needed to create or fulfill.

The message points at the offerer, who is fine; the request simply ran past
the remainder. The same call placed directly against the contract settles.

Take the minimum the comment calls for, in both the single and batch paths.
Amounts, tips and numerator/denominator are then derived from a fill that
will actually happen, and the value attached to the transaction covers the
units that really transfer.

Only applies once an order is partially filled. Asking for more units than
the order was ever divisible into is a caller error rather than a race, and
getAdvancedOrderNumeratorDenominator still rejects that with its own
message.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.95%. Comparing base (f618d19) to head (fe0710f).
⚠️ Report is 368 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #977      +/-   ##
==========================================
+ Coverage   98.27%   98.95%   +0.67%     
==========================================
  Files          35       51      +16     
  Lines       14526    18603    +4077     
  Branches      660      992     +332     
==========================================
+ Hits        14276    18408    +4132     
+ Misses        245      188      -57     
- Partials        5        7       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ryanio

ryanio commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks, and yes to capping rather than raising. Matching what the contract does, and staying consistent with fulfillOrder with no unitsToFill at all, is the right call.

One note on the tests. Reverting the whole source hunk removes the exported clampUnitsToRemaining, so fulfill-units.spec.ts fails at module load rather than on an assertion. Narrowing the revert to just the two call sites gives the honest answer: your four unit cases still pass, because they call the helper directly and never touch the wiring, and it is the two integration cases in fulfill-remaining-units.spec.ts that catch a regression, both failing on "The offerer does not have the amount needed to create or fulfill" exactly as you described. The unit cases are still worth having, they just cover the helper rather than the fix.

This has landed in #985 with seven other contributions from the same day. Closing in favour of that one.

@ryanio ryanio closed this Aug 22, 2026
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.

3 participants