diff --git a/README.md b/README.md index 9c0499f..c8083fc 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,23 @@ execute_contract( memo='the first transfer') ``` +### query a cosmwasm contract + +```python +from cyberutils.contract import query_contract + +query_contract( + query={ + 'get_asset': { + 'chain_name': 'osmosis', + 'base': 'ibc/FE2CD1E6828EC0FAB8AF39BAC45BC25B965BA67CCBC50C13A14BD610B0D1E2C4' + } + }, + contract_address='bostrom1w33tanvadg6fw04suylew9akcagcwngmkvns476wwu40fpq36pms92re6u', + node_lcd_url='https://lcd.bostrom.cybernode.ai' +) +``` + ### execute a graphql query ```python diff --git a/cyberutils/contract/__init__.py b/cyberutils/contract/__init__.py index c371a3f..508ff2b 100644 --- a/cyberutils/contract/__init__.py +++ b/cyberutils/contract/__init__.py @@ -1,2 +1,3 @@ -from .execution import execute_contract from .instantiate import instantiate_contract +from .execution import execute_contract +from .query import query_contract diff --git a/cyberutils/contract/execution.py b/cyberutils/contract/execution.py index c2eccb3..13974eb 100644 --- a/cyberutils/contract/execution.py +++ b/cyberutils/contract/execution.py @@ -23,7 +23,8 @@ def execute_contract(execute_msgs: list[dict], contract_execute_schema: Optional[dict] = None, memo: Optional[str] = None) -> Optional[Union[BlockTxBroadcastResult, Tx]]: """ - Execute contract list of messages for a contract in a transaction or get an unsigned transaction + Execute contract list of messages for a contract in a transaction or get an unsigned transaction. + You can validate messages by contract execute schema. :param execute_msgs: list of execute messages :param contract_address: contract address :param lcd_client: network LCD client @@ -34,7 +35,7 @@ def execute_contract(execute_msgs: list[dict], :param sender: transaction sender address :param sign_and_broadcast_tx: sign and broadcast a transaction if true, otherwise return an unsigned transaction :param contract_execute_schema: schema of contract execute messages for message validation - :param memo: note(memo) of a transaction + :param memo: transaction note(memo) :return: a transaction result or an unsigned transaction """ assert ((wallet or sender) and not sign_and_broadcast_tx) or (wallet and sign_and_broadcast_tx) @@ -47,7 +48,7 @@ def execute_contract(execute_msgs: list[dict], [MsgExecuteContract( sender=_sender, contract=AccAddress(contract_address), - execute_msg=execute_msg) for execute_msg in execute_msgs] + msg=execute_msg) for execute_msg in execute_msgs] if sign_and_broadcast_tx is False: return lcd_client.tx.create( diff --git a/cyberutils/contract/query.py b/cyberutils/contract/query.py new file mode 100644 index 0000000..20adcbb --- /dev/null +++ b/cyberutils/contract/query.py @@ -0,0 +1,24 @@ +import json +import requests + +from base64 import b64encode + + +def query_contract( + contract_address: str, + query: dict, + node_lcd_url: str, + display_query_url: bool = False) -> dict: + """ + Query contract + :param contract_address: contract address + :param query: contract query + :param node_lcd_url: node lcd url + :param display_query_url: display a query url or not + :return: query result + """ + _query_msg = b64encode(json.dumps(query).encode("utf-8")).decode("utf-8") + _query = f'{node_lcd_url}/cosmwasm/wasm/v1/contract/{contract_address}/smart/{_query_msg}' + if display_query_url: + print(_query) + return requests.get(_query).json() diff --git a/pyproject.toml b/pyproject.toml index 1baf16c..70efdd5 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.5" +version = "0.0.6" [tool.poetry.dependencies] pandas = "^1.0.0"