From f2d8cf0c3e5d25526db844f63bddf447d358abe4 Mon Sep 17 00:00:00 2001 From: Pedro Yves Fracari Date: Wed, 10 Apr 2024 16:34:09 -0300 Subject: [PATCH] chore: remove old subgraphs folder --- cow_py/subgraphs/__init__.py | 0 cow_py/subgraphs/base/client.py | 52 ---------------------------- cow_py/subgraphs/base/query.py | 23 ------------- cow_py/subgraphs/client.py | 14 -------- cow_py/subgraphs/deployments.py | 36 -------------------- cow_py/subgraphs/queries.py | 60 --------------------------------- tests/subgraphs/__init__.py | 0 tests/subgraphs/deployments.py | 43 ----------------------- 8 files changed, 228 deletions(-) delete mode 100644 cow_py/subgraphs/__init__.py delete mode 100644 cow_py/subgraphs/base/client.py delete mode 100644 cow_py/subgraphs/base/query.py delete mode 100644 cow_py/subgraphs/client.py delete mode 100644 cow_py/subgraphs/deployments.py delete mode 100644 cow_py/subgraphs/queries.py delete mode 100644 tests/subgraphs/__init__.py delete mode 100644 tests/subgraphs/deployments.py diff --git a/cow_py/subgraphs/__init__.py b/cow_py/subgraphs/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/cow_py/subgraphs/base/client.py b/cow_py/subgraphs/base/client.py deleted file mode 100644 index 4dd6d77..0000000 --- a/cow_py/subgraphs/base/client.py +++ /dev/null @@ -1,52 +0,0 @@ -from abc import ABC, abstractmethod - -from cow_py.common.chains import Chain - -import json -import logging - -import httpx - - -class GraphQLError(Exception): - pass - - -async def gql(url, query, variables={}): - logging.debug(f"Executing query: {query[:15]}") - logging.debug(f"URL: {url}") - logging.debug(f"Variables: {variables}") - async with httpx.AsyncClient() as client: - r = await client.post( - url, - json=dict(query=query, variables=variables), - ) - logging.debug(f"Response status: {r.status_code}") - logging.debug(f"Response body: {r.text}") - r.raise_for_status() - - try: - return r.json().get("data", r.json()) - except KeyError: - print(json.dumps(r.json(), indent=2)) - raise GraphQLError - - -class GraphQLClient(ABC): - def __init__(self, chain) -> None: - self.url = self.get_url(chain) - - async def instance_query(self, query, variables=dict()): - return await gql(self.url, query, variables=variables) - - @abstractmethod - def get_url(self, chain) -> str: - pass - - @classmethod - async def query(cls, chain=Chain.MAINNET, query=None, variables=dict()): - if not query: - raise ValueError("query must be provided") - - client = cls(chain) - return await client.instance_query(query, variables) diff --git a/cow_py/subgraphs/base/query.py b/cow_py/subgraphs/base/query.py deleted file mode 100644 index 2d94085..0000000 --- a/cow_py/subgraphs/base/query.py +++ /dev/null @@ -1,23 +0,0 @@ -from abc import ABC, abstractmethod - -from cow_py.common.chains import Chain -from cow_py.subgraphs.base.client import GraphQLClient - - -class GraphQLQuery(ABC): - def __init__(self, chain=Chain.MAINNET, variables=dict()) -> None: - self.chain = chain - self.variables = variables - - @abstractmethod - def get_query(self) -> str: - pass - - @abstractmethod - def get_client(self) -> GraphQLClient: - pass - - async def execute(self): - query = self.get_query() - client = self.get_client() - return await client.__class__.query(self.chain, query, self.variables) diff --git a/cow_py/subgraphs/client.py b/cow_py/subgraphs/client.py deleted file mode 100644 index 035bbb5..0000000 --- a/cow_py/subgraphs/client.py +++ /dev/null @@ -1,14 +0,0 @@ -from cow_py.subgraphs.base.query import GraphQLQuery -from cow_py.subgraphs.base.client import GraphQLClient -from cow_py.subgraphs.deployments import build_subgraph_url, SubgraphEnvironment - - -class CoWSubgraph(GraphQLClient): - def get_url(self, chain): - # TODO: add a nice way to change the environment - return build_subgraph_url(chain, SubgraphEnvironment.PRODUCTION) - - -class CoWSubgraphQuery(GraphQLQuery): - def get_client(self): - return CoWSubgraph(self.chain) diff --git a/cow_py/subgraphs/deployments.py b/cow_py/subgraphs/deployments.py deleted file mode 100644 index ab4ed00..0000000 --- a/cow_py/subgraphs/deployments.py +++ /dev/null @@ -1,36 +0,0 @@ -from cow_py.common.chains import Chain -from dataclasses import dataclass -from enum import Enum - - -class SubgraphEnvironment(Enum): - PRODUCTION = "production" - STAGING = "staging" - - -SUBGRAPH_BASE_URL = "https://api.thegraph.com/subgraphs/name/cowprotocol" - - -def build_subgraph_url(chain: Chain, env: SubgraphEnvironment) -> str: - base_url = SUBGRAPH_BASE_URL - - network_suffix = "" if chain == Chain.MAINNET else "-gc" - env_suffix = "-" + env.value if env == SubgraphEnvironment.STAGING else "" - - if chain == Chain.SEPOLIA: - raise ValueError(f"Unsupported chain: {chain}") - - return f"{base_url}/cow{network_suffix}{env_suffix}" - - -@dataclass -class SubgraphConfig: - chain: Chain - - @property - def production(self) -> str: - return build_subgraph_url(self.chain, SubgraphEnvironment.PRODUCTION) - - @property - def staging(self) -> str: - return build_subgraph_url(self.chain, SubgraphEnvironment.STAGING) diff --git a/cow_py/subgraphs/queries.py b/cow_py/subgraphs/queries.py deleted file mode 100644 index b25ba9a..0000000 --- a/cow_py/subgraphs/queries.py +++ /dev/null @@ -1,60 +0,0 @@ -from cow_py.subgraphs.client import CoWSubgraphQuery - -# /** -# * GraphQL query for the total number of tokens, orders, traders, settlements, volume, and fees. -# */ -TOTALS_QUERY = """ - query Totals { - totals { - tokens - orders - traders - settlements - volumeUsd - volumeEth - feesUsd - feesEth - } - } -""" - -# /** -# * GraphQL query for the total volume over the last N days. -# * @param days The number of days to query. -# */ -LAST_DAYS_VOLUME_QUERY = """ - query LastDaysVolume($days: Int!) { - dailyTotals(orderBy: timestamp, orderDirection: desc, first: $days) { - timestamp - volumeUsd - } - } -""" - -# /** -# * GraphQL query for the total volume over the last N hours. -# * @param hours The number of hours to query. -# */ -LAST_HOURS_VOLUME_QUERY = """ - query LastHoursVolume($hours: Int!) { - hourlyTotals(orderBy: timestamp, orderDirection: desc, first: $hours) { - timestamp - volumeUsd - } - } -""" - - -class TotalsQuery(CoWSubgraphQuery): - def get_query(self): - return TOTALS_QUERY - - -class LastDaysVolumeQuery(CoWSubgraphQuery): - def get_query(self): - return LAST_DAYS_VOLUME_QUERY - - -class LastHoursVolumeQuery(CoWSubgraphQuery): - def get_query(self): - return LAST_HOURS_VOLUME_QUERY diff --git a/tests/subgraphs/__init__.py b/tests/subgraphs/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/subgraphs/deployments.py b/tests/subgraphs/deployments.py deleted file mode 100644 index a7c8580..0000000 --- a/tests/subgraphs/deployments.py +++ /dev/null @@ -1,43 +0,0 @@ -import pytest -from cow_py.common.chains import Chain -from cow_py.subgraphs.deployments import ( - build_subgraph_url, - SubgraphConfig, - SubgraphEnvironment, - SUBGRAPH_BASE_URL, -) - - -def test_build_subgraph_url(): - assert ( - build_subgraph_url(Chain.MAINNET, SubgraphEnvironment.PRODUCTION) - == f"{SUBGRAPH_BASE_URL}/cow" - ) - assert ( - build_subgraph_url(Chain.MAINNET, SubgraphEnvironment.STAGING) - == f"{SUBGRAPH_BASE_URL}/cow-staging" - ) - assert ( - build_subgraph_url(Chain.GNOSIS, SubgraphEnvironment.PRODUCTION) - == f"{SUBGRAPH_BASE_URL}/cow-gc" - ) - assert ( - build_subgraph_url(Chain.GNOSIS, SubgraphEnvironment.STAGING) - == f"{SUBGRAPH_BASE_URL}/cow-gc-staging" - ) - - with pytest.raises(ValueError): - build_subgraph_url(Chain.SEPOLIA, SubgraphEnvironment.PRODUCTION) - - -def test_subgraph_config(): - mainnet_config = SubgraphConfig(Chain.MAINNET) - assert mainnet_config.production == f"{SUBGRAPH_BASE_URL}/cow" - assert mainnet_config.staging == f"{SUBGRAPH_BASE_URL}/cow-staging" - - gnosis_chain_config = SubgraphConfig(Chain.GNOSIS) - assert gnosis_chain_config.production == f"{SUBGRAPH_BASE_URL}/cow-gc" - assert gnosis_chain_config.staging == f"{SUBGRAPH_BASE_URL}/cow-gc-staging" - - with pytest.raises(ValueError): - SubgraphConfig(Chain.SEPOLIA).production