HomeKnowledge BaseCoW Hooks: You are in control!

CoW Hooks: You are in control!

8 min read

CoW Protocol is proud to announce the launch of CoW Hooks. You can now implement custom-coded DeFi actions that execute directly before and/or after your trades. CoW Hooks can be used to chain together complex actions such as trading, bridging, staking, depositing, and much more.

The best part? CoW Hooks execute your entire sequence as a single transaction and, true to the CoW Protocol model, you pay gas fees in your sell token if, and only if, your transaction succeeds.

If you are interested in #BUIDLing, reach out, grants are available for great ideas @ https://grants.cow.fi/

Background

In recent months, the crypto industry has been absorbed in discussions on “intent” messages on Ethereum and the new possibilities that this innovation unlocks.

So far, the only production-ready intents system live on Ethereum is CoW Swap. The rest has only been theory and research papers covering the possibilities of intents. What’s more, these papers frame the user transaction as an “intent” but they don’t usually allow for composability. This is why the CoW Swap model asks the user to sign the trade instead of executing it directly on chain… This allows solvers to optimize the order by composing it with other strategies and optimizations. Composability unlocks the real power of intents, as they are meant to abstract the user away from complex interactions.

CoW Swap has been the leader in the intent space for a while and has built one of the most widely-used intent-based models. On CoW Swap, users delegate their trade execution to a network of solvers that complete the transaction on their behalf. This delegated trading model makes way for composability as solvers can batch trades together, find Coincidence of Wants (CoW) matches, update the trading path at the moment of execution (rather than the moment of creation) and more.

Today, CoW Swap is proud to announce the launch of an overhauled intents system that allows for composability of a wide range of actions before and after a swap.

It’s called CoW Hooks.

What are CoW Hooks?

1-cow-hooks.webp

CoW Hooks are arbitrary Ethereum “calls” that attach to an order and execute before and/or after the order. Developers and advanced traders can use code to express an intent that performs some action before a swap (pre-hooks) and after a swap (post-hooks).

Pre-hooks can be used to “set up” conditions for an order. Examples include executing the code necessary for validating an on-chain signature, or setting the required approvals through an EIP-2612 permit.

Post-hooks execute after a swap takes place and the receiver address receives the funds. Post-hooks provide an opportunity to put the funds to use immediately through staking, liquidity providing, bridging tokens to L2s, and much more.

How do CoW Hooks work?

CoW Hooks let you do anything you can imagine. You can use whatever you can encode on-chain as a pre or post hook. The only requirement is that your action includes a trade on CoW Swap.

With CoW Hooks, users define:

  • A list of pre-hooks intents
  • The swap intent
  • A list of post-hooks intents

The flexibility of creating CoW Hooks opens up a world of possibilities — all you need to do is encode your actions in the right order for your particular use-case.

Fresh Address Receives tokens: Permit, Swap & Bridge

Let’s say that you receive USDC and you would like to bridge the funds to another chain. You can use CoW Hooks to execute a token approval, swap, and bridge all at once.

  • Pre-hooks: Use a pre-hooks action to approve the token for a swap
  • Swap: Use CoW Swap to swap your USDC to the token you would like
  • Post-hooks: Send your new tokens to the bridge contract as a post-hook

And just like that, you have received, swapped, and bridged your tokens to a different chain all in one set of transactions. As a bonus, all of your gas fees will be charged in your sell token (USDC in this case,) which means that you can move your received funds without first sending ETH to cover gas costs.

If you are interested in learning more about the code behind this example, we’ve created a walkthrough on how to reproduce this exact set of actions in our CoW Hooks documentation, as well as on YouTube:

Flash Loan payback adapter for CDPs via Stop loss orders

You can also use CoW Hooks to repay debt or manage collateral levels. In this case, your hooks actions might look like this:

  • Pre-hooks: Approve the tokens you would like to swap
  • Swap: Use CoW Swap to swap for the tokens you need to service your debt
  • Post-hooks: Repay your debt and close your position, or use your tokens to add collateral so as to not be undercollateralized

We’ve also put together a walkthrough for how to code this set of hooks, you can view it here:

Use CoW Hooks for everything

The examples illustrated above are just some of the uses of DeFi assets, but the possible use-cases for CoW Hooks are really endless. Here are some more examples:

  • Repaying a debt & staking: You could use CoW Hooks to repay a debt, close a position, swap assets for a yield bearing token, and stake your tokens all-in-one. To do this, you would set your pre-hook action to repaying the debt and closing the position, then you would use CoW Swap to swap your assets, and finally use a post-hook to deposit your new assets into a staking vault.
  • Opening an LP position: You can also work with only a pre-hook and a swap or only a post-hook and a swap. For example, if you wanted to open an LP position, you could first swap some assets for ETH, and then use a post-hook for the rest. If you wanted to do this via a CDP, you could deposit your ETH as collateral, borrow stablecoins, swap some stables to the other LP token side, and open a liquidity provider position.
  • JIT (Just-in-time) smart orders: Say that you want to leverage the benefits of programmable smart contract wallets to deploy a Safe and also trade via the composable CoW framework (ERC-1271). Using CoW Hooks, you can express the entirety of your transaction sequence at no cost. Connect the deployment of the Safe, execute your custom smart contract approval and swap, and even add on another post-swap action all in one. The entire sequence is conditional on every step being able to execute, otherwise none of it goes through.
  • NFT sniping & selling: CoW Hooks also come in useful in the NFT world. You can, for example, sell an NFT and encode a CoW Hooks action to immediately swap those funds for a token of your choice. Or, alternatively, you could place a limit order to purchase crypto at a certain price and then buy an NFT directly after.
  • Bridging to another chain: If you want to swap some tokens and bridge them to another chain, you could use CoW Hooks. In your flow, you would first swap your tokens and then send them to a bridge contract in order to combine both the approval and the bridging.
  • Claiming airdrops: While they’re not as common as they used to be, airdrops are a core part of many Ethereum projects. You can use CoW Hooks to sell your airdrops without spending any ETH on gas. Simply approve the token in your wallet and add a swap transaction to sell it. Your gas fees will be taken in the sell token, so you can enjoy holding on to your precious ETH.
  • Unstaking and restaking: If you’re an Ethereum validator and you want to claim your rewards, you’ll first need to unstake your 32 ETH in order to perform any action with them. With CoW Hooks, you can unstake your ETH, approve and swap to a token of your choice, and then re-stake your new assets somewhere else, all in a single set of encoded transactions.
  • Automatically growing your LP position: Let’s say that you’re a liquidity provider for a EUR — USDC pool on Gnosis chain that rewards you in GNO tokens. You can use CoW Hooks to automatically convert your GNO to EUR/USDC in a 50/50 split through CoW Swap and then use each of the assets to increase your LP position.

What are you going to build with this anon?

The best part about CoW Hooks? They’re fully-featured and already live at the protocol level! So, if you are a developer or an advanced technical user, get your wallets ready and step into a new era in DeFi!

To get started, check out our documentation. We’re really excited about all the possibilities with CoW Hooks so if you need help implementing them or have an exciting idea, please reach out on Twitter or by joining CoW Protocol Discord.

Happy hooking!