CoWSwapClone

Git Source

Inherits: IERC1271, Clone

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.
  • operator (address): The address of the operator allowed to manage the trade.
  • receiver (address): The address that will receive the bought tokens. To use this contract, deploy it as a clone using the ClonesWithImmutableArgs library with the above immutable arguments packed into a single bytes array. After deployment, call initialize() 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

NameTypeDescription
orderDigestbytes32The digest of the order to validate.
encodedOrderbytes

Returns

NameTypeDescription
<none>bytes4A 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

NameTypeDescription
claimedSellAmountuint256The amount of sell tokens claimed.
claimedBuyAmountuint256The amount of buy tokens claimed.

sellToken

Returns the address of the sell token.

function sellToken() public pure returns (address);

Returns

NameTypeDescription
<none>addressThe address of the sell token.

buyToken

Returns the address of the buy token.

function buyToken() public pure returns (address);

Returns

NameTypeDescription
<none>addressThe address of the buy token.

sellAmount

Returns the amount of sell tokens.

function sellAmount() public pure returns (uint256);

Returns

NameTypeDescription
<none>uint256The amount of sell tokens.

minBuyAmount

Returns the amount of buy tokens.

function minBuyAmount() public pure returns (uint256);

Returns

NameTypeDescription
<none>uint256The amount of buy tokens.

validTo

Returns the timestamp until which the order is valid.

function validTo() public pure returns (uint32);

Returns

NameTypeDescription
<none>uint32The timestamp until which the order is valid.

receiver

Returns the address of the receiver.

function receiver() public pure returns (address);

Returns

NameTypeDescription
<none>addressThe 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

NameTypeDescription
<none>addressThe address of the operator.

Events

OrderCreated

EVENTS ///

Emitted when a new order is created.

event OrderCreated(
    address indexed sellToken,
    address indexed buyToken,
    uint256 sellAmount,
    uint256 minBuyAmount,
    uint32 validTo,
    address indexed receiver,
    address operator
);

Parameters

NameTypeDescription
sellTokenaddressThe address of the token to be sold.
buyTokenaddressThe address of the token to be bought.
sellAmountuint256The amount of the sell token.
minBuyAmountuint256The minimum amount of the buy token.
validTouint32The timestamp until which the order is valid.
receiveraddressThe address that will receive the bought tokens.
operatoraddressThe 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

NameTypeDescription
operatoraddressThe address of the operator who claimed the order.
claimedSellAmountuint256The amount of sell tokens claimed.
claimedBuyAmountuint256The amount of buy tokens claimed.

Errors

CallerIsNotOperatorOrReceiver

ERRORS ///

Thrown when the caller is not the operator or receiver of the order.

error CallerIsNotOperatorOrReceiver();