Skip to content

Commit

Permalink
Merge pull request #6 from Snedashkovsky/add_contract_query
Browse files Browse the repository at this point in the history
Add contract query
  • Loading branch information
Snedashkovsky authored Apr 15, 2024
2 parents a96ef8b + f677235 commit 3e56b87
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cyberutils/contract/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .execution import execute_contract
from .instantiate import instantiate_contract
from .execution import execute_contract
from .query import query_contract
7 changes: 4 additions & 3 deletions cyberutils/contract/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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(
Expand Down
24 changes: 24 additions & 0 deletions cyberutils/contract/query.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3e56b87

Please sign in to comment.