cow.order

Documentation for eth_defi.cow.order Python module.

Order data structures for CoW Swap.

How the order UID is computed:

function packOrderUidParams(
    bytes memory orderUid,
    bytes32 orderDigest,
    address owner,
    uint32 validTo
) internal pure {
    require(orderUid.length == UID_LENGTH, "GPv2: uid buffer overflow");

    // NOTE: Write the order UID to the allocated memory buffer. The order
    // parameters are written to memory in **reverse order** as memory
    // operations write 32-bytes at a time and we want to use a packed
    // encoding. This means, for example, that after writing the value of
    // `owner` to bytes `20:52`, writing the `orderDigest` to bytes `0:32`
    // will **overwrite** bytes `20:32`. This is desirable as addresses are
    // only 20 bytes and `20:32` should be `0`s:
    //
    //        |           1111111111222222222233333333334444444444555555
    //   byte | 01234567890123456789012345678901234567890123456789012345
    // -------+---------------------------------------------------------
    //  field | [.........orderDigest..........][......owner.......][vT]
    // -------+---------------------------------------------------------
    // mstore |                         [000000000000000000000000000.vT]
    //        |                     [00000000000.......owner.......]
    //        | [.........orderDigest..........]
    //
    // Additionally, since Solidity `bytes memory` are length prefixed,
    // 32 needs to be added to all the offsets.
    //
    // solhint-disable-next-line no-inline-assembly
    assembly {
        mstore(add(orderUid, 56), validTo)
        mstore(add(orderUid, 52), owner)
        mstore(add(orderUid, 32), orderDigest)
    }
}

Functions

post_order(chain_id, order[, api_timeout])

Decode CowSwap order from event log and post to CowSwap API

Classes

GPv2OrderData

See GPv2Order.Data struct in CowSwap contracts.

PostOrderResponse

Reply for opening an order at CowSwap API

SigningScheme

class GPv2OrderData

Bases: TypedDict

See GPv2Order.Data struct in CowSwap contracts.

Automatically decoded by Web3.py ABI machinery.

tx_hash: str | None

Presigned order trnasaction hash

uid: str | None

Order UID (hash)

__init__(*args, **kwargs)
__new__(**kwargs)
clear() None.  Remove all items from D.
copy() a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
class SigningScheme

Bases: enum.IntEnum

__init__(*args, **kwds)
__new__(value)
as_integer_ratio()

Return a pair of integers, whose ratio is equal to the original int.

The ratio is in lowest terms and has a positive denominator.

>>> (10).as_integer_ratio()
(10, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
>>> (0).as_integer_ratio()
(0, 1)
bit_count()

Number of ones in the binary representation of the absolute value of self.

Also known as the population count.

>>> bin(13)
'0b1101'
>>> (13).bit_count()
3
bit_length()

Number of bits necessary to represent self in binary.

>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
conjugate()

Returns self, the complex conjugate of any int.

denominator

the denominator of a rational number in lowest terms

from_bytes(byteorder='big', *, signed=False)

Return the integer represented by the given array of bytes.

bytes

Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.

signed

Indicates whether two’s complement is used to represent the integer.

imag

the imaginary part of a complex number

is_integer()

Returns True. Exists for duck type compatibility with float.is_integer.

numerator

the numerator of a rational number in lowest terms

real

the real part of a complex number

to_bytes(length=1, byteorder='big', *, signed=False)

Return an array of bytes representing an integer.

length

Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.

signed

Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.

class PostOrderResponse

Bases: object

Reply for opening an order at CowSwap API

order_uid: str

What CowSwap backend thinks should be the order UID

order_data: eth_defi.cow.order.GPv2OrderData

Order data we posted to CowsSwap API

get_order_uid()

Get the order UID from the response data

Return type

str

Get CowSwap explorer link for the order.

Return type

str

__init__(order_uid, order_data)
Parameters
Return type

None

post_order(chain_id, order, api_timeout=datetime.timedelta(seconds=600))

Decode CowSwap order from event log and post to CowSwap API

Example error:

eth_defi.cow.api.CowAPIError: Error posting CowSwap order: 404 {"errorType":"NoLiquidity","description":"no route found"}
Raises

CowAPIError – In the case API gives non-200 response.

Parameters
Return type

eth_defi.cow.order.PostOrderResponse