Hooks
CoW Protocol supports order hooks - custom smart contract calls that execute before (pre-hooks) or after (post-hooks) your order settlement. This enables advanced use cases like token approvals, flash loans, and complex DeFi interactions.Overview
Hooks allow you to:- Execute pre-order logic: Approve tokens, wrap/unwrap assets, or setup flash loans
- Execute post-order logic: Stake tokens, compound yields, or trigger other contracts
- Chain complex operations: Combine multiple DeFi protocols in a single transaction
- Integrate with dApps: Enable seamless integration with other protocols
Hook Structure
Hooks are defined in the order’s app data and consist of:Adding Hooks to Orders
Basic Hook Setup
Using TradingSdk with Hooks
Pre-Hooks
Pre-hooks execute before the order settlement.Token Approval
Wrap Native Token
Flash Loan Setup
Post-Hooks
Post-hooks execute after the order settlement.Stake Tokens
Deposit to Yield Protocol
Unwrap Wrapped Token
Complete Example
Here’s a complete example with pre and post hooks:Gas Limit Considerations
Recommended Gas Limits
Estimating Gas
Hook Best Practices
Test hooks thoroughly
Test hooks thoroughly
Always test your hooks on testnets before production:
Handle failures gracefully
Handle failures gracefully
Consider whether your hook should fail the entire order:
- Critical hooks (approvals): Must succeed
- Optional hooks (staking): Can fail without breaking order
Minimize hook complexity
Minimize hook complexity
Keep hooks simple to reduce gas costs and failure risk:
Use dappId for tracking
Use dappId for tracking
Always include a
dappId to identify your hooks:Use Cases
1. Automated Yield Farming
Swap and immediately deposit into yield protocol:2. Leveraged Trading
Flash loan, swap, and repay:3. Cross-Chain Preparation
Prepare tokens for bridging:Debugging Hooks
Check Hook Execution
Post-Hooks and CoW Shed
Post-hooks that need to act on the tokens received from a swap require special handling. By default, bought tokens are sent to the user’s wallet — but the post-hook executes in the context of the HooksTrampoline contract, not the user’s wallet. This means a post-hook cannot directly access the user’s received tokens. The solution is CoW Shed — a deterministic smart account (proxy) that acts as an intermediary:- Set the order’s
receiverto the user’s CoW Shed account - Tokens land in the CoW Shed proxy after the swap
- The post-hook calls the CoW Shed proxy to execute pre-signed operations (stake, bridge, deposit, etc.)
- Account Abstraction Guide — setup, signing, and execution
- CoW Shed SDK Reference — complete API reference
Limitations
- Hooks must complete within their specified gas limit
- Hooks are executed in order (pre-hooks -> order -> post-hooks)
- Failed hooks will cause the entire transaction to revert
- Hooks cannot be updated after order submission
- Maximum gas for all hooks combined is subject to block gas limit
Next Steps
Account Abstraction (CoW Shed)
Pre-authorize post-hook operations with CoW Shed
Cross-Chain Bridging
Execute cross-chain swaps with post-hook bridging
Hook Store
Browse pre-built hooks or publish your own hook dApp
Hooks: Getting Started
Create your first hook step-by-step using CoW Swap
Hook dApp Development
Build custom hook dApps for the CoW Swap interface
Partner Fee
Configure partner fee collection