Skip to content

Instantly share code, notes, and snippets.

@atomlab
Last active November 19, 2020 17:21
Show Gist options
  • Save atomlab/ae28d15fa0ad331011727b0ab1f66502 to your computer and use it in GitHub Desktop.
Save atomlab/ae28d15fa0ad331011727b0ab1f66502 to your computer and use it in GitHub Desktop.
Cardano pool registration tx build
#!/bin/bash
set -e
cardano-cli shelley query protocol-parameters \
--mainnet \
--out-file params.json
currentSlot=$(cardano-cli shelley query tip --mainnet | jq -r '.slotNo')
cardano-cli shelley query utxo --address $(cat payment.addr) --mainnet > fullUtxo.out
tail -n +3 fullUtxo.out | sort -k3 -nr > balance.out
cat balance.out
tx_in=""
total_balance=0
while read -r utxo; do
in_addr=$(awk '{ print $1 }' <<< "${utxo}")
idx=$(awk '{ print $2 }' <<< "${utxo}")
utxo_balance=$(awk '{ print $3 }' <<< "${utxo}")
total_balance=$((${total_balance}+${utxo_balance}))
echo TxHash: ${in_addr}#${idx}
echo ADA: ${utxo_balance}
tx_in="${tx_in} --tx-in ${in_addr}#${idx}"
echo tx_in:${tx_in}
done < balance.out
txcnt=$(cat balance.out | wc -l)
echo Total ADA balance: ${total_balance}
echo Number of UTXOs: ${txcnt}
poolDeposit=$(cat params.json | jq -r '.poolDeposit')
echo poolDeposit: $poolDeposit
cardano-cli shelley transaction build-raw \
${tx_in} \
--tx-out $(cat payment.addr)+$(( ${total_balance} - ${poolDeposit})) \
--ttl $(( ${currentSlot} + 10000)) \
--fee 0 \
--certificate-file pool.cert \
--certificate-file deleg.cert \
--out-file tx.tmp
fee=$(cardano-cli shelley transaction calculate-min-fee \
--tx-body-file tx.tmp \
--tx-in-count ${txcnt} \
--tx-out-count 1 \
--mainnet \
--witness-count 3 \
--byron-witness-count 0 \
--protocol-params-file params.json | awk '{ print $1 }')
echo fee: $fee
# Ensure your balance is greater than cost of fee + minPoolCost or this will not work.
txOut=$((${total_balance}-${poolDeposit}-${fee}))
echo Change Output: ${txOut}
cardano-cli shelley transaction build-raw \
${tx_in} \
--tx-out $(cat payment.addr)+${txOut} \
--ttl $(( ${currentSlot} + 10000)) \
--fee ${fee} \
--certificate-file pool.cert \
--certificate-file deleg.cert \
--out-file tx.raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment