Skip to content

feat: FusionTwap extension to execute a Fusion order as a TWAP (proof of concept) - #233

Open
deacix wants to merge 9 commits into
mainfrom
cursor/fusion-twap-extension-e8ae
Open

deacix wants to merge 9 commits into
mainfrom
cursor/fusion-twap-extension-e8ae

Conversation

@deacix

@deacix deacix commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Change Summary

What does this PR change?

Adds FusionTwap, a standalone, opt-in extension that executes one signed Fusion order as a TWAP: the order unlocks in equal parts, one per interval, every part is sold through a fresh run of the deployed settlement's own Dutch auction, and nothing is fillable after the last interval. It needs no settlement or Limit Order Protocol redeploy and keeps no storage. This is a proof of concept for the TWAP design discussion; it is not audited.

Extension field Target Data
makingAmountData / takingAmountData FusionTwap [settlement][AuctionDetails][fee data][whitelist]. FusionTwap moves the 4-byte auction start time to the current part's start and forwards the call to the settlement's getter, so the curve, gas bump and fees are the settlement's own
preInteraction FusionTwap The schedule: [start uint32][interval uint24][parts uint16]. Caps the cumulative filled amount by the parts unlocked so far (from the remaining amount LOP already tracks), reverts after the last interval, and checks that both getters route to FusionTwap
postInteraction settlement Unchanged: fees, whitelist, access token, surplus, mainnet priority fee cap
makerTraits — Partial and multiple fills; expiry at the schedule end
  • Fail closed. The getters refuse an order whose pre-interaction is missing or points elsewhere, or that uses the bit invalidator. The pre-interaction refuses a fill when either getter bypasses FusionTwap.
  • Hard end. Fills revert with TwapEnded once the last interval is over, whatever the order's expiry.
  • Anchored start (opt-in). With the top bit of the start time set, the schedule starts at the later of the signed start and OrderRegistrator.announcedAt(orderHash); an unannounced order cannot be filled. The registrator is a constructor argument and may be zero; the deployed 4.3.x registrators have no announcedAt, and limit-order-protocol#444 proposes rolling it back on main. IOrderRegistrator and its mock mirror feat: announcement-anchored Dutch auction with fill-size pricing (standalone extension) #223.
  • Missed parts stay unlocked and are sold on the current part's auction at whatever point it has reached, so the auction duration should equal the interval.

Commits, best reviewed one by one: contract, tests and a gas case; fix: make FusionTwap fail closed… (drops the per-fill cap a resolver bypasses by chaining fills, adds the hard end, rejects a zero start, adds the getter routing check); feat: anchor…; parity and random-sequence tests; regenerated docs; the two-character scripts/ lint fix #223 also carries; and chore: add the scripts behind the TWAP charts, the research tooling behind the charts below, which can be dropped before merge.

Related Issue/Ticket: none. Related: #223, #229 and #219 (announcement-anchored auctions); 1inch/swap-vm#191 removed SwapVM's TWAPSwap (PT1-861).

Visual proofs

The TWAP swap, run on-chain

Every chart in this group comes from running the contracts in this PR on Hardhat with yarn hardhat run scripts/twap/chart-data.js, at commit 2eaa5cf.

FusionTwap quotes over 40 minutes read from the contracts: the auction restarts at every part, while one Fusion order with the same auction decays once

What FusionTwap quotes. The getter restarts the settlement's auction at every part, while a plain Fusion order with the same auction decays once and stays at the limit price. All 320 on-chain quotes equal an independent model of the settlement's pricing to the wei.

A 2-hour TWAP in 12 parts executed on-chain by a resolver bot against a simulated market, with the unlocked and filled share of the order below

A whole TWAP swap. A resolver bot quotes the real getters every 12-second block and fills each part where the quote crosses below a simulated market (seeded GBM, σ 60%/yr). It makes 12 fills, on average 15.8 bps below the market. The lower panel shows the filled amount never exceeding what the schedule has unlocked.

A Safe that signs 45 minutes late: with a fixed start five parts sell in one fill 144 bps below the market; with the anchored start the first fill is one part 24 bps below

Why the anchored start exists. Same market, order and bot, with the order signed 45 minutes late. With a fixed start, the five missed parts sell in one fill 144 bps below the market (70.4 bps on average over the order). With the start anchored to the announcement, the schedule starts fresh and the first fill is 24 bps below (19.4 bps on average).

Gas per fill for a plain Fusion partial fill, a FusionTwap part and an anchored FusionTwap part, first and later fills

Gas. A part costs 11.4k more gas than a plain Fusion partial fill, plus 6.1k with the anchored start. Later parts cost about 38k less than the first, because the remaining-amount slot already exists and the signature is not checked again. The resolver keeps a WETH balance in these runs; test/MeasureGas.js empties it and gets a storage refund, so its absolute numbers are about 4.8k lower with the same overhead.

Where each part's auction should start: Monte Carlo

From python3 scripts/twap/simulation.py: 4,000 GBM paths per cell, competitive resolvers filling at the first block where the curve is at most the market price minus their margin (10 bps on Ethereum, 5 bps on L2s).

One 24-hour market path: the curve anchored to the limit price fills every part at its top once the market rallies past it; the ratchet follows the market

One path, two designs. On a 90th-percentile path the market rallies past the static curve's +3% top, so every part fills at the top and the resolver keeps the difference: 34.9 bps per fill. Re-anchoring each part on the previous part's first fill follows the market: 12.5 bps.

Monte Carlo cost by anchoring design for a 24-hour TWAP, and the cost above the resolver margin in every scenario

Across scenarios. The static curve (v1) is fine for short TWAPs and on L2s, but loses up to 69 bps beyond the resolver margin in long, volatile ones. The ratchet stays within 1–9 bps of the margin and the oracle within 2–7.

Monte Carlo: cost of a late start with and without anchoring, cost by starting premium, and cost by part length

Defaults. Without anchoring, a late start costs 159–326 bps on the first fill; with it, 19. A +3% starting premium is the best of those tried for a 4-hour TWAP, and longer parts are cheaper on 12-second blocks (the model has no market impact, which would favour more, smaller parts).

Reproduce

yarn hardhat run scripts/twap/chart-data.js   # on-chain data -> build/twap-charts/onchain.json
python3 scripts/twap/simulation.py            # Monte Carlo, needs numpy -> build/twap-charts/simulation.json
python3 scripts/twap/charts.py                # charts, needs matplotlib -> build/twap-charts/*.png

Testing & Verification

How was this tested?

  • Unit tests

  • Integration tests

  • Manual testing (describe steps): the on-chain charts above, produced by scripts/twap/chart-data.js

  • Verified on staging

  • test/FusionTwap.js, 32 tests: schedule (not before start, unlocked-parts cap with exact revert amounts, last part until the end then TwapEnded, hard end with no expiry); pricing (top of a fresh auction, restart every part, fills by taking amount, missed parts on the current auction); settlement behaviour kept (fees, access token, whitelist, gas bump on a part's auction, mainnet priority fee cap); fail-closed cases (no or foreign pre-interaction, a getter that bypasses FusionTwap, single-fill order, missing auction details, five invalid schedules); anchoring (unannounced, late announcement starts a fresh schedule and moves the end, early announcement keeps the signed start, no registrator); maker cancellation; an ERC-1271 maker that signs once; three seeded random fill sequences checked fill by fill against an independent model of the settlement's pricing.

  • yarn test:ci: 149 passing. FusionTwap coverage: 100% lines, statements and functions, 94% branches. yarn lint passes.

Risk Assessment

Risk Level:

  • Low - Minor changes, no operational impact
  • Medium - Moderate changes, limited impact, standard rollback available
  • High - Significant changes, potential operational impact, complex rollback

Risks & Impact

  • New, separately deployed contract that orders opt into; no deployed contract or existing code path changes. scripts/twap/ is research tooling only.
  • Part auctions are anchored to the signed limit price, like any Fusion order. In a trend beyond the top premium, parts fill at the top of the curve (see the charts above); re-anchoring each part is the natural v2.
  • The getter depends on the settlement's AuctionDetails layout (start time at bytes 7..11). Bit 31 of the schedule's start is the anchor flag, so a non-anchored start must be before 2038, the same convention as [PT1-785] Feature/anchored minimal #229.
  • Anchoring needs an OrderRegistrator with announcedAt; without one, anchored orders cannot be filled.
  • fusion-sdk rejects these orders today (FusionExtension.fromExtension requires the getters to start with the settlement address), and resolver bots need the part window and unlocked amount before production use.
Open in Web Open in Cursor 

cursoragent and others added 8 commits November 4, 2026 03:44
One signed order unlocks in equal parts, one per interval. As the order's
pre-interaction, FusionTwap caps the cumulative filled amount by the parts
unlocked so far, using the remaining amount the limit order protocol already
tracks. As the amount getter in front of the settlement's getter, it restarts
the settlement's Dutch auction at the start of the current part by rewriting
the auction start time it forwards. It keeps no storage and needs no
settlement redeploy.

Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
… makers

Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
- Drop the per-fill part cap. A resolver can chain fills in one transaction,
  so it never limited how much of a backlog sells on one auction.
- Revert with TwapEnded once the last interval is over, independently of the
  order's expiry, so a remainder cannot sit at the limit price indefinitely.
- Reject a zero start time, which unlocked every part at once.
- Check in the pre-interaction that both amount getters route to FusionTwap,
  so an order whose other getter bypasses it cannot be filled through it.

Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
With the top bit of the schedule's start time set, the schedule starts at the
later of the signed start and OrderRegistrator.announcedAt(orderHash), and an
order that was never announced cannot be filled. A Safe that signs late then
starts a fresh schedule instead of selling every missed part on whatever
point the current auction has reached.

The registrator is a constructor argument and may be zero where no registrator
with announcedAt is deployed. IOrderRegistrator and its mock mirror the ones
in #223.

Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
… fee cap and random fills

- The settlement's gas bump still applies on a part's auction.
- The mainnet Settlement's priority fee cap still applies to TWAP fills.
- Seeded random fill sequences never exceed the unlocked parts, reject every
  over-fill with the exact amounts, and pay exactly the current part's curve
  price as computed independently from the settlement's formulas.

Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
They keep the lint job red on main and on every branch; the same fix is in #223.

Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
@cursor
cursor Bot requested a review from krboktv November 4, 2026 04:30
- scripts/twap/chart-data.js runs FusionTwap and SimpleSettlement on Hardhat:
  getter quotes over time (checked against the pricing model), a resolver
  executing a TWAP against a seeded market path, a late start with and
  without announcement anchoring, and the gas of each kind of fill.
- scripts/twap/simulation.py is the Monte Carlo study of where each part's
  auction should start.
- scripts/twap/charts.py renders both into PNG charts under build/.

Co-authored-by: Sergej Kunz <s.kunz@deacix.com>
@cursor
cursor Bot requested a review from ifelsedeveloper November 4, 2026 04:58
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.

2 participants