Skip to content

Commit

Permalink
Merge pull request #5 from Snedashkovsky/add_contract_msg_validation
Browse files Browse the repository at this point in the history
Add validation of contract execute messages
  • Loading branch information
Snedashkovsky authored Jun 7, 2023
2 parents f18ac37 + 210ae93 commit 7ed7d17
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ get_json_from_bash_query(
### execute a cosmwasm contract

```python
import requests

from cyber_sdk.client.lcd import LCDClient
from cyber_sdk.key.mnemonic import MnemonicKey

Expand All @@ -49,14 +51,20 @@ lcd_client = LCDClient(
)
wallet = lcd_client.wallet(mk)

# the execution message in a contract must match its schema
# an execution message in a contract must match its schema
execute_msg = {
"transfer" : {
"recipient": "bostrom1xszmhkfjs3s00z2nvtn7evqxw3dtus6yr8e4pw",
"amount": "1000000"
}
}

# load a contract schema for an execute message validation
contract_schema_json = \
requests.get(
url='https://raw.githubusercontent.com/Snedashkovsky/cw-plus/main/contracts/cw20-base/schema/cw20-base.json'
).json()

# execution of a contract
execute_contract(
execute_msgs=[execute_msg],
Expand All @@ -66,6 +74,7 @@ execute_contract(
gas=500_000,
fee_amount=0,
fee_denom='boot',
contract_execute_schema=contract_schema_json['execute'],
memo='the first transfer')
```

Expand Down
7 changes: 7 additions & 0 deletions cyberutils/contract/execution.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from jsonschema import validate
from typing import Optional, Union

from cyber_sdk.client.lcd import LCDClient
Expand All @@ -19,6 +20,7 @@ def execute_contract(execute_msgs: list[dict],
wallet: Optional[Wallet] = None,
sender: Optional[Union[str, AccAddress]] = None,
sign_and_broadcast_tx: bool = True,
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
Expand All @@ -31,10 +33,15 @@ def execute_contract(execute_msgs: list[dict],
:param wallet: executable wallet
: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
: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)
if contract_execute_schema:
for _execute_msg in execute_msgs:
validate(_execute_msg, contract_execute_schema)

_sender = wallet.key.acc_address if sender is None else AccAddress(sender)
_msgs = \
[MsgExecuteContract(
Expand Down
3 changes: 2 additions & 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.4"
version = "0.0.5"

[tool.poetry.dependencies]
pandas = "^1.0.0"
Expand All @@ -30,6 +30,7 @@ pandarallel = "^1.6.0"
multiprocess = "^0.70.14"
python = "^3.9"
cyber_sdk = "^1.1.2"
jsonschema = "^4.17.1"
gql = { version = "^3.0.0", extras = ["all"] }

[tool.poetry.dev-dependencies]
Expand Down

0 comments on commit 7ed7d17

Please sign in to comment.