Python wrapper for cardano-cli for working with cardano cluster
Project description
README for cardano-clusterlib
Python wrapper for cardano-cli for working with cardano cluster.
Installation
# create and activate virtual env
$ python3 -m venv .env
$ . .env/bin/activate
# install it from PyPI
$ pip install cardano-clusterlib
# - OR - install it in develop mode together with dev requirements
$ make install
Usage
Needs working cardano-cli
(the command is available on PATH
, cardano-node
is running, CARDANO_NODE_SOCKET_PATH
is set). In state_dir
it expects "shelley/genesis.json".
Transfer funds
from cardano_clusterlib import clusterlib
# instantiate `ClusterLib`
cluster = clusterlib.ClusterLib(state_dir="path/to/cluster/state_dir")
src_address = "addr_test1vpst87uzwafqkxumyf446zr2jsyn44cfpu9fe8yqanyuh6glj2hkl"
src_skey_file = "/path/to/skey"
dst_addr = cluster.gen_payment_addr_and_keys(name="destination_address")
amount_lovelace = 10_000_000 # 10 ADA
# specify where to send funds and amounts to send
txouts = [clusterlib.TxOut(address=dst_addr.address, amount=amount_lovelace)]
# provide keys needed for signing the transaction
tx_files = clusterlib.TxFiles(signing_key_files=[src_skey_file])
# build, sign and submit the transaction
tx_raw_output = cluster.send_tx(
src_address=src_address,
tx_name="send_funds",
txouts=txouts,
tx_files=tx_files,
)
# check that the funds were received
cluster.get_utxo(dst_addr.address)
Lock and redeem funds with Plutus script
from cardano_clusterlib import clusterlib
# instantiate `ClusterLib`
cluster = clusterlib.ClusterLib(state_dir="path/to/cluster/state_dir", tx_era="babbage")
# source address - for funding
src_address = "addr_test1vpst87uzwafqkxumyf446zr2jsyn44cfpu9fe8yqanyuh6glj2hkl"
src_skey_file = "/path/to/skey"
# destination address - for redeeming
dst_addr = cluster.gen_payment_addr_and_keys(name="destination_address")
amount_fund = 10_000_000 # 10 ADA
amount_redeem = 5_000_000 # 5 ADA
# get address of the Plutus script
script_address = cluster.gen_payment_addr(
addr_name="script_address", payment_script_file="path/to/script.plutus"
)
# create a Tx output with a datum hash at the script address
# provide keys needed for signing the transaction
tx_files_fund = clusterlib.TxFiles(signing_key_files=[src_skey_file])
# get datum hash
datum_hash = cluster.get_hash_script_data(script_data_file="path/to/file.datum")
# specify Tx outputs for script address and collateral
txouts_fund = [
clusterlib.TxOut(address=script_address, amount=amount_fund, datum_hash=datum_hash),
# for collateral
clusterlib.TxOut(address=dst_addr.address, amount=2_000_000),
]
# build and submit the Tx
tx_output_fund = cluster.build_tx(
src_address=src_address,
tx_name="fund_script_address",
tx_files=tx_files_fund,
txouts=txouts_fund,
fee_buffer=2_000_000,
)
tx_signed_fund = cluster.sign_tx(
tx_body_file=tx_output_fund.out_file,
signing_key_files=tx_files_fund.signing_key_files,
tx_name="fund_script_address",
)
cluster.submit_tx(tx_file=tx_signed_fund, txins=tx_output_fund.txins)
# get newly created UTxOs
fund_utxos = cluster.get_utxo(tx_raw_output=tx_output_fund)
script_utxos = clusterlib.filter_utxos(utxos=fund_utxos, address=script_address)
collateral_utxos = clusterlib.filter_utxos(utxos=fund_utxos, address=dst_addr.address)
# redeem the locked UTxO
plutus_txins = [
clusterlib.ScriptTxIn(
txins=script_utxos,
script_file="path/to/script.plutus",
collaterals=collateral_utxos,
datum_file="path/to/file.datum",
redeemer_file="path/to/file.redeemer",
)
]
tx_files_redeem = clusterlib.TxFiles(signing_key_files=[dst_addr.skey_file])
txouts_redeem = [
clusterlib.TxOut(address=dst_addr.address, amount=amount_redeem),
]
# The entire locked UTxO will be spent and fees will be covered from the locked UTxO.
# One UTxO with "amount_redeem" amount will be created on "destination address".
# Second UTxO with change will be created on "destination address".
tx_output_redeem = cluster.build_tx(
src_address=src_address, # this will not be used, because txins (`script_txins`) are specified explicitly
tx_name="redeem_funds",
tx_files=tx_files_redeem,
txouts=txouts_redeem,
script_txins=plutus_txins,
change_address=dst_addr.address,
)
tx_signed_redeem = cluster.sign_tx(
tx_body_file=tx_output_redeem.out_file,
signing_key_files=tx_files_redeem.signing_key_files,
tx_name="redeem_funds",
)
cluster.submit_tx(tx_file=tx_signed_redeem, txins=tx_output_fund.txins)
More examples
See cardano-node-tests for more examples, e.g. minting new tokens or minting new tokens with Plutus
Source Documentation
https://cardano-clusterlib-py.readthedocs.io/en/latest/cardano_clusterlib.html
Contributing
Install this package and its dependencies as described above.
Run pre-commit install
to set up the git hook scripts that will check you changes before every commit. Alternatively run make lint
manually before pushing your changes.
Follow the Google Python Style Guide, with the exception that formatting is handled automatically by Black (through pre-commit
command).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cardano-clusterlib-0.3.0.tar.gz
.
File metadata
- Download URL: cardano-clusterlib-0.3.0.tar.gz
- Upload date:
- Size: 44.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a8df2eec6967728aaf6e6d90fe19b0949c3e0def37040153e3eab70f9571955 |
|
MD5 | 56b94d3051762eae903397db75752464 |
|
BLAKE2b-256 | 006551d9e2926ab56c8149b93c68a59608fa0750c28803dfdea70b2b37030a54 |
File details
Details for the file cardano_clusterlib-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: cardano_clusterlib-0.3.0-py3-none-any.whl
- Upload date:
- Size: 38.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a474b7ace3e48bd56bcdb13a4b61fd8d9c7cc442fedad3b6cefa5a08073a2add |
|
MD5 | 6d22e4ac0ce04b9599d815cf7f0b7106 |
|
BLAKE2b-256 | 8668edf1720e2f03752d795981d9481b21800117152c2ecb1c8ff18c1ccdf924 |