Last active
December 4, 2021 18:54
-
-
Save dk8996/6dc32b3dd8dab6786e6f15d64a18267e to your computer and use it in GitHub Desktop.
Script for creating a simple transaction on ADA network (Shelley 1.18.0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#title :simple-transaction.sh | |
#donations :ADA: addr1q832gesdnxn9twyymtqmqywcsutmqc23u6wghr5y44mmyn6dze685vgkuem8hvd3kdej7kpzuzf7wxk3qndrmwjplusq5rr3d6 | |
#description :This script makes it easy to send a simple transaction from one address to another, see https://docs.cardano.org/projects/cardano-node/en/latest/stake-pool-operations/simple_transaction.html. | |
#author :Dimitry Kudryavtsev ([email protected]) | |
#version :1 | |
#usage :./simple-transaction.sh <File with address of the sending wallet, payment.addr> <File with address of the receiving address payment2.addr> <File with the skey (payment.skey) sending wallet> | |
#notes :For updates, please look at https://gist.github.com/dk8996/6dc32b3dd8dab6786e6f15d64a18267e | |
#============================================================================== | |
ONE_M=1000000 | |
if [ $# -eq 3 ]; then | |
echo "Your command line contains $# arguments" | |
else | |
echo "Error: Your command line contains no arguments" | |
exit 1 | |
fi | |
echo "Environment Variables" | |
echo "Path: $PATH" | |
echo "User: $USER" | |
echo "==============" | |
file_check_exit() { | |
local FILE=$1 | |
if [ -f "$FILE" ]; then | |
echo "$FILE exists." | |
else | |
echo "Error: $FILE does not exist." | |
exit 1 | |
fi | |
} | |
tx_table(){ | |
local count=-2 | |
local table | |
local col=$1 | |
while read line; do | |
count=$((count+1)) | |
tx_row=(`echo $line | cut -d " " --output-delimiter=" " -f 1-`) | |
local adaAmount=$((tx_row[2]/ONE_M)) | |
if [ $count -eq $1 ]; then | |
echo "Selected --- $line --- $adaAmount" | |
return 0 | |
fi | |
done | |
} | |
file_check_exit $1 | |
file_check_exit $2 | |
file_check_exit $3 | |
FROM_PATH_PAYMENT_ADDR=$1 | |
TO_PATH_PAYMENT_ADDR=$2 | |
FROM_PATH_PAYMENT_KEY=$3 | |
echo "FROM payment.addr file path: $FROM_PATH_PAYMENT_ADDR" | |
echo "TO payment2.addr file path: $TO_PATH_PAYMENT_ADDR" | |
echo "FROM payment.skey file path: $FROM_PATH_PAYMENT_KEY" | |
echo "Create protocol.json" | |
cardano-cli shelley query protocol-parameters \ | |
--mainnet \ | |
--out-file protocol.json | |
cardano-cli shelley query utxo \ | |
--address $(cat $FROM_PATH_PAYMENT_ADDR) \ | |
--mainnet | |
echo " " | |
read -p "Select one TxHash [1 to ..] (the first TxHash is 1, second TxHash 2, ...)? " txhash_num | |
echo " " | |
tx_table < <(cardano-cli shelley query utxo \ | |
--address $(cat $FROM_PATH_PAYMENT_ADDR) \ | |
--mainnet) $txhash_num | |
tx_hash=${tx_row[0]} | |
tx_tx=${tx_row[1]} | |
tx_balance=${tx_row[2]} | |
echo "Selected TxHash: $tx_hash" | |
adaAmount=$((tx_row[2]/ONE_M)) | |
echo "Selected Amount: $tx_balance Lovelace, $adaAmount ADA" | |
cardano-cli shelley transaction build-raw \ | |
--tx-in "$tx_hash#$tx_tx" \ | |
--tx-out $(cat $TO_PATH_PAYMENT_ADDR)+0 \ | |
--tx-out $(cat $FROM_PATH_PAYMENT_ADDR)+0 \ | |
--ttl 0 \ | |
--fee 0 \ | |
--out-file tx.draft | |
echo " " | |
fee_text=$(cardano-cli shelley transaction calculate-min-fee \ | |
--tx-body-file tx.draft \ | |
--tx-in-count 1 \ | |
--tx-out-count 2 \ | |
--witness-count 1 \ | |
--byron-witness-count 0 \ | |
--mainnet \ | |
--protocol-params-file protocol.json) | |
echo "Fee will be: $fee_text" | |
fee_text_arr=(`echo $fee_text | cut -d " " --output-delimiter=" " -f 1-`) | |
fee=${fee_text_arr[0]} | |
max_amount=$(((tx_balance-fee)/ONE_M)) | |
echo " " | |
read -p "Amount to send, you can only send max of $((max_amount)), (ADA)? " send_amount_ada | |
echo " " | |
send_amount=$((send_amount_ada*ONE_M)) | |
change_send_back=$((tx_balance - fee - send_amount)) | |
change_send_back_ada=$((change_send_back/ONE_M)) | |
echo "Change Back: $change_send_back Lovelace, $change_send_back_ada ADA" | |
tip_text=$(cardano-cli shelley query tip --mainnet) | |
tip_text_arr=(`echo $tip_text | cut -d "," --output-delimiter=" " -f 1-`) | |
slotNo=${tip_text_arr[6]} | |
ttl=$((slotNo+200)) | |
echo " " | |
echo "Current Slot: $slotNo, Setting ttl to: $ttl" | |
to_addr=$(cat $TO_PATH_PAYMENT_ADDR) | |
from_addr=$(cat $FROM_PATH_PAYMENT_ADDR) | |
echo " " | |
echo "Send: $send_amount_ada (ADA)" | |
echo "From address: $from_addr" | |
echo "To address: $to_addr" | |
cardano-cli shelley transaction build-raw \ | |
--tx-in "$tx_hash#$tx_tx" \ | |
--tx-out "$to_addr+$send_amount" \ | |
--tx-out "$from_addr+$change_send_back" \ | |
--ttl $ttl \ | |
--fee $fee \ | |
--out-file tx.raw | |
cardano-cli shelley transaction sign \ | |
--tx-body-file tx.raw \ | |
--signing-key-file $FROM_PATH_PAYMENT_KEY \ | |
--mainnet \ | |
--out-file tx.signed | |
echo " " | |
echo "You will need to type in the following command to finish your tx:" | |
echo "cardano-cli shelley transaction submit --tx-file tx.signed --mainnet" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your script is very well structured and work ok.
Tested in ubuntu Ubuntu 21.04 in cardano local node testnet
Transcode script in js at
https://github.com/stergiosa/cardano_ADA/blob/main/transactionTestAda.mjs
Regards Stergiosa