Skip to main content

No project description provided

Project description

CoW Protocol Python SDK

Help the herd ๐Ÿฎ

๐Ÿ„ Introduction

Welcome to the CoW Protocol Python SDK (cowdao_cowpy), a developer-friendly Python library for interacting with the CoW Protocol. This SDK provides tools for querying on-chain data, managing orders, and integrating with the CoW Protocol's smart contracts. Whether you're building a DeFi application, a solver, or just exploring the CoW Protocol, this SDK aims to make your development journey smoother and more enjoyable. ๐Ÿš€

๐Ÿ„ Features

  • Querying CoW Protocol subgraph.
  • Managing orders on the CoW Protocol.
  • Interacting with CoW Protocol smart contracts.
  • Encoding orders metadata and pinning to CID.
  • Fetching and decoding blockchain data.

๐Ÿ„ Installation

Get started by installing cowdao_cowpy:

pip install cowdao_cowpy

๐Ÿ„ Getting Started

Here's a simple example to get your hooves dirty:

from cowdao_cowpy.order_book.api import OrderBookApi, UID

# Initialize the OrderBookApi
order_book_api = OrderBookApi()

# Fetch and display orders
orders = order_book.get_order_by_uid(UID("0x..."))
print(orders)

๐Ÿ„ Project Structure

  • common/: Utilities and configurations, the backbone of the SDK.
  • contracts/(TODO): A pasture of Smart contract ABIs for interaction.
  • order_book/: Functions to wrangle orders on the CoW Protocol.
  • order_signing/(TODO): Tools for signing and validating orders. Anything inside this module should use higher level modules, and the process of actually signing (ie. calling the web3 function to generate the signature, should be handled in contracts, not here).
  • subgraph/: GraphQL client for querying CoW Protocol's Subgraph.
  • web3/: Web3 providers for blockchain interactions.

๐Ÿ„ How to Use

Querying the Subgraph

Using the built-in GraphQL client, you can query the CoW Protocol's Subgraph to get real-time data on the CoW Protocol. You can query the Subgraph by using the SubgraphClient class and passing in the URL of the Subgraph.

from cowdao_cowpy.subgraph.client import SubgraphClient
from cowdao_cowpy.subgraph.deployments import build_subgraph_url

url = build_subgraph_url() # Default network is Chain.SEPOLIA and env SubgraphEnvironment.PRODUCTION
client = SubgraphClient(url=url)

# Fetch the total supply of the CoW Protocol, defined in a query in cowdao_cowpy/subgraph/queries
totals = await client.totals()
print(totals) # Pydantic model, defined in cowdao_cowpy/subgraph/graphql_client/{query_name}.py

Or you can leverage SubgraphClient to use a custom query and get the results as JSON:

from pprint import pprint
from cowdao_cowpy.subgraph.client import SubgraphClient
from cowdao_cowpy.subgraph.deployments import build_subgraph_url

url = build_subgraph_url() # Default network is Chain.SEPOLIA and env SubgraphEnvironment.PRODUCTION
client = SubgraphClient(url=url)

response = await client.execute(query="""
            query LastDaysVolume($days: Int!) {
              dailyTotals(orderBy: timestamp, orderDirection: desc, first: $days) {
                timestamp
                volumeUsd
              }
            }
            """, variables=dict(days=2)
            )

data = client.get_data(response)
pprint(data)

Or you can leverage SubgraphClient to use a custom query and get the results as JSON:

from pprint import pprint
from cowdao_cowpy.subgraph.client import SubgraphClient
from cowdao_cowpy.subgraph.deployments import build_subgraph_url

url = build_subgraph_url() # Default network is Chain.SEPOLIA and env SubgraphEnvironment.PRODUCTION
client = SubgraphClient(url=url)

response = await client.execute(query="""
            query LastDaysVolume($days: Int!) {
              dailyTotals(orderBy: timestamp, orderDirection: desc, first: $days) {
                timestamp
                volumeUsd
              }
            }
            """, variables=dict(days=2)
            )

data = client.get_data(response)
pprint(data)

๐Ÿ„ Development

๐Ÿ„ Tests

Run tests to ensure everything's working:

make test # or poetry run pytest

๐Ÿ„ Formatting/Linting

Run the formatter and linter:

make format # or ruff check . --fix
make lint # or ruff format

๐Ÿ„ Codegen

Generate the SDK from the CoW Protocol smart contracts, Subgraph, and Orderbook API:

make codegen

๐Ÿ„ Development

๐Ÿ„ Tests

Run tests to ensure everything's working:

make test # or poetry run pytest

๐Ÿ„ Formatting/Linting

Run the formatter and linter:

make format # or ruff check . --fix
make lint # or ruff format

๐Ÿ„ Codegen

Generate the SDK from the CoW Protocol smart contracts, Subgraph, and Orderbook API:

make codegen

๐Ÿ„ Development

๐Ÿ„ Tests

Run tests to ensure everything's working:

make test # or poetry run pytest

๐Ÿ„ Formatting/Linting

Run the formatter and linter:

make format # or ruff check . --fix
make lint # or ruff format

๐Ÿ„ Codegen

Generate the SDK from the CoW Protocol smart contracts, Subgraph, and Orderbook API:

make codegen

๐Ÿ„ Contributing to the Herd

Interested in contributing? Here's how you can help:

git clone https://github.com/cowdao-grants/cow-py
cd cow-py
poetry install

After making changes, make sure to run the appropriate code generation tasks and tests:

make codegen
make test

๐Ÿ„ Need Help?

Got questions, bug reports, or feature requests? Open an issue in our GitHub repository.

๐Ÿ„ License

cowdao_cowpy is released under the GNU License. For more details, check out the LICENSE file.


Happy coding, and may the herd be with you! ๐Ÿ„๐Ÿ’ป

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cowdao_cowpy-1.0.0rc1.tar.gz (60.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cowdao_cowpy-1.0.0rc1-py3-none-any.whl (82.0 kB view details)

Uploaded Python 3

File details

Details for the file cowdao_cowpy-1.0.0rc1.tar.gz.

File metadata

  • Download URL: cowdao_cowpy-1.0.0rc1.tar.gz
  • Upload date:
  • Size: 60.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for cowdao_cowpy-1.0.0rc1.tar.gz
Algorithm Hash digest
SHA256 9dd450883fd0860d49b17366ee0117ffcb94956eccda79dc66e61b10c56908f6
MD5 13f96af21f0563e741105afe1611a3b0
BLAKE2b-256 9459b0857025230f78ea03097e9fd98fba455deb809a54eab858b3436452733d

See more details on using hashes here.

Provenance

The following attestation bundles were made for cowdao_cowpy-1.0.0rc1.tar.gz:

Publisher: publish-to-test-pypi.yml on cowdao-grants/cow-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cowdao_cowpy-1.0.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for cowdao_cowpy-1.0.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 188dffe62c0e5daf1bb5377b9dc3ee3cc3ba79e3be6f514acf5b77b9947aaddd
MD5 3cdcf2cb13b62fa34ff4e0d4ca0f22d2
BLAKE2b-256 8b5b5ddbeb56a33b255f23ebcdf0a81737824cd2118fc3c9ca2d83fe83797151

See more details on using hashes here.

Provenance

The following attestation bundles were made for cowdao_cowpy-1.0.0rc1-py3-none-any.whl:

Publisher: publish-to-test-pypi.yml on cowdao-grants/cow-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page