How to create a bash script to send tADA?

I’m using the Cardano-Wallet server to send tADA and it works just fine when pasting directly from the command prompt.

curl --request POST \
  --url http://localhost:1337/v2/wallets/5076b34c6949dbd150eb9c39039037543946bdce/transactions \
  --header 'Content-Type: application/json' \
  --data '{
    "passphrase": "test123456",
    "payments": [
        {
            "address": "addr_test1qzyfnjk3zmgzmvnnvnpeguv6se2ptjj3w3uuh30llqe5xdtzdduxxvke8rekwukyn0qt9g5pahasrnrdmv7nr86x537qxdgza0",
            "amount": {
                "quantity": 250000000,
                "unit": "lovelace"
            }
        }
    ]
}'

Now i want to put it inside a script so i can issue this command at the prompt:
pay.sh from_walletID to_wallet_address tADA_amount

Here’s the content of pay.sh:

URL="http://localhost:1338/v2/wallets/$1/transactions"
to_address="$2"
tADA_amount=$3

curl --request POST \
  --url "$URL" \
  --header 'Content-Type: application/json' \
  --data '{
    "passphrase": "test123456",
    "payments": [
        {
            "address": "$to_address",
            "amount": {
                "quantity": $tADA_amount,
                "unit": "lovelace"
            }
        }
    ]
}'

When I issue the script i get this error:
{"message":"I couldn't understand the content of your message. If your message is intended to be in JSON format, please check that the JSON is valid.","code":"bad_request"}

So I tried manually replacing variables $to_address and $tADA_amount with real values and the script works fine. It looks like the $tADA_amount is being read as string when it’s expected to be a number while the $to_address is already a string but still gets an error.

So how do I correctly format the variables $to_address and $tADA_amount?

You can put additional single quotes outside the variable references… so the long literal string ends before the variable reference begins, and then begins again after the variable is inserted, e.g.:

           "address": "'$to_address'",
           "quantity": '$tADA_amount',

Note that if any of these variables could contain a space (which in this syntax it wouldn’t, but in general it might) you’d also have to put double-quotes around the variable references… otherwise any white space in the middle of a variable would break the long string into multiple arguments (here, only the first one would be passed to --data). So the general best practice would be:

           "address": "'"$to_address"'",
           "quantity": '"$tADA_amount"',
1 Like

Thank you! That solved my script error.

1 Like