用于处理cardano集群的cardano-cli的Python封装
项目描述
cardano-clusterlib的README
cardano-cli的Python封装,用于与cardano集群交互。它支持所有cardano-cli命令(除了genesis和governance的部分)。
该库用于开发cardano-node系统测试。
安装
# create and activate virtual env
$ python3 -m venv .env
$ . .env/bin/activate
# install cardano-clusterlib from PyPI
$ pip install cardano-clusterlib
# - OR - install cardano-clusterlib in development mode together with dev requirements
$ make install
用法
该库需要正常工作的cardano-cli
(命令在PATH
中可用,cardano-node
正在运行,CARDANO_NODE_SOCKET_PATH
已设置)。在state_dir
中它期望"shelley/genesis.json"。
# instantiate `ClusterLib`
cluster = clusterlib.ClusterLib(state_dir="path/to/cluster/state_dir")
在拜伦时代启动的自定义测试网上,您可能需要在拜伦时代和雪莉时代之间指定槽位偏移量。 "slots_offset" 是拜伦时代的槽位数与相同数量的雪莉时代槽位数的差值。
例如,对于一个具有以下参数的测试网
- 拜伦时代每个时代100个槽位
- 雪莉时代每个时代1000个槽位
- 在分叉到雪莉时代之前,拜伦时代有两个时代
偏移量将是 2 * (1000 - 100) = 1800
。
cluster = clusterlib.ClusterLib(state_dir="path/to/cluster/state_dir", slots_offset=1800)
转账资金
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.g_address.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.g_transaction.send_tx(
src_address=src_address,
tx_name="send_funds",
txouts=txouts,
tx_files=tx_files,
)
# check that the funds were received
cluster.g_query.get_utxo(dst_addr.address)
使用Plutus脚本锁定和赎回资金
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.g_address.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.g_address.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.g_transaction.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.g_transaction.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.g_transaction.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.g_transaction.submit_tx(tx_file=tx_signed_fund, txins=tx_output_fund.txins)
# get newly created UTxOs
fund_utxos = cluster.g_query.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.g_transaction.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.g_transaction.sign_tx(
tx_body_file=tx_output_redeem.out_file,
signing_key_files=tx_files_redeem.signing_key_files,
tx_name="redeem_funds",
)
cluster.g_transaction.submit_tx(tx_file=tx_signed_redeem, txins=tx_output_fund.txins)
更多示例
请参阅 cardano-node-tests 以获取更多示例,例如 铸造新代币 或 使用Plutus铸造新代币
源文档
https://cardano-clusterlib-py.readthedocs.io/en/latest/cardano_clusterlib.html
贡献
按照上述说明安装此软件包及其依赖项。
运行 pre-commit install
以设置在每次提交之前检查您更改的git钩子脚本。或者,在推送更改之前手动运行 make lint
。
遵循 Google Python风格指南,但格式化由 Black 自动处理(通过 pre-commit
命令)。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源代码发行版
cardano_clusterlib-0.6.3.tar.gz (67.1 kB 查看哈希值)
构建分发
关闭
cardano_clusterlib-0.6.3.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ab2e424c4f3e38a021b49e6912ac4e82f748d88e4657d1e2ae627223504a9037 |
|
MD5 | f77a0fd01e621d292a8008a8ea29085b |
|
BLAKE2b-256 | a0f3490621a7a85b6d53a706669048730f4feb85d22cc6ec70365726fbed0435 |
关闭
cardano_clusterlib-0.6.3-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 326cd4e686b857cd0e9c2065ebeb1f612f4b81772330e676a3db74005f723c2c |
|
MD5 | ba1f8b070fc5ff303f653e7eeb2ad6d6 |
|
BLAKE2b-256 | 8059b12549745559d7253bd36716639c7022ac91505aa2aa08b88f98d1dd4d8d |