Skip to content

Commit

Permalink
add send_token_multi_addresses.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
rocklee44 committed Aug 11, 2024
1 parent 5ab2cf4 commit 905525b
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This document is a step-by-step guide to :
|---cardano-node-ipc # store node socket file node.socket
|---configuration # store network , topology and eras configs
|---cardano-wallet # cardano wallet binaries
|---tokens # store payment address , policyID of token
```
2. Compile cardano wallet binaries your self or use downloaded latest pre-built binaries of cardano-wallet in this repo, please refer https://developers.cardano.org/docs/get-started/installing-cardano-wallet (ignore this step if you clone my repo)

Expand Down Expand Up @@ -44,6 +45,18 @@ bash-4.4# cardano-cli query tip --testnet-magic 1
"slot": 62502587,
"block": 3680370
}

Or use setenv.sh
source setenv.sh
$CARDANO_CLI query tip $NET
{
"block": 2574131,
"epoch": 160,
"era": "Babbage",
"hash": "e9dbd32ceafeca281f6ea7f501e5eb4c4794ba781ee02613f734dac9eeaac173",
"slot": 67690344,
"syncProgress": "100.00"
}
```

## Do lab with shell script
Expand Down Expand Up @@ -247,3 +260,39 @@ ca4e391cef69af129426fd896f4669ff82d6df2ad922844ba31cff0a1ba19370 0 99
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
```

9. Send ADA and token to multi wallet
```
list.txt
cat list.txt
addr_test1vz6aay995sa8rdaajlzxlkcldjg9c5zxp7j298a8dhqchrgkasc0m 0 100
addr_test1vq88zrwjrqdrzpuv2vp4fxsx2r2skyxkkvlkrdvlyxs9w0gh92vyl 0 200
addr_test1vrp5gyf7tv895uns6gyfjh8f0w5r5j66s20y7n877v70hfsjm9n5q 0 300
```

```
./send_token_multi_addresses.sh ./list.txt Chuong_Test_1 13d4032273c910b6877e968b4cc11c3264ac8ac1ece4d2e1d9e5c2403517c774 0
raw_transaction is : /opt/preprod/Run-cardano-node-on-docker/cardano-wallet/cardano-cli transaction build-raw --fee 0 --tx-in 13d4032273c910b6877e968b4cc11c3264ac8ac1ece4d2e1d9e5c2403517c774#0 --tx-out addr_test1vz6aay995sa8rdaajlzxlkcldjg9c5zxp7j298a8dhqchrgkasc0m+2000000+"100 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31" --tx-out addr_test1vq88zrwjrqdrzpuv2vp4fxsx2r2skyxkkvlkrdvlyxs9w0gh92vyl+2000000+"200 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31" --tx-out addr_test1vrp5gyf7tv895uns6gyfjh8f0w5r5j66s20y7n877v70hfsjm9n5q+2000000+"300 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31" --tx-out addr_test1vrkq5fk3ulkplsy5sd2h97peka0tu2g4mljjp6w8wl24eesm5fy8z+9993817779+"99999400 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31" --out-file tokens/rec_matx.raw
Rebuilt raw_transaction is : /opt/preprod/Run-cardano-node-on-docker/cardano-wallet/cardano-cli transaction build-raw --fee 192649 --tx-in 13d4032273c910b6877e968b4cc11c3264ac8ac1ece4d2e1d9e5c2403517c774#0 --tx-out addr_test1vz6aay995sa8rdaajlzxlkcldjg9c5zxp7j298a8dhqchrgkasc0m+2000000+"100 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31" --tx-out addr_test1vq88zrwjrqdrzpuv2vp4fxsx2r2skyxkkvlkrdvlyxs9w0gh92vyl+2000000+"200 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31" --tx-out addr_test1vrp5gyf7tv895uns6gyfjh8f0w5r5j66s20y7n877v70hfsjm9n5q+2000000+"300 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31" --tx-out addr_test1vrkq5fk3ulkplsy5sd2h97peka0tu2g4mljjp6w8wl24eesm5fy8z+9993625130+"99999400 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31" --out-file tokens/rec_matx.raw
Transaction successfully submitted.
Sent token successfully
$CARDANO_CLI query utxo --address $address $NET
TxHash TxIx Amount
--------------------------------------------------------------------------------------
42f4a8152ae7a495755aaf6e0e73662111dc92d7051d5f6ade9787bcbaf3cadc 3 9993625130 lovelace + 99999400 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31 + TxOutDatumNone
$CARDANO_CLI query utxo --address addr_test1vz6aay995sa8rdaajlzxlkcldjg9c5zxp7j298a8dhqchrgkasc0m $NET
TxHash TxIx Amount
--------------------------------------------------------------------------------------
42f4a8152ae7a495755aaf6e0e73662111dc92d7051d5f6ade9787bcbaf3cadc 0 2000000 lovelace + 100 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31 + TxOutDatumNone
$CARDANO_CLI query utxo --address addr_test1vq88zrwjrqdrzpuv2vp4fxsx2r2skyxkkvlkrdvlyxs9w0gh92vyl $NET
TxHash TxIx Amount
--------------------------------------------------------------------------------------
42f4a8152ae7a495755aaf6e0e73662111dc92d7051d5f6ade9787bcbaf3cadc 1 2000000 lovelace + 200 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31 + TxOutDatumNone
$CARDANO_CLI query utxo --address addr_test1vrp5gyf7tv895uns6gyfjh8f0w5r5j66s20y7n877v70hfsjm9n5q $NET
TxHash TxIx Amount
--------------------------------------------------------------------------------------
42f4a8152ae7a495755aaf6e0e73662111dc92d7051d5f6ade9787bcbaf3cadc 2 2000000 lovelace + 300 d7dcafac1919b7dbc6afa295c9ab793eeba55e927a6d94ace67a9067.4368756f6e675f546573745f31 + TxOutDatumNone
```
3 changes: 3 additions & 0 deletions list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
addr_test1vz6aay995sa8rdaajlzxlkcldjg9c5zxp7j298a8dhqchrgkasc0m 0 100
addr_test1vq88zrwjrqdrzpuv2vp4fxsx2r2skyxkkvlkrdvlyxs9w0gh92vyl 0 200
addr_test1vrp5gyf7tv895uns6gyfjh8f0w5r5j66s20y7n877v70hfsjm9n5q 0 300
119 changes: 119 additions & 0 deletions send_token_multi_addresses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash

for file in ./functions/*; do
if [ -f "$file" ] && [ "${file##*.}" == "sh" ]; then
source "$file"
fi
done

check_cardano_env

NET="--testnet-magic 1"

if [ "$#" -ne 4 ]; then
echo "Usage: $0 <file_path> <token_name> <txhash> <txix>"
exit 1
fi

file_path=$1
token_name=$2
txhash=$3
txix=$4

check_wallet_existence
check_policy_existence

address=$(cat tokens/payment.addr)
selected_utxo=$($CARDANO_CLI query utxo --address $address $NET)
policyid=$(cat tokens/policy/policyID)
fee="0"
receiver_output="2000000" # Fixed lovelace amount
tokennameec16=$(echo -n "$token_name" | xxd -ps | tr -d '\n')

# Get funds
funds=$(awk -v th="$txhash" -v ix="$txix" '
$1==th && $2==ix {
print $3;
}' <<<"$selected_utxo")

token_amount=$(awk -v th="$txhash" -v ix="$txix" '
$1==th && $2==ix {
if ($5 == "+" && $6 == "TxOutDatumNone") {
print "The TxHash , TxIx you selected is invalid.";
}
else if ($5 == "+" && $6 ~ /^[0-9]+$/ && $7 == "'$policyid.$tokennameec16'") {
print $6;
}
}' <<<"$selected_utxo")

if echo "$token_amount" | grep -q "The TxHash , TxIx you selected"; then
echo "$token_amount"
exit 1 # exit program
fi

# Initialize raw transaction
raw_transaction="$CARDANO_CLI transaction build-raw --fee $fee --tx-in $txhash#$txix"

# Initialize total cost of lovelace and tokens
totalcostlovelace=0
totalcosttoken=0

# Generate tx_out for each line in the file
while IFS=" " read -r receiver lovelace tokenamt; do
receiver_output_adjusted=$((receiver_output + lovelace))
raw_transaction+=" --tx-out $receiver+$receiver_output_adjusted+\"$tokenamt $policyid.$tokennameec16\""
totalcostlovelace=$((totalcostlovelace + receiver_output_adjusted))
totalcosttoken=$((totalcosttoken + tokenamt))
done < "$file_path"

# Calculate remaining tokens and output balance
tokenremain=$((token_amount - totalcosttoken))
output=$((funds - fee - totalcostlovelace))

# Add change output to the raw transaction
raw_transaction+=" --tx-out $address+$output+\"$tokenremain $policyid.$tokennameec16\" --out-file tokens/rec_matx.raw"

echo "raw_transaction is : $raw_transaction"
eval $raw_transaction

# Calculate fee
fee=$($CARDANO_CLI transaction calculate-min-fee --tx-body-file tokens/rec_matx.raw --tx-in-count 1 --tx-out-count $(($(wc -l < "$file_path")+1)) --witness-count 1 $NET --protocol-params-file tokens/protocol.json | cut -d " " -f1)

# Update output balance after fee deduction
output=$((funds - fee - totalcostlovelace))

# Rebuild transaction with updated fee and output balance
raw_transaction="$CARDANO_CLI transaction build-raw --fee $fee --tx-in $txhash#$txix"

# Rebuild tx_outs
while IFS=" " read -r receiver lovelace tokenamt; do
receiver_output_adjusted=$((receiver_output + lovelace))
raw_transaction+=" --tx-out $receiver+$receiver_output_adjusted+\"$tokenamt $policyid.$tokennameec16\""
done < "$file_path"

# Add change output
raw_transaction+=" --tx-out $address+$output+\"$tokenremain $policyid.$tokennameec16\" --out-file tokens/rec_matx.raw"

echo "Rebuilt raw_transaction is : $raw_transaction"
eval $raw_transaction

# Sign transaction
$CARDANO_CLI transaction sign --signing-key-file tokens/payment.skey $NET --tx-body-file tokens/rec_matx.raw --out-file tokens/rec_matx.signed

# Submit transaction
$CARDANO_CLI transaction submit --tx-file tokens/rec_matx.signed $NET

# Check submit result
submit_exit_code=$?
if [ $submit_exit_code -eq 0 ]; then
echo "Sent token successfully"
# Display wallet balance after minting tokens
sleep 10
echo "Wallet balance:"
$CARDANO_CLI query utxo --address $address $NET
exit 0
else
echo "Error submitting transaction. Exit code: $submit_exit_code"
exit $submit_exit_code
fi

6 changes: 6 additions & 0 deletions setenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

export NET="--testnet-magic 1"
export CARDANO_NODE_SOCKET_PATH=`pwd`"/cardano-node-ipc/node.socket"
export CARDANO_CLI=`pwd`"/cardano-wallet/cardano-cli"
export address="addr_test1vrkq5fk3ulkplsy5sd2h97peka0tu2g4mljjp6w8wl24eesm5fy8z"

0 comments on commit 905525b

Please sign in to comment.