feat: add MetaSwap flexible settlement enforcer - #204
Open
hanzel98 wants to merge 1 commit into
Open
Conversation
hanzel98
force-pushed
the
feat/metaswap-flexible-settlement-enforcer
branch
4 times, most recently
from
September 3, 2026 01:14
b1dee69 to
ff660b3
Compare
Authorize one open-route MetaSwap settlement with exact input constraints, signed approval flexibility, minimum output, and atomic one-shot consumption.
hanzel98
force-pushed
the
feat/metaswap-flexible-settlement-enforcer
branch
from
September 3, 2026 13:52
ff660b3 to
7995fa6
Compare
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.
Summary
MetaSwapFlexibleSettlementEnforcerfor one successful MetaSwap settlement with an exact signed input and redeemer-selected route.SkipApproval,approve(amount), andapprove(0) + approve(amount)through an exactApprovalModeenum.Behavior
One signed delegation authorizes one direct
BATCH_DEFAULT_MODEsettlement matching its selected mode:Terms bind the MetaSwap target, input token and amount, exact approval mode, output token, recipient, and minimum output:
The enum values are
None = 0,SkipApproval = 1,Approve = 2, andResetApprove = 3. Native input requiresNone; ERC-20 input requires exactly one of the other modes.SkipApprovaldoes not call or validateallowance().aggregatorIdand routedataremain flexible and are supplied by the redeemer.Approval spender and swap input token arguments must be canonical complete ABI address words with zeroed upper bytes. Swap calldata must be at least 196 bytes, covering the selector, four-word head, and two dynamic length words required by
swap(string,address,uint256,bytes).State and gas
A boolean mapping keyed by
(DelegationManager, delegationHash)permanently records successful use. A separate mapping temporarily stores the recipient's raw pre-execution balance and is deleted after validation for a storage refund. Failed settlements revert atomically, including both writes, and remain retryable. Identical delegation hashes are isolated across DelegationManager contracts.Prototype benchmarks against the equivalent three-caveat composition measured approximately 29-36% lower estimated charged redemption gas after applying the balance-snapshot storage refund. The production implementation also emits
SettlementConsumedfor indexable fills.Security
The delegator trusts the configured MetaSwap contract, its adapters, and the delegate-provided route because swap route data is intentionally unrestricted. The enforcer binds the exact approval shape, approval spender and amount, swap target, input token and amount, and minimum output.
The enforcer cannot prevent arbitrary route side effects or protect unrelated assets already approved to MetaSwap or its adapters. Unrelated transfers, rebases, or dishonest token balance reporting may satisfy the output check. A residual input allowance may remain if MetaSwap spends less than the approved amount. Input and output tokens must differ.
Deployment and verification
Deploy with
script/DeployCaveatEnforcers.s.soland verify through the existingscript/verification/verify-enforcer-contracts.shflow. No dedicated script or environment variable is introduced.Test plan
4..255aggregatorIdand route dataLINEA_RPC_URLandARBITRUM_RPC_URLNote
Medium Risk
New on-chain path for MetaSwap swaps and ERC-20 approvals under delegation; route data stays unconstrained and output checks are balance-delta based, so delegators must trust MetaSwap/adapters and understand documented side-effect and token-behavior limits.
Overview
Adds
MetaSwapFlexibleSettlementEnforcer, a single caveat that authorizes one successful MetaSwap settlement per signed delegation inBATCH_DEFAULT_MODE, with redeemer-chosenaggregatorIdand routedatabut fixed swap target, input token/amount, output token, recipient, and minimum net balance increase.Signed 145-byte packed terms include an
ApprovalModethat pins the exact batch shape: native-only swap with value, ERC-20 swap only (SkipApproval),approve+ swap, or reset +approve+ swap.beforeHookstructurally validates each execution (canonical approve words, minimumswapcalldata length) and sets a per-(DelegationManager, delegationHash)consumed flag while snapshotting the recipient’s pre-execution output balance.afterHookrequires the balance delta to meettokenOutMin, emitsSettlementConsumed, and clears the snapshot; failed redemptions revert atomically so the settlement stays retryable until a successful fill.Also wires deployment in
DeployCaveatEnforcers.s.sol, documents behavior and trust assumptions inCaveatEnforcers.md, and adds a broad Foundry test suite (modes, validation edge cases, end-to-end redemption, replay prevention).Reviewed by Cursor Bugbot for commit 7995fa6. Bugbot is set up for automated code reviews on this repo. Configure here.