Deploy a contract

You can deploy smart contracts in CosmPy using LedgerContract. For this, you will need the path to where the contract is stored (in this case simple.wasm), a LedgerClient and a Wallet:

from cosmpy.aerial.contract import LedgerContract

PATH = "../simple.wasm"

contract = LedgerContract(PATH, ledger_client)
contract.deploy({}, wallet)

You can now start interacting with the contract. To get the address of where the contract is deployed on the network:

print(f"Contract deployed at: {contract.address}")

You can query the values of the contract's state variables:

result = contract.query({"get": {"owner": str(wallet.address())}})
print("Initial state:", result)

You can also set these values. The following sets the state variable value to foobar:

contract.execute({"set": {"value": "foobar"}}, wallet).wait_to_complete()

Let's check if this was set correctly:

result = contract.query({"get": {"owner": str(wallet.address())}})
print("State after set:", result)

Similarly, you can clear the state variables:

contract.execute({"clear": {}}, wallet).wait_to_complete()

result = contract.query({"get": {"owner": str(wallet.address())}})
print("State after clear:", result)
Back to top