Skip to main content
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

Set appropriate gas limits for hooks. If a hook runs out of gas, the entire order transaction will fail.

Estimating Gas

Hook Best Practices

Always test your hooks on testnets before production:
Consider whether your hook should fail the entire order:
  • Critical hooks (approvals): Must succeed
  • Optional hooks (staking): Can fail without breaking order
Keep hooks simple to reduce gas costs and failure risk:
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

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

Partner Fee

Configure partner fee collection

Cross-Chain Bridging

Execute cross-chain swaps