Generalized Wrappers
Generalized wrappers are smart contracts that enable custom logic to execute before and after CoW Protocol settlement operations. This reference documents the contract interfaces, implementation patterns, and on-chain behavior of the wrapper system.
Architectureโ
Execution Flowโ
Key Design Principlesโ
- Abstract: There are very few limitations on what a wrapper can/can't do around a settlement transaction
- Efficient Encoding: Wrapper-specific data appended to minimize gas overhead
- Nested Support: Multiple wrappers chain by encoding addresses sequentially, allowing CoW orders
- Authentication: Only allowlisted wrappers can call settlement contract
Implementationโ
Developers looking to integrate using wrappers should copy this all-in-one solidity file into their project as vendored dependency.
It provides a few base contracts which can serve as the foundation for integration:
ICowWrapper: Core interface all wrappers must implementCowWrapper: Abstract base contract providing security and utilities that all wrappers should use
See the code for CowWrapper.sol on GitHub.
Quick Start Exampleโ
The EmptyWrapper serves as a good starting point for building a wrapper. Following this, the implementation functions should be filled in as described below.
Virtual Functions for integratorsโ
_wrapโ
Contains custom surrounding settlement logic. Must call _next() to continue the chain to the settlement contract.
function _wrap(bytes calldata settleData, bytes calldata wrapperData, bytes calldata remainingWrapperData) internal virtual;
Implementation Requirements:
- Parse wrapper-specific data from
wrapperDataas required - Execute pre-settlement logic
- Call
_next(remainingWrapperData)to continue chain - Execute post-settlement logic
Example:
function _wrap(bytes calldata settleData, bytes calldata wrapperData, bytes calldata remainingWrapperData) internal override {
// 1. Parse data
(address token, uint256 amount) = abi.decode(wrapperData, (address, uint256));
// 2. Pre-settlement logic. Example, receive tokens from user
IERC20(token).transferFrom(msg.sender, address(this), amount);
// 3. Continue chain (REQUIRED)
_next(settleData, remainingWrapperData);
// 4. Post-settlement logic. Example: stake tokens to a contract (for swap and stake)
stakeTokens(token, amount);
}
validateWrapperDataโ
Validates wrapper-specific data. Must be deterministic and revert on invalid input.
function validateWrapperData(
bytes calldata wrapperData
) external view override;
Requirements:
- Deterministic: Same input must always produce same output. This means you cannot check the timestamp and revert if the order is valid, for example.
- View function: Cannot modify state
- Revert on invalid: Revert if data is malformed
Internal Functions (Provided for integrator use)โ
_nextโ
Continues the wrapper chain or calls settlement. Handles all parsing and routing automatically.
function _next(bytes calldata settleData, bytes calldata remainingWrapperData) internal;
Behavior:
- Extracts the next target address (first 20 bytes of
remainingWrapperData) - Calls the next wrapper via
wrappedSettle(), or the settlement contract viasettle()if no wrappers remain
Calldata Encoding Specificationโ
The chainedWrapperData parameter of wrappedSettle(settleData, chainedWrapperData) is specially encoded to minimize bit shuffling overhead:
โโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโโโ
โ Lenโ โ Dataโ โ Addrโ โ Lenโ โ Dataโ โ
โ(2 B) โ (Lenโ) โ (20 B) โ(2 B) โ (Lenโ) โ
โโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโดโโโโโโโโโโโ
โ<โโโโโโโโโโโ chainedWrapperData โโโโโโโโโโโโโ>โ
Components:
- Lenโ: 2-byte (uint16) length of wrapper 1's data
- Dataโ: Wrapper 1's custom data
- Addrโ: 20-byte address of wrapper 2
- Lenโ: 2-byte (uint16) length of wrapper 2's data
- Dataโ: Wrapper 2's custom data ... (repeat Addrโ, Lenโ, Dataโ for every subsequent wrapper)
Wrapper Public Functionsโ
wrappedSettleโ
Entry point for wrapper execution. Validates caller authentication and delegates to _wrap()--where integrators place their custom logic. See the full implementation in CowWrapper.sol.
Parameters:
settleData: OriginalGPv2Settlement.settle(...)calldatachainedWrapperData: Encoded wrapper chain โ see Calldata Encoding Specification
CowWrapperHelpersโ
A view-only periphery contract for validating and encoding wrapper chains. See the source code for the complete implementation.
Deploymentsโ
The contract is deployed via CREATE2:
| Environment | Address |
|---|---|
| Production | 0x8C8f88F3081A7d54fCbC2Bd7C6Cf5E9Fc26e30E9 |
| Staging | 0xfd1a35C8E7AEC58a4bB6AeDca0B2Bf9c9Be9E07F |
CowWrapperHelpers may not yet be deployed on all networks supported by CoW Protocol.