Overview
Swap Guards provide an additional layer of security and control over conditional orders by restricting which orders can be settled via CoW Protocol. They implement theISwapGuard interface and are called during order verification to enforce custom restrictions.
Swap Guards are optional. If no guard is set for an owner, all authorized conditional orders can be settled without restrictions.
How Swap Guards Work
When a conditional order is being verified for settlement, ComposableCoW checks if the owner has set a swap guard. If one exists, the guard’sverify function must return true for the order to proceed.
false, the order verification fails with a SwapGuardRestricted error.
ISwapGuard Interface
TheISwapGuard interface is minimal and flexible, allowing for various restriction implementations:
Parameters
- order: The complete
GPv2Order.Datastruct containing all order details (tokens, amounts, receiver, etc.) - ctx: The context identifier -
bytes32(0)for merkle tree orders, orH(params)for single orders - params: The conditional order parameters including handler address, salt, and static input
- offchainInput: Dynamic off-chain data that may be used in order generation
BaseSwapGuard
TheBaseSwapGuard abstract contract provides ERC165 interface support, making it easier to implement custom guards:
Example Implementation: ReceiverLock
TheReceiverLock guard ensures that orders can only settle funds back to the owner’s Safe (not to external addresses):
In GPv2 orders,
address(0) for the receiver is a special value meaning “self” - the owner receives the bought tokens.Setting a Swap Guard
Deploy your guard contract
Deploy your custom
ISwapGuard implementation or use an existing one like ReceiverLock.Call setSwapGuard
Call
ComposableCoW.setSwapGuard(ISwapGuard swapGuard) from your Safe to enable the guard.Removing a Swap Guard
To remove a swap guard and allow unrestricted settlements again:Use Cases
Receiver Restrictions
Prevent funds from being sent to external addresses (like
ReceiverLock)Token Whitelisting
Only allow trading specific token pairs
Time-based Controls
Restrict settlements to specific time windows
Amount Limits
Enforce maximum trade sizes or daily volume limits
Custom Swap Guard Example
Here’s an example of a guard that restricts settlements to specific hours:Integration with Order Flow
Swap guards are checked at two key points:- During
getTradeableOrderWithSignature: Before returning an order to watchtowers - During
isValidSafeSignature: When CoW Protocol verifies the order signature
Next Steps
Value Factories
Learn about on-chain value determination for orders
Custom Orders
Implement your own conditional order types