Skip to main content
This tutorial walks through building a custom programmatic order type — a smart contract that generates CoW Protocol orders based on on-chain conditions. By the end, you’ll have a working TradeAboveThreshold handler that automatically sells tokens when a wallet’s balance exceeds a threshold.

What You’ll Build

A TradeAboveThreshold 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

Create remappings.txt:

Step 2: Implement the Handler

Create src/TradeAboveThreshold.sol:

Key Design Decisions

Step 3: Add Price Protection (Optional)

For production, add an oracle to set a reasonable buyAmount:

Step 4: Write Tests

Create test/TradeAboveThreshold.t.sol:
Run tests:

Step 5: Deploy

Note the deployed address — you’ll need it to create orders.

Step 6: Create an Order

Use the ComposableCoW contract to register your programmatic order:

Step 7: Verify Watch Tower Picks It Up

After the creation transaction confirms, the Watch Tower should detect the ConditionalOrderCreated event and start polling your handler. Check the Watch Tower API:
If the order doesn’t appear, see Debugging Programmatic Orders for troubleshooting.

Error Handling Best Practices

Use specific revert errors to guide the Watch Tower:

Architecture

Next Steps

Last modified on March 12, 2026