TradeAboveThreshold handler that automatically sells tokens when a wallet’s balance exceeds a threshold.
What You’ll Build
ATradeAboveThreshold programmatic order that:
- Monitors a token balance in a Safe wallet
- When the balance exceeds a threshold, generates a sell order for the excess
- Automatically repeats until cancelled
Prerequisites
- Foundry installed
- A Safe wallet with the ComposableCoW setup completed (fallback handler + domain verifier)
- Basic Solidity knowledge
Step 1: Project Setup
remappings.txt:
Step 2: Implement the Handler
Createsrc/TradeAboveThreshold.sol:
Key Design Decisions
Step 3: Add Price Protection (Optional)
For production, add an oracle to set a reasonablebuyAmount:
Step 4: Write Tests
Createtest/TradeAboveThreshold.t.sol:
Step 5: Deploy
Step 6: Create an Order
Use theComposableCoW contract to register your programmatic order:
Step 7: Verify Watch Tower Picks It Up
After the creation transaction confirms, the Watch Tower should detect theConditionalOrderCreated event and start polling your handler.
Check the Watch Tower API:
Error Handling Best Practices
Use specific revert errors to guide the Watch Tower:Architecture
Next Steps
- Custom Orders Reference — interface details and advanced patterns
- ComposableCoW Troubleshooting — setup issues
- Debugging Programmatic Orders — when orders don’t execute
- Watch Tower — how the Watch Tower monitors and submits orders