Skip to main content

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

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

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:
  1. Set the order’s receiver to the user’s CoW Shed account
  2. Tokens land in the CoW Shed proxy after the swap
  3. The post-hook calls the CoW Shed proxy to execute pre-signed operations (stake, bridge, deposit, etc.)
If your post-hook needs to act on received tokens (staking, bridging, depositing into a protocol), you must use CoW Shed. Without it, the tokens will be in the user’s wallet but the hook will execute without access to them.
For full CoW Shed documentation, see:

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
Last modified on March 12, 2026