From 027b710d32d5b0dba02e552e3fac98472e2e0f59 Mon Sep 17 00:00:00 2001 From: Snedashkovsky Date: Mon, 8 May 2023 13:25:35 +0800 Subject: [PATCH 1/3] - new version --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 28b1708..e2ea4c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ classifiers = [ "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", ] -description = "The Toolset for cyber protocol" +description = "The Toolset for cyber protocol and cosmos ecosystem" documentation = "https://github.com/Snedashkovsky/cyberutils" homepage = "https://github.com/Snedashkovsky/cyberutils" keywords = ["bostrom", "cyber", "knowledge-graph", "dex", "swap", "defi", "crypto", "blockchain", ] @@ -18,7 +18,7 @@ license = "MIT" packages = [{ include = "cyberutils" }] readme = "README.md" repository = "https://github.com/Snedashkovsky/cyberutils.git" -version = "0.0.1" +version = "0.0.2" [tool.poetry.dependencies] pandas = "^1.0.0" From 588f7105d0349252bf8a22ad8415b4106e7e3685 Mon Sep 17 00:00:00 2001 From: Snedashkovsky Date: Wed, 10 May 2023 14:08:31 +0800 Subject: [PATCH 2/3] - add parameters for graphql execution --- README.md | 28 ++++++++++++++++++++++++++++ cyberutils/graphql/execution.py | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9788a29..46c3dd6 100644 --- a/README.md +++ b/README.md @@ -95,5 +95,33 @@ res = await execute_graphql( """, graphql_url='https://index.bostrom.cybernode.ai/v1/graphql') +pd.DataFrame(res['contracts']) +``` +a query with variable values +```python +import pandas as pd + +from cyberutils.graphql import execute_graphql + +res = await execute_graphql( + request=""" + query ContractsCodeID($code_id: bigint) { + contracts(order_by: {tx: desc_nulls_last}, where: {code_id: {_eq: $code_id}}) { + address + admin + creation_time + creator + fees + gas + height + label + tx + code_id + } + } + """, + variable_values={"code_id": "3"}, + graphql_url='https://index.bostrom.cybernode.ai/v1/graphql') + pd.DataFrame(res['contracts']) ``` \ No newline at end of file diff --git a/cyberutils/graphql/execution.py b/cyberutils/graphql/execution.py index bee8755..6571554 100644 --- a/cyberutils/graphql/execution.py +++ b/cyberutils/graphql/execution.py @@ -1,12 +1,32 @@ +from typing import Union, Optional, Dict, Any + +from graphql import ExecutionResult from gql import gql, Client from gql.transport.aiohttp import AIOHTTPTransport -async def execute_graphql(request: str, graphql_url: str) -> dict: +async def execute_graphql(request: str, + graphql_url: str, + variable_values: Optional[Dict[str, Any]] = None, + operation_name: Optional[str] = None, + serialize_variables: Optional[bool] = None, + parse_result: Optional[bool] = None, + get_execution_result: bool = False, + **kwargs, + ) -> Union[Dict[str, Any], ExecutionResult]: """ Execute a GraphQL query :param request: the GraphQL request as a String :param graphql_url: The GraphQL server URL. Example: 'https://server.com:PORT/path'. + :param variable_values: Dictionary of input parameters. + :param operation_name: Name of the operation that shall be executed. + :param serialize_variables: whether the variable values should be + serialized. Used for custom scalars and/or enums. + By default, use the serialize_variables argument of the client. + :param parse_result: Whether gql will unserialize the result. + By default, use the parse_results argument of the client. + :param get_execution_result: return the full ExecutionResult instance instead of + only the "data" field. Necessary if you want to get the "extensions" field. :return: GraphQL result """ # Select your transport with a defined url endpoint @@ -17,4 +37,12 @@ async def execute_graphql(request: str, graphql_url: str) -> dict: ) as _session: # Execute a query _gql_query = gql(request_string=request) - return await _session.execute(_gql_query) + return await _session.execute( + document=_gql_query, + variable_values=variable_values, + operation_name=operation_name, + serialize_variables=serialize_variables, + parse_result=parse_result, + get_execution_result=get_execution_result, + **kwargs, + ) From 2fcaf5d574a127beeaf0efa3e762a884538aa9fb Mon Sep 17 00:00:00 2001 From: Snedashkovsky Date: Thu, 11 May 2023 14:16:46 +0800 Subject: [PATCH 3/3] - add getting messages with a given address and type - bump version --- README.md | 14 ++++++++++ cyberutils/graphql/__init__.py | 1 + cyberutils/graphql/queries.py | 49 ++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 cyberutils/graphql/queries.py diff --git a/README.md b/README.md index 46c3dd6..3b281b0 100644 --- a/README.md +++ b/README.md @@ -124,4 +124,18 @@ res = await execute_graphql( graphql_url='https://index.bostrom.cybernode.ai/v1/graphql') pd.DataFrame(res['contracts']) +``` +get messages with a given address and type +```python +import pandas as pd + +from cyberutils.graphql import get_messages_by_address_and_type + + +res = await get_messages_by_address_and_type( + address='bostrom1xszmhkfjs3s00z2nvtn7evqxw3dtus6yr8e4pw', + msg_type='cosmos.bank.v1beta1.MsgSend', + graphql_url='https://index.bostrom.cybernode.ai/v1/graphql') + +pd.DataFrame(res) ``` \ No newline at end of file diff --git a/cyberutils/graphql/__init__.py b/cyberutils/graphql/__init__.py index 66f1bde..c0385b1 100644 --- a/cyberutils/graphql/__init__.py +++ b/cyberutils/graphql/__init__.py @@ -1 +1,2 @@ from .execution import execute_graphql +from .queries import get_messages_by_address_and_type diff --git a/cyberutils/graphql/queries.py b/cyberutils/graphql/queries.py new file mode 100644 index 0000000..edba9d7 --- /dev/null +++ b/cyberutils/graphql/queries.py @@ -0,0 +1,49 @@ +from typing import Optional, Dict, List + +from .execution import execute_graphql + + +async def get_messages_by_address_and_type(address: str, + msg_type: str, + graphql_url: str, + limit: int = 100, + offset: int = 0) -> Optional[List[Dict]]: + """ + get messages with a given address and type + :param address: network address + :param msg_type: type of message, ex. `cosmos.bank.v1beta1.MsgSend` + :param graphql_url: The GraphQL server URL. Example: 'https://server.com:PORT/path'. + :param limit: limit of entities + :param offset: where in the list the server should start when returning items for a query + :return: dict with messages + """ + _res = await execute_graphql( + request=""" + query MessagesByAddressAndType($address: _text, $types: _text, $limit: bigint, $offset: bigint) { + messages_by_address( + args: {addresses: $address, limit: $limit, offset: $offset, types: $types} + order_by: {transaction: {block: {height: desc}}} + ) { + message_type: type + message_value: value + message_involved_addresses: involved_accounts_addresses + transaction_hash + transaction { + success + memo + signer_infos + } + } + } + """, + variable_values={ + "address": f"{{{address}}}", + "types": f"{{{msg_type}}}", + "limit": str(limit), + "offset": str(offset) + }, + graphql_url=graphql_url) + try: + return _res['messages_by_address'] + except KeyError: + return diff --git a/pyproject.toml b/pyproject.toml index e2ea4c1..bd9f388 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ license = "MIT" packages = [{ include = "cyberutils" }] readme = "README.md" repository = "https://github.com/Snedashkovsky/cyberutils.git" -version = "0.0.2" +version = "0.0.3" [tool.poetry.dependencies] pandas = "^1.0.0"