Repository navigation
Conversation
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>
- 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>
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.
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.makingAmountData/takingAmountDataFusionTwap[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 ownpreInteractionFusionTwap[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 FusionTwappostInteractionmakerTraitsTwapEndedonce the last interval is over, whatever the order's expiry.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 noannouncedAt, and limit-order-protocol#444 proposes rolling it back onmain.IOrderRegistratorand its mock mirror feat: announcement-anchored Dutch auction with fill-size pricing (standalone extension) #223.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-characterscripts/lint fix #223 also carries; andchore: 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 commit2eaa5cf.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 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.
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. 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.jsempties 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 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.
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.
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
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.jsVerified on staging
test/FusionTwap.js, 32 tests: schedule (not before start, unlocked-parts cap with exact revert amounts, last part until the end thenTwapEnded, 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.FusionTwapcoverage: 100% lines, statements and functions, 94% branches.yarn lintpasses.Risk Assessment
Risk Level:
Risks & Impact
scripts/twap/is research tooling only.AuctionDetailslayout (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.OrderRegistratorwithannouncedAt; without one, anchored orders cannot be filled.fusion-sdkrejects these orders today (FusionExtension.fromExtensionrequires the getters to start with the settlement address), and resolver bots need the part window and unlocked amount before production use.