diff --git a/README.md b/README.md index 3b281b0..9c0499f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -49,7 +51,7 @@ 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", @@ -57,6 +59,12 @@ execute_msg = { } } +# 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], @@ -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') ``` diff --git a/cyberutils/contract/execution.py b/cyberutils/contract/execution.py index 3fcd69f..c2eccb3 100644 --- a/cyberutils/contract/execution.py +++ b/cyberutils/contract/execution.py @@ -1,3 +1,4 @@ +from jsonschema import validate from typing import Optional, Union from cyber_sdk.client.lcd import LCDClient @@ -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 @@ -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( diff --git a/pyproject.toml b/pyproject.toml index 2bc5f52..1baf16c 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.4" +version = "0.0.5" [tool.poetry.dependencies] pandas = "^1.0.0" @@ -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]