Architecture
A settlement comprises of:- A list of traded tokens with their corresponding price in the batch
- A list of trades to execute
- A list of interactions
Guarantees and Invariants
The parameters of the order are verified for validity:- The signature for the order matches the user’s address
- The order is not expired
- The order was not previously filled
- The current prices are equal to or better than what is specified in the order
Trades
Trades contain a description of the users’ orders, a signature for verifying its validity, and the executed amount (for partially fillable orders). The contract decodes the order parameters from the trade.Interactions
Interactions allow solvers to execute arbitrary calls to any on-chain contract. Normally, they are used to interact with other on-chain liquidity providers, for example, to make a swap call to Uniswap.Protocol fee collection
Protocol fee collection
Interactions are also used for accounting / bookkeeping purposes as well. As trades are executed, the Protocol collects a fee from each trade and stores this in the settlement contract (known as internal buffers). At regular intervals, the Protocol withdraws the fees from the settlement contract to the Protocol’s treasury Safe.
Solvers are NOT able to directly access user funds through interactions. There are strict guarantees enforced by the settlement contract over movement of user funds.
Data Types and Storage
GPv2Order.Data struct
The GPV2Order.Data is the one of the most important data structures in the Protocol. It:
- defines the parameters of an order
- is the basis of which the order digest is determined using
EIP-712and subsequently signed by the user
| Field | Description |
|---|---|
sellToken | ERC-20 token sell |
buyToken | ERC-20 token to buy |
receiver | The address that will receive the proceedings of the trade. If this field is address(0) (i.e. the zero address 0x00...0), then the user who signed the trade is going to receive the funds. |
sellAmount | Amount of sellToken that is sold in wei. |
buyAmount | Amount of buyToken that is bought in wei |
validTo | UNIX timestamp (in seconds) until which the order is valid |
appData | Extra information about the order. Not enforced by the smart contract outside of signature verification (may be used for referrals etc). |
feeAmount | Amount of fees paid in sellToken wei |
kind | buy or sell |
partiallyFillable | partially fillable (true) or fill-or-kill (false) |
sellTokenBalance | From where the sellToken balance is withdrawn |
buyTokenBalance | Where the buyToken is deposited |
Balance locations
Balance locations
The
sellTokenBalance and buyTokenBalance fields the keccak256 hash of the balance location. The following table describes the possible values and their meaning:| Value | Description |
|---|---|
erc20 | User’s ERC-20 balance via approvals given to the GPv2VaultRelayer (default) |
external | User’s ERC-20 balance via approvals given to the Balancer vault |
internal | User’s internal Balancer vault balance |
orderUid
The orderUid is a unique identifier for an order. It is 56 bytes and defined as:
orderDigestis theEIP-712digest of theGPv2Order.Datastruct (32 bytes)owneris the address of the order owner (20 bytes)validTois the timestamp until which the order is valid (4 bytes)‖is the concatenation operator
filledAmounts
The filledAmounts mapping stores the amount of an order that has been filled. It is defined as:
orderUid and the value is the amount of the order that has been filled.
Functions
This section will cover the main functions that are used by users and solvers.For users
setPreSignature
This function allows a user to pre-sign an order, which can be used if for some reason the user is unable to sign with eth_sign, EIP-712, or ERC-1271. This is most useful for smart contracts that have neither a private key, nor implement ERC-1271, but still want to use CoW Protocol.
invalidateOrder
This function allows a user to invalidate (cancel) an order:
For solvers
settle
This function is permissioned and can only be called by solvers passing the allow-list authentication. It executes a settlement:
Indexing
Events that are indexed are:Trade- on any tradeInteraction- on any interactionSettlement- which solver executed the settlementOrderInvalidated- when an order is invalidatedPreSignature- when pre-signing or revoking a pre-signed signature
Off-chain
As this is the main contract of CoW Protocol, all the off-chain infrastructure is built around it. This includes:- The Protocol
- Solvers