How It Works
The contract continuously monitors your balance of a specific token. When your balance reaches or exceeds the threshold, it creates a sell order for your entire balance.Data Structure
Parameters
The token to monitor and sell when the threshold is reached
The token to receive in exchange
The address that will receive the bought tokens
The width of the validity bucket in seconds. Orders queried within the same bucket will have the same Recommended values:
validTo timestamp (and thus the same orderUid).The validTo timestamp is calculated as:- 300 (5 minutes)
- 900 (15 minutes)
- 3600 (1 hour)
Shorter periods update the order more frequently, longer periods reduce orderbook spam.
The minimum balance of
sellToken required to trigger the order. When balance >= threshold, the order becomes valid.Example: Set to 1000e18 to trigger when you have at least 1000 tokens (assuming 18 decimals)The IPFS hash of the appData associated with the order
Behavior
Threshold Check
The order checks your current balance:Sell Amount
The order always sells your entire current balance, not just the threshold amount:Market Execution
The order setsbuyAmount = 1 (the minimum non-zero value), which effectively makes it a market order. The actual received amount depends on the solver’s execution.
CoW Protocol solvers compete to provide the best execution. While there’s no explicit limit price, solvers are incentivized to maximize your output to win the batch.
Validity Bucketing
The order uses validity buckets to prevent orderbook spam. Orders queried within the same bucket have identicalorderUid values:
- At 10:07 → validTo = 10:15
- At 10:12 → validTo = 10:15 (same orderUid)
- At 10:16 → validTo = 10:30 (new orderUid)
Order Properties
- Kind: Always sell orders (
KIND_SELL) - Partially fillable: No (
false) - Fee: 0 (limit order)
- Balance: ERC20 balances for both tokens
Validation
The owner’s balance is below the threshold. The order will poll again in the next block.
Example Usage
Example 1: Yield Harvesting
Automatically sell staking rewards when they accumulate:- You earn 10 REWARD tokens per day from staking
- When balance reaches 100 tokens (after ~10 days), order triggers
- Automatically converts all rewards to USDC
- Repeats when balance reaches 100 again
Example 2: DCA Out of Position
Automatically sell tokens as you acquire them:Example 3: Fee Collection
Automatically convert protocol fees to treasury token:Example 4: Low Threshold for Frequent Conversions
Convert tokens frequently with low threshold:Use Cases
Automated Yield Harvesting
- Stake tokens and earn rewards
- Automatically sell rewards when they accumulate
- Convert to stablecoins or other tokens
- No manual intervention needed
Regular Conversions
- Receive payments in one token
- Automatically convert to preferred token
- Maintain desired portfolio composition
- Dollar-cost average out of positions
Fee Management
- Collect protocol fees in various tokens
- Automatically convert to treasury token
- Reduce operational overhead
- Optimize gas costs with threshold-based execution
Stream Liquidation
- Receive token streams (e.g., from Sablier)
- Automatically liquidate streamed tokens
- Convert to desired asset
- Minimize manual transactions
Implementation Details
Contract Address
The Trade Above Threshold conditional order type is deployed at/src/types/TradeAboveThreshold.sol.
Key Function
Validity Bucket Utility
Considerations
Balance Monitoring: The order continuously polls your balance. Once your balance falls below the threshold (e.g., after execution), the order becomes invalid until your balance increases again.
Gas Efficiency: By using a threshold, you avoid creating orders for small amounts. Set your threshold based on gas costs and desired trade frequency.