Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 breakdown pt3 - add order_book module #12

Merged
merged 26 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
85a0793
refactor repo configs/readme
ribeirojose Apr 25, 2024
f2324d7
add codegen module
ribeirojose Apr 25, 2024
cd653d9
add common module
ribeirojose Apr 25, 2024
15df625
remove stale subgraphs/core modules
ribeirojose Apr 25, 2024
4b923e4
add contracts module
ribeirojose Apr 25, 2024
04f9139
add order_book module
ribeirojose Apr 25, 2024
661fe9b
add order posting example
ribeirojose Apr 25, 2024
8b78530
add generated api model
yvesfracari Aug 16, 2024
0f938d5
add python-dotenv package
yvesfracari Aug 16, 2024
5576b57
refactor importing sort
yvesfracari Aug 16, 2024
a3e1187
refactor .env usage and variables requested
yvesfracari Aug 16, 2024
0c9c04c
codegen: update reference openapi file for orderbook api codegen
ribeirojose Sep 9, 2024
48d660b
add E2E_SEPOLIA_TESTING_EOA_PRIVATE_KEY to .env.example
ribeirojose Sep 9, 2024
97ebc3a
codegen: regenerate order book models
ribeirojose Sep 9, 2024
2151934
api: refactor api base code to handle errors from orderbook api
ribeirojose Sep 10, 2024
b2d7ef7
orderbook: fix issue with type in order cancellation
ribeirojose Sep 10, 2024
f7e6d1b
tests(e2e): add e2e order posting test backed by vcr
ribeirojose Sep 10, 2024
4b7a26f
Merge branch 'main' into cow-4-order-book
ribeirojose Sep 10, 2024
4d48b42
ignore .env* files
ribeirojose Sep 10, 2024
dec2723
api(orderbook): refactor data serialization/deserialization
ribeirojose Sep 10, 2024
f97c695
tests(e2e): use gnosis mainnet for e2e test
ribeirojose Sep 10, 2024
b54f316
ci: stop using deprecated version of actions/upload-artifact
ribeirojose Sep 10, 2024
b88148e
chore: ignore pyright error when instantiating model with string posi…
ribeirojose Sep 10, 2024
ea2d831
chore: remove TODO/WIP comments from README
ribeirojose Sep 10, 2024
18fa6f2
ci: ensure E2E_GNOSIS_MAINNET_TESTING_EOA_PRIVATE_KEY is available wh…
ribeirojose Sep 10, 2024
895dd8f
chore(ci): revert actions/upload-artifact to v3 to avoid bug when sav…
ribeirojose Sep 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PRIVATE_KEY=
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
python-version: [
"3.8", "3.9", "3.10", "3.11", "3.12",
"3.10", "3.11", "3.12",
]
timeout-minutes: 10
steps:
Expand All @@ -32,6 +32,10 @@ jobs:
cache: poetry
cache-dependency-path: poetry.lock

- name: Poetry lock
run: |
poetry lock
- name: Install dependencies
run: |
poetry install
Expand Down
17 changes: 9 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.4
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.7
hooks:
# Run the linter.
- id: ruff
args: [--fix]
# Run the formatter.
- id: ruff-format
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.PHONY: codegen web3_codegen orderbook_codegen subgraph_codegen test lint format remove_unused_imports

codegen: web3_codegen orderbook_codegen subgraph_codegen

# web3_codegen:
# poetry run web3_codegen

orderbook_codegen:
poetry run datamodel-codegen --url="https://raw.githubusercontent.com/cowprotocol/services/v2.245.1/crates/orderbook/openapi.yml" --output cow_py/order_book/generated/model.py --target-python-version 3.12 --output-model-type pydantic_v2.BaseModel --input-file-type openapi

subgraph_codegen:
poetry run ariadne-codegen

test:
poetry run pytest -s

lint:
poetry run ruff check . --fix

format: remove_unused_imports
poetry run ruff format

remove_unused_imports:
poetry run pycln --all .

typecheck:
poetry run pyright
80 changes: 68 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Welcome to the CoW Protocol Python SDK (cow_py), a developer-friendly Python lib

## 🐄 Features

- Querying CoW Protocol subgraphs.
- Querying CoW Protocol subgraph.
- Managing orders on the CoW Protocol.
- Interacting with CoW Protocol smart contracts.
- Encoding orders metadata and pinning to CID.
Expand All @@ -27,14 +27,14 @@ pip install cow_py
Here's a simple example to get your hooves dirty:

```python
# TODO: this code is aspirational, this API doesn't exist
from cow_py.order_book import OrderBook

# Initialize the OrderBook
order_book = OrderBook()
from cow_py.order_book.api import OrderBookApi, UID

# Initialize the OrderBookApi
order_book_api = OrderBookApi()

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

Expand All @@ -44,19 +44,47 @@ print(orders)
- `contracts/`(TODO): A pasture of Smart contract ABIs for interaction.
- `order_book/`(TODO): 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).
- `subgraphs/`(WIP): GraphQL clients for querying CoW Protocol data.
- `subgraph/`(WIP): GraphQL client for querying CoW Protocol's Subgraph.
- `web3/`: Web3 providers for blockchain interactions.

## 🐄 How to Use

### Querying the Subgraph (WIP)
### 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.

```python
from cow_py.subgraphs.queries import TotalsQuery
from cow_py.subgraph.client import SubgraphClient

totals_query = TotalsQuery()
totals = await totals_query.execute()
print(totals)
url = build_subgraph_url() # Default network is Chain.MAINNET and env SubgraphEnvironment.PRODUCTION
client = SubgraphClient(url=url)

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

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

```python
from pprint import pprint
from cow_py.subgraph.client import SubgraphClient

url = build_subgraph_url() # Default network is Chain.MAINNET 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)
```

### Signing an Order (TODO)
Expand All @@ -75,6 +103,34 @@ signed_order = sign_order(order_details, private_key="your_private_key")
print(signed_order)
```

## 🐄 Development

### 🐄 Tests

Run tests to ensure everything's working:

```bash
make test # or poetry run pytest
```

### 🐄 Formatting/Linting

Run the formatter and linter:

```bash
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:

```bash
make codegen
```


## 🐄 Contributing to the Herd

Interested in contributing? Here's how you can help:
Expand Down
5 changes: 5 additions & 0 deletions cow_py/codegen/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .abi_handler import ABIHandler

__all__ = [
"ABIHandler",
]
Loading
Loading