Bitcoin Forum
November 02, 2024, 07:04:47 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: bash script to automate proccess of sending BTC to other addresses  (Read 189 times)
darkv0rt3x (OP)
Hero Member
*****
Offline Offline

Activity: 1274
Merit: 681


I rather die on my feet than to live on my knees


View Profile
August 29, 2019, 09:49:34 PM
Merited by LoyceV (2)
 #1

Hello guys.

I'm trying to automate the process of sending BTC to a couple of addresses with a bash script.

The addresses are in a file and I load each address into an array. This part is done.

How can I build/construct the bitcoin-cli sendmany command knowing that the number of addresses in this file may change? I mean, today I can have 3 addresses in that file, tomorrow I can have 5, the day after I can have only one address and so on.

Or what other approach can I try?

Bitcoin is energy. Bitcoin is freedom
I rather die on my feet than living on my knees!
DaveF
Legendary
*
Offline Offline

Activity: 3654
Merit: 6660


Crypto Swap Exchange


View Profile WWW
August 30, 2019, 02:09:44 AM
Merited by LoyceV (2)
 #2

It's a bit of a cheat, BUT if you can format the text file the way bitcoin-cli wants it and start it with the bitcoin-cli command all you have to do then is run the file.

bitcoin-cli sendmany "" "{\"12345678ZZStgN5qWduEHyGrsSw72WEiTZ\":0.01,\"12345678ZZStgN5qWduEHyGrsSw72WEiTZ\":0.02}"

The other option is to send them as individual transactions:

bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.001
bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.002
bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.003

It's a bit easier as you are not needing the \ and { and everything else.
just start each line with bitcoin-cli sendtoaddress  and add the address and the amount and run it.

-Dave

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
darkv0rt3x (OP)
Hero Member
*****
Offline Offline

Activity: 1274
Merit: 681


I rather die on my feet than to live on my knees


View Profile
September 01, 2019, 12:31:36 PM
 #3

It's a bit of a cheat, BUT if you can format the text file the way bitcoin-cli wants it and start it with the bitcoin-cli command all you have to do then is run the file.

bitcoin-cli sendmany "" "{\"12345678ZZStgN5qWduEHyGrsSw72WEiTZ\":0.01,\"12345678ZZStgN5qWduEHyGrsSw72WEiTZ\":0.02}"

The other option is to send them as individual transactions:

bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.001
bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.002
bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.003

It's a bit easier as you are not needing the \ and { and everything else.
just start each line with bitcoin-cli sendtoaddress  and add the address and the amount and run it.

-Dave


I'm taking another approach but your's is also an option as I might need to add different amounts to each address and also the change address!
But I'm already half way done to the first version. Then, to change to other format will be easier!

To send individual transactions will make more fees and if I can o it with a single transaction, fees will be less, right? The idea is also to be able to set several outputs into a single transaction to save in fees!

Bitcoin is energy. Bitcoin is freedom
I rather die on my feet than living on my knees!
DaveF
Legendary
*
Offline Offline

Activity: 3654
Merit: 6660


Crypto Swap Exchange


View Profile WWW
September 01, 2019, 01:16:21 PM
 #4

It's a bit of a cheat, BUT if you can format the text file the way bitcoin-cli wants it and start it with the bitcoin-cli command all you have to do then is run the file.

bitcoin-cli sendmany "" "{\"12345678ZZStgN5qWduEHyGrsSw72WEiTZ\":0.01,\"12345678ZZStgN5qWduEHyGrsSw72WEiTZ\":0.02}"

The other option is to send them as individual transactions:

bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.001
bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.002
bitcoin-cli sendtoaddress "12345678ZZStgN5qWduEHyGrsSw72WEiT" 0.003

It's a bit easier as you are not needing the \ and { and everything else.
just start each line with bitcoin-cli sendtoaddress  and add the address and the amount and run it.

-Dave


I'm taking another approach but your's is also an option as I might need to add different amounts to each address and also the change address!
But I'm already half way done to the first version. Then, to change to other format will be easier!

To send individual transactions will make more fees and if I can o it with a single transaction, fees will be less, right? The idea is also to be able to set several outputs into a single transaction to save in fees!

Fees are based on the size of the transaction (data size not BTC) which is usually based on the number of inputs, but many individual ones will most likely be more expensive to send.

I have used the method quoted above to do things for myself just because it is human readable.
I did all the processing in a DB, then dumped what I needed to text and ran a bitcoin-cli command against it.

It was a bit more programming work on my part, but I could look at a text file to see what happened instead of going through SQL logs trying to figure out what happened.
As you start to do more things the text file approach does become more labor YMMV.

And the obligatory, test it against testnet coins 1st or some alt-coin that costs almost nothing. So long as it's based off one of the newer core releases it should be fine. But if you mess something up your didn't loose real money.

-Dave

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
linenoise
Sr. Member
****
Offline Offline

Activity: 309
Merit: 290


View Profile WWW
September 02, 2019, 04:41:39 AM
 #5

If everything is in an array you're almost there. Loop through the array to construct the sendmany variable, adding each address/value.

Each iteration add a comma to the end of the constructed variable, except for the last pair as it is not required.

Then perform the actual sendmany using the variable you have just created.




posmonero
Newbie
*
Offline Offline

Activity: 8
Merit: 2


View Profile
September 02, 2019, 04:57:00 AM
Merited by LoyceV (2)
 #6

addresses.txt:
Code:
<address1> <amount1>
<address2> <amount2>
...

send.sh:
Code:
#!/bin/bash

arg=''
while read line; do
    address=$(echo $line | cut -d' ' -f1)
    amount=$(echo $line | cut -d' ' -f2)
    if ! [ -z "$arg" ]; then arg="$arg,"; fi
    arg=$arg'"'$address'":'$amount
done <<< "$(cat addresses.txt)"

bitcoin-cli sendmany "" "{$arg}"

Runs:
Code:
bitcoin-cli sendmany "" '{"<address1>":<amount1>,"<address2>":<amount2>,"...":...}'
darkv0rt3x (OP)
Hero Member
*****
Offline Offline

Activity: 1274
Merit: 681


I rather die on my feet than to live on my knees


View Profile
December 06, 2020, 04:42:11 PM
 #7

Ohhh.. I even forgot this thread of mine.

Thank you for that example. It's exactly what I needed and with 1/5 of the code I did. Smiley
I completed this small task I had in the past but I might update it with this code, actually.

Bitcoin is energy. Bitcoin is freedom
I rather die on my feet than living on my knees!
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!