CoWSwapClone
Inherits: IERC1271, Clone
Title: CoWSwapClone
A contract that implements the ERC1271 interface for signature validation and manages token trades. This
contract is designed to be used as a clone with immutable arguments, leveraging the ClonesWithImmutableArgs
library.
The clone should be initialized with the following packed bytes, in this exact order:
sellToken(address): The address of the token to be sold.buyToken(address): The address of the token to be bought.sellAmount(uint256): The amount of the sell token.buyAmount(uint256): The minimum amount of the buy token.validTo(uint64): The timestamp until which the order is valid.receiver(address): The address that will receive the bought tokens.operator(address): The address of the operator allowed to manage the trade. To use this contract, deploy it as a clone using theClonesWithImmutableArgslibrary with the above immutable arguments packed into a single bytes array. After deployment, callinitialize()to set up the necessary token approvals for the trade.
The isValidSignature function can be used to validate the signature of an order against the stored order
digest.
State Variables
_ERC1271_MAGIC_VALUE
CONSTANTS ///
bytes4 internal constant _ERC1271_MAGIC_VALUE = 0x1626ba7e
_ERC1271_NON_MAGIC_VALUE
bytes4 internal constant _ERC1271_NON_MAGIC_VALUE = 0xffffffff
_COW_SETTLEMENT_DOMAIN_SEPARATOR
The domain separator of GPv2Settlement contract used for orderDigest calculation.
bytes32 internal constant _COW_SETTLEMENT_DOMAIN_SEPARATOR =
0xc078f884a2676e1345748b1feace7b0abee5d00ecadb6e574dcdd109a63e8943
_VAULT_RELAYER
Address of the GPv2VaultRelayer. https://docs.cow.fi/cow-protocol/reference/contracts/core
address internal constant _VAULT_RELAYER = 0xC92E8bdf79f0507f65a392b0ab4667716BFE0110
Functions
initialize
Initializes the CoWSwapClone contract by approving the vault relayer to spend the maximum amount of the sell token.
This function should be called after the clone is deployed to set up the necessary token approvals.
function initialize() external payable;
isValidSignature
Validates the signature of an order. The order is considered valid if the order digest matches the stored order digest. Second parameter is not used.
function isValidSignature(bytes32 orderDigest, bytes calldata encodedOrder)
external
view
override
returns (bytes4);
Parameters
| Name | Type | Description |
|---|---|---|
orderDigest | bytes32 | The digest of the order to validate. |
encodedOrder | bytes |
Returns
| Name | Type | Description |
|---|---|---|
<none> | bytes4 | A magic value if the signature is valid, otherwise a non-magic value. |
claim
Claims the sell and buy tokens. Calling this function before the trade has settled will cancel the trade. Only the operator or the receiver can claim the tokens.
function claim() external payable returns (uint256 claimedSellAmount, uint256 claimedBuyAmount);
Returns
| Name | Type | Description |
|---|---|---|
claimedSellAmount | uint256 | The amount of sell tokens claimed. |
claimedBuyAmount | uint256 | The amount of buy tokens claimed. |
sellToken
Returns the address of the sell token.
function sellToken() public pure returns (address);
Returns
| Name | Type | Description |
|---|---|---|
<none> | address | The address of the sell token. |
buyToken
Returns the address of the buy token.
function buyToken() public pure returns (address);
Returns
| Name | Type | Description |
|---|---|---|
<none> | address | The address of the buy token. |
sellAmount
Returns the amount of sell tokens.
function sellAmount() public pure returns (uint256);
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | The amount of sell tokens. |
minBuyAmount
Returns the amount of buy tokens.
function minBuyAmount() public pure returns (uint256);
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | The amount of buy tokens. |
validTo
Returns the timestamp until which the order is valid.
function validTo() public pure returns (uint32);
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint32 | The timestamp until which the order is valid. |
receiver
Returns the address of the receiver.
function receiver() public pure returns (address);
Returns
| Name | Type | Description |
|---|---|---|
<none> | address | The address of the receiver. |
operator
Returns the address of the operator who can claim the tokens after the trade has settled. The operator can also cancel the trade before it has settled by calling the claim function before the trade has settled.
function operator() public pure returns (address);
Returns
| Name | Type | Description |
|---|---|---|
<none> | address | The address of the operator. |
Events
CoWSwapCloneCreated
EVENTS ///
Emitted when a new order is created.
event CoWSwapCloneCreated(
address indexed sellToken,
address indexed buyToken,
uint256 sellAmount,
uint256 minBuyAmount,
uint32 validTo,
address indexed receiver,
address operator
);
Parameters
| Name | Type | Description |
|---|---|---|
sellToken | address | The address of the token to be sold. |
buyToken | address | The address of the token to be bought. |
sellAmount | uint256 | The amount of the sell token. |
minBuyAmount | uint256 | The minimum amount of the buy token. |
validTo | uint32 | The timestamp until which the order is valid. |
receiver | address | The address that will receive the bought tokens. |
operator | address | The address of the operator allowed to manage the trade. |
OrderClaimed
Emitted when an order is claimed.
event OrderClaimed(address indexed operator, uint256 claimedSellAmount, uint256 claimedBuyAmount);
Parameters
| Name | Type | Description |
|---|---|---|
operator | address | The address of the operator who claimed the order. |
claimedSellAmount | uint256 | The amount of sell tokens claimed. |
claimedBuyAmount | uint256 | The amount of buy tokens claimed. |
Errors
CallerIsNotOperatorOrReceiver
ERRORS ///
Thrown when the caller is not the operator or receiver of the order.
error CallerIsNotOperatorOrReceiver();