Let's find out how to use HeraPy quickly with a few examples.
- Python3 (>= 3.7)
Setup your environment and install aergo-herapy
$ cd my_new_project
$ virtualenv -p python3 venv
$ source venv/bin/activate
$ pip install aergo-herapy
Connecting to Aergo
Connecting to Aergo can be done with a public api like 'testnet-api.aergo.io:7845' or by running your own Aergo node_.
import aergo.herapy as herapy
aergo = herapy.Aergo()
aergo.connect('testnet-api.aergo.io:7845')
print(aergo.get_chain_info())
aergo.disconnect()
Connecting to Aergo is optional when creating new accounts with the parameter skip_state=True.
import aergo.herapy as herapy
aergo = herapy.Aergo()
# connect to a node to retrieve the account state (nonce, balance...)
# aergo.connect('testnet-api.aergo.io:7845')
# aergo.new_account()
# create a new account offline
aergo.new_account(skip_state=True)
# print the address
print(aergo.get_address())
# print the address as bytes
print(bytes(aergo.get_address()))
For using an account created in various other SDKs and Aergocli, the prefered method is to import an Aergo encrypted keystore file.
Connecting to a node is optional.
import aergo.herapy as herapy
aergo = herapy.Aergo()
aergo.new_account(skip_state=True)
exp_account = aergo.export_account_to_keystore("keep-safe")
aergo2 = herapy.Aergo()
aergo2.import_account_from_keystore(exp_account, "keep-safe", skip_state=True)
import aergo.herapy as herapy
# connect to a node
aergo = herapy.Aergo()
aergo.connect('testnet-api.aergo.io:7845')
keystore_file_path = "./my/keystore.json"
# import account from keystore file and get current nonce
aergo.import_account_from_keystore_file(keystore_file_path, "keep-safe")
# transfer 1 aergo
tx, status = aergo.transfer(to_address, 1 * 10**18)
assert result.status == herapy.CommitStatus.TX_OK
receipt = aergo.wait_tx_result(tx.tx_hash)
assert receipt.status == herapy.TxResultStatus.SUCCESS:
import aergo.herapy as herapy
# connect to a node
aergo = herapy.Aergo()
aergo.connect('testnet-api.aergo.io:7845')
keystore_file_path = "./my/keystore.json"
# import account from keystore file and get current nonce
aergo.import_account_from_keystore_file(keystore_file_path, "keep-safe")
# deploy a new contract
payload = "Compiled contract string"
tx, result = aergo.deploy_sc(amount=0, payload=payload, args=1234)
assert result.status == herapy.CommitStatus.TX_OK
receipt = aergo.wait_tx_result(tx.tx_hash)
assert receipt.status == herapy.TxResultStatus.CREATED:
# get address of newly deployed contract
sc_address = receipt.contract_address
# send a transaction to a contract (write)
tx, result = aergo.call_sc(sc_address, "lua function name")
assert result.status == herapy.CommitStatus.TX_OK
assert receipt.status == herapy.TxResultStatus.SUCCESS:
receipt = aergo.wait_tx_result(tx.tx_hash)
# query a contract function (read-only)
return_value = aergo.query_sc(sc_address, "lua function name")