Skip to content

Instantly share code, notes, and snippets.

@ilap
Last active May 22, 2020 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilap/6fd66340234b58b1a4dee058252fb0d7 to your computer and use it in GitHub Desktop.
Save ilap/6fd66340234b58b1a4dee058252fb0d7 to your computer and use it in GitHub Desktop.
FnF Exercise 2

Excercise 2

  1. Build simple transactions using the basic transaction mechanism;
  2. Sign transactions and confirm that the transaction is complete;
  3. Submit transactions to the Pioneer Blockchain;
  4. Verify that the transactions have been processed by inspecting the addresses that they have been sent to.

Build transactions

#!/bin/bash

PREFIX=addr
FROM="617190446876aed298ee207c6b5a335e832e2169a060b8167ef3ba9caff6fa3393"
AMOUNT=100000000


## Create To address
if [ ! -e "$PREFIX" ]; then
 cardano-cli shelley address key-gen \
     --signing-key-file "$PREFIX".skey \
     --verification-key-file "$PREFIX".vkey
fi

TO=$(cardano-cli shelley address build --payment-verification-key-file "$PREFIX.vkey" | tee "$PREFIX")

echo "To Address: $TO"

## 

#cd "$FNF_HOME" 
. "${FNF_HOME}/scripts/env"

# Get Input
TX=$(cardano-cli shelley query filtered-utxo --testnet-magic 42 --address "$FROM"  | grep "^[^- ]" | sort -k 2n | tail -1)
UTXO=$( echo "$TX" | awk '{ print $1 }')
ID=$( echo "$TX" | awk '{ print $2 }')
BALANCE=$( echo "$TX" | awk '{ print $3 }')
INPUT="${UTXO}#${ID}"


printf "Input: %s (%s)\n" "$INPUT" "$BALANCE"
# Calculate the fee

cardano-cli shelley query protocol-parameters \
     --testnet-magic 42 > protocol.json

FEE=$(cardano-cli shelley transaction calculate-min-fee \
--protocol-params-file protocol.json \
--tx-in-count 1 \
--tx-out-count 2 \
--ttl 500000 \
--testnet-magic 42 \
--signing-key-file ../../priv/fnf.skey | awk '{ print $2}')

CHANGE=$(( $BALANCE - $AMOUNT - $FEE ))

echo "Balance: $BALANCE, Amount: "$AMOUNT",  Change: $CHANGE, runTxCalculateMinFee: $FEE" 

if [ "$CHANGE" -lt 0 ]; then
	echo "Error in fees: change $CHANGE" >&2
	exit 127
fi

# Build Tx
cardano-cli shelley transaction build-raw \
	--tx-in "$INPUT" \
	--tx-out "${TO}+${AMOUNT}" \
	--tx-out "${FROM}+${CHANGE}" \
	--ttl 500000 \
	--fee "$FEE" \
	--tx-body-file tx

# Sign
cardano-cli shelley transaction sign \
    --tx-body-file tx \
    --signing-key-file ../../priv/fnf.skey \
    --tx-file signed-tx \
    --testnet-magic 42
# Submit
echo export CARDANO_NODE_SOCKET_PATH=/opt/cardano/fnf/sockets/node0.socket
echo cardano-cli shelley transaction submit --tx-file signed-tx --testnet-magic 42
echo cardano-cli shelley query filtered-utxo --testnet-magic 42 --address "$FROM"
echo cardano-cli shelley query filtered-utxo --testnet-magic 42 --address "$TO"

Sign transactions and confirm that the transaction is complete

cardano-cli shelley transaction sign \
    --tx-body-file tx \
    --signing-key-file ../../priv/fnf.skey \
    --tx-file signed-tx \
    --testnet-magic 42

Submit transactions to the Pioneer Blockchain

cardano-cli shelley transaction submit --tx-file signed-tx --testnet-magic 42

Verify that the transactions have been processed by inspecting the addresses that they have been sent to

# Before tx
$ cardano-cli shelley query filtered-utxo \
--testnet-magic 42 \
--address 617190446876aed298ee207c6b5a335e832e2169a060b8167ef3ba9caff6fa3393
                           TxHash                                 TxIx        Lovelace
----------------------------------------------------------------------------------------
7190446876aed298ee207c6b5a335e832e2169a060b8167ef3ba9caff6fa3393     0     1000000000000

# After TX
$ cardano-cli shelley query filtered-utxo --testnet-magic 42 --address 610ebc7f0c88d4e0a55244fcca657f1a582477bae0f45173e9c508d4da6e35abe1
                           TxHash                                 TxIx        Lovelace
----------------------------------------------------------------------------------------
e71770bb818cdcac4539e4bc996628d33e2209795a42e9ecec60b6ed8248b2cb     0         100000000

$ cardano-cli shelley query filtered-utxo --testnet-magic 42 --address 617190446876aed298ee207c6b5a335e832e2169a060b8167ef3ba9caff6fa3393
                           TxHash                                 TxIx        Lovelace
----------------------------------------------------------------------------------------
e71770bb818cdcac4539e4bc996628d33e2209795a42e9ecec60b6ed8248b2cb     1      999899831903
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment