# CoW Protocol

### 1. Providing GPv2Settlement address in the RFQ

From the perspective of Liquorice, trade is performed with the CoW’s GPv2Settlement contract, not directly with the trader.

Therefore, a solver must first and foremost provide the [address of the CoW's GPv2Settlement smart contract](https://docs.cow.fi/cow-protocol/reference/contracts/core#deployments) in both trader and effectiveTrader fields of the RFQ message.

* [REST API RFQ](https://liquorice.gitbook.io/liquorice-docs/for-solvers/rest-api)
* [WS API RFQ](https://liquorice.gitbook.io/liquorice-docs/for-solvers/websocket-api)

### **2. Calling** [**GPv2Settlement.settle**](https://docs.cow.fi/cow-protocol/reference/contracts/core/settlement#settle) **function**

CoW settlement signature

```solidity
function settle(
        IERC20[] calldata tokens,
        uint256[] calldata clearingPrices,
        GPv2Trade.Data[] calldata trades,
        GPv2Interaction.Data[][3] calldata interactions
    ) external
```

During the settlement process, funds from the trader are transferred to the GPv2Settlement contract.

### **2.1 Trades argument**

To assemble trades\[] argument solver would need to use data from signed CoW’s trader order and configure flags argument according to the trader's  signed GPv2Order

However, when creating a GPv2Order to pass it for trader's signature, certain flags should comply with the following:

```solidity
bytes32 kind = GPv2Order.KIND_SELL;
bytes32 buyTokenBalance GPv2Order.BALANCE_ERC20;
```

If the selected kind is GPv2Order.KIND\_SELL

Otherwise, if kind is GPv2Order.KIND\_BUY, then

```solidity
bytes32 kind = GPv2Order.KIND_BUY;
bytes32 sellTokenBalance GPv2Order.BALANCE_ERC20;
```

### **2.2 Interactions**

CoW’s GPv2Settlement.settle function receives interactions in the following format:

```solidity
GPv2Interaction.Data[][3] calldata interactions
```

This format reflects that inside CoW’s settlement there are 3 groups of interactions

To interact with LiquoriceSettlement solver should use the second group (index \[]\[1] of array)

Here is what should be contained inside this array:

**Approval**

When performing the trade, Liquorice Balance Manager contract must have a baseToken approval given by the GPv2Settlement contract.

**Interaction with LiquoriceSettlement**

To make interaction with LiquoriceSettlement solver would need to use the following field from API:

* baseToken
* baseTokenAmount
* tx

**Solidity example**

```solidity
struct Transaction {
  address to;
  bytes data;
}

struct Allowance {
  address token;
  address spender;
  uint256 amount;
}

struct LiquoriceRFQQuoteLevel {
  address baseToken;
  uint256 baseTokenAmount;
  Transaction tx;
  Allowance[] allowances;
}

function getCoWInteractions(LiquoriceRFQQuoteLevel calldata quoteLevel) public pure returns (GPv2Interaction.Data[][3] memory) {
  GPv2Interaction.Data[] memory preInteractions = new GPv2Interaction.Data[](0);
  
  uint256 allowancesLength = quoteLevel.allowances.length;
  GPv2Interaction.Data[] memory interactions = new GPv2Interaction.Data[](allowancesLength + 1);
  
  for (uint256 i = 0; i < allowancesLength; i++) {
    interactions[i] = GPv2Interaction.Data(
      quoteLevel.allowances[i].token,
      0,
      abi.encodeWithSelector(IERC20.approve.selector, quoteLevel.allowances[i].spender, quoteLevel.allowances[i].amount)
    );
  }
  
  interactions[allowancesLength] = GPv2Interaction.Data(quoteLevel.tx.to, 0, quoteLevel.tx.data);
  
  GPv2Interaction.Data[] memory postInteractions = new GPv2Interaction.Data[](0);

  return [preInteractions, interactions, postInteractions];
}
```

### **2.3** Remaining **arguments**

Remaining arguments such as signature and order provided as is.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liquorice.gitbook.io/liquorice-docs/for-solvers/settlement/cow-protocol.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
