Python SDK
Let's generate a Python SDK for KT1Fjiwsmo49yM7jch6KG4ETLFkatF3Qj8of and interact with it. See section SDK generation for the details about this contract. The Python SDK relies on the Pytezos library for forging and signing operations.
Import a KT1 from the Tezos blockchain
To generate the SDK of the contract in the current directory, run:
factori import kt1 . KT1Fjiwsmo49yM7jch6KG4ETLFkatF3Qj8of \
--python \
--name my_contract \
--network ghostnet \
--force
Compile deps and SDK
You want to install the necessary Python dependencies:
pip install pytezos mypy pyright black
Some of these packages may be installed in $HOME/.local/bin
; check
that it is in your path.
Now cleanly format the generated code:
make format-python
You are ready to use the Python SDK!
Hello world example
First, you may want to have a look at the content of
src/python_sdk/
directory.
ls src/python_sdk/
Create a file src/python_sdk/test.py
with the following content:
from importlib.metadata import metadata
import time
import my_contract_python_interface
import blockchain
def main():
debug = False
hello_kt1 = "KT1Fjiwsmo49yM7jch6KG4ETLFkatF3Qj8of"
hello_param: my_contract_python_interface.Hello = "Hello from Python"
ophash = my_contract_python_interface.callHello(
hello_kt1,
_from=blockchain.alice,
param=hello_param,
networkName="ghostnet",
debug=debug,
)
print("A hello operation is sent to a node. Its hash is: " + ophash)
print("Waiting for its inclusion in a block ...")
time.sleep(15) # we need to wait for one block before calling the contract
print("Check the status of your operation: https://ghostnet.tzkt.io/" + ophash)
main()
To run it, simply run:
python3 src/python_sdk/test.py
If everything goes well, you should see your transaction at: https://ghostnet.tzkt.io/KT1Fjiwsmo49yM7jch6KG4ETLFkatF3Qj8of