bawejakunal
bawejakunal

Reputation: 1708

Bitcoin: error parsing json in bashscript to send BTC using sendmany command

I am simulating a bitcoin network on my machine for which I have written a bash script to simulate the transactions between the nodes. When I use the sendmany option of bitcoin-cli to send transactions I am getting an json parsing error reported by bitcoin-cli.

Code:

#!/bin/bash
MAX_NODES=2
MY_PATH=/home/ubuntu/test
CLIENT=/usr/local/bin/bitcoin-cli

declare -a addr

function fcomp() {
        /usr/bin/awk -v n1=$1 -v n2=$2 'BEGIN{ if (n1>n2) exit 0; exit 1}'
}

json="'{"

#get addresses to send
for ((i = 1; i <= MAX_NODES; i++));
do
    addr[$i-1]="$($CLIENT -regtest -rpcport=$((16500 + $i)) -datadir=$MY_PATH/$i/ getnewaddress myaccount)"
    if [ "$i" -lt "$MAX_NODES" ]
    then
        json="$json\"${addr[$i-1]}\":0.00001, "
    else
        json="$json\"${addr[$i-1]}\":0.00001"
    fi
done

json="$json}'"
echo $json

#loop to send money to other nodes
for ((i = 1; i <= MAX_NODES; i++));
do
        balance=`$CLIENT -regtest -rpcport=$((16500 + $i)) -datadir=$MY_PATH/$i/ getbalance`
        if fcomp $balance 0.002; then
                $CLIENT -regtest -rpcport=$((16500 + $i)) -datadir=$MY_PATH/$i sendmany myaccount $json
    fi
done

echo json output:

'{"mj2FrDhEomSzyQtRoGY78oVRPcQs5L5o95":0.00001, "mkxnkT3kx9dsFS8V3qYydpL1o5F5MfwCvM":0.00001}'

This gives me an error as:

error: Error parsing JSON:'{"mj2FrDhEomSzyQtRoGY78oVRPcQs5L5o95":0.00001,

I tried all possible combinations of quotes, double quotes and escape sequences but failed. If I copy paste the output of echo $json to a manual bitcoin-cli sendmany command it works perfectly fine.

Upvotes: 2

Views: 945

Answers (1)

bawejakunal
bawejakunal

Reputation: 1708

Ok, so the json was not being passed to bitcoin-cli as I had expected, so I had to make 3 changes to my improper syntax.

json="'{" to json="{" , json="$json}'" to "json="$json}" and $CLIENT -regtest -rpcport=$((16500 + $i)) -datadir=$MY_PATH/$i sendmany myaccount $json to $CLIENT -regtest -rpcport=$((16500 + $i)) -datadir=$MY_PATH/$i sendmany myaccount "$json"

Upvotes: 1

Related Questions