HomeKnowledge BaseAll you need to know about CoW Swap’s new Safe fallback handler

All you need to know about CoW Swap’s new Safe fallback handler

5 min read

Don’t worry, trade happy! CoW Swap has been busy cooking up Conditional Orders, an architecture that allows developers (and their users) to create sophisticated orders of all kinds. At the core of this system is a flexible smart contract wallet that allows the protocol to implement its unique logic. What’s better than standing on the shoulders of giants in the ecosystem and within the Gnosis family Safe?

Wait? Smart contract wallet?

That’s right: CoW Swap pioneered intents across the Ethereum space for EOAs (think MetaMask, Trezor, Trust Wallet, etc.), but what’s less well-known is that the protocol can actually work in a similar way for smart contracts by relying on a flexible intents system with the ERC-1271 integration. It sounds technical, but this standard is basically designed to allow smart contracts to “Sign” a message like any other EOAs. This is a familiar procedure for the seasoned CoWmunity, but Conditional Orders now allow anyone to define a set of conditions and have the smart contract wallet do the clicking for you.

Incubated through the CoW Grants Program, and supported by the Safe team throughout the testing/auditing process, the ExtensibleFallbackHandler is now live. Sounds complicated, we know! Put simply, this is a way to add extra powers to your Safe, while preserving the strong security guarantees that the Safe is well known for.

Upgrading your Safe’s fallback handler is a secure process, all you have to do is tick a box when placing an order, and your Safe upgrade will be batched with your order, making for a single-transaction order placement and upgrade. The contracts that implement this system have been audited both by an independent auditor, and an external auditor from Ackee Blockchain (You can read the final audit reports here).

The owl-eyed among you would be aware that smart contracts have always been able to use CoW Protocol, though this required both a transaction on chain to signal your intent, and that the orders be fixed (not conditional). Wave goodbye to these limitations!

With an ExtensibleFallbackHandler+ComposableCoW-enabled Safe you now benefit from all the CoW Swap features directly from your smart contract wallet: MEV protection, no failed swap transactions, at-execution path discovery, conditional orders, and batching if you need to do pesky things like approve or wrap ETH.

If you’re technically inclined, check out how you can start building with conditional orders using ComposableCoW, or drop into #tech-talk on our discord! While you’re busy reading up on how to write the latest and greatest conditional order, you could also be gradually buying up more of your favorite token using TWAP!

What is a Safe fallback handler?

Long story short, Safe is designed to have strong security guarantees over basic operations (think executing transactions, spending your precious tokens, etc.), but there’s not enough time to program all functionality that a user may require. Therefore, the Safe can “forward” all function requests that it doesn’t recognize to another contract (the fallback handler).

CRITICALLY: Fallback handlers do NOT have the same authority as the Safe from which they were called. Due to the security architecture of Safe, it is not possible for the fallback handler to execute transactions on behalf of the Safe.

OK, the fallback handler can’t execute a transaction for you, so why should you care? Safe doesn’t by default (without a fallback handler) support ERC-1271 signatures. The standard deployment of a Safe deploys with CompatibilityFallbackHandler as the fallback handler, which has limited ERC-1271 support (meaning: it cannot read and thus, cannot be automated). CoW Swap intents super-charge this by allowing a Safe to also specify a smart contract that can sign messages for a particular application on its behalf.

This is where ComposableCoW takes over! 🐮🎶 You can think of ComposableCoW as an autonomous agent, signing orders that meet whatever conditions you have specified. After you create your first conditional order, the second transaction you see in the batch will enable ComposableCoW, which tells your Safe “I want ComposableCoW to be able to sign orders from CoW Protocol”. TWAP is just a conditional order type that works with ComposableCoW.

CRITICALLY: Due to the design of ERC-1271, all signature verification is read-only which means it is impossible for a ComposableCoW or any conditional order to directly access user funds (the movement of funds during a swap is initiated by the settlement contract ONLY).

In summary, to make CoW Swap’s new TWAP order feature work:

  • Safe users need to upgrade their fallback handler(enables conditional signing)
  • Conditional orders are automatically placed in the CoW Protocol order book (now signed with the new handler)
  • Automated signing of orders means you may hear less of CoW’s trademark moo. Feel free to set this as your phone’s notification tone if suffering withdrawal symptoms 🐄

CoW Swap takes security very seriously. The underlying Safe fallback handler code has been fully audited by both the Safe and CoW teams as well as by two independent external auditors, G0 and Ackee Blockchain. You can find all the audit final reports here.

The solution presented follows Safe’s high security standards. What’s more, the fallback handler isn’t just about placing orders. It allows limitless and, as of yet, unknown functionality. Maybe you want to:

  • Define a custom method handler for any non-standard method (if you really want you can turn your Safe into an ERC-20 token)
  • Define a custom EIP-712 domain verifier, whereby a contract implementing ISafeSignatureVerifier can do your menial signing automatically (it is this interface that ComposableCoW implements to power the shiny new TWAP orders)
  • Define a set of methods (interfaceId) that your Safe supports, and report such using ERC-165

The architecture for the ExtensibleFallbackHandler can be viewed here.

NOTE: Audits are not a guarantee that code is free from error. Use with caution.

We wish you much surplus and happy TWAP order trading! Give TWAP orders a try today and let us know what you think by reaching out on Twitter or Discord!