Bitcoin

bitcoin core – Using sendtoaddress fails when using json-rpc, and I don’t know why.

The steps I took were to first load the wallet with another method that works, then check the wallet balance with another method that works, and then call this sendbitcoin method (below). You can see that the Bitcoin server received the message but returns 500.

Using bitcoin-cli works, but you need to add the -rpcwallet flag. I understand that there is no need to use the flag since the wallet is loaded. My wallet is not encrypted so I don’t think I need to use a password.

Here is my bitcoin.conf file:

rpcuser=rpcuser
rpcpassword=rpcpassword
testnet=1
prune=550
server=1
dbcache=1000
(test)
debug=mempool
debug=rpc
dbcache=1000
rpcport=8332
rpcbind=0.0.0.0
rpcallowip=172.19.0.0/16
minconf=1
fallbackfee=0.00000001

And here’s how:

func (w *Wallet) SendBitcoin(sendTo string, amount float64, coachID uint) (BitcoinTransaction, error) {

username := os.Getenv("RPC_USER")
password := os.Getenv("RPC_PASSWORD")
rpcURL := os.Getenv("BITCOIN_NODE_HOST")

if username == "" || password == "" || rpcURL == "" 
    log.Error("RPC_USER, RPC_PASSWORD, or BITCOIN_NODE_HOST environment variables are not set")
    return BitcoinTransaction, errors.New("RPC_USER, RPC_PASSWORD, or BITCOIN_NODE_HOST environment variables are not set")


// Prepare the RPC request payload
requestBody := map(string)interface
    "jsonrpc": "1.0",
    "method":  "sendtoaddress",
    "params":  ()interfacesendTo, amount,
    "id":      1,


logrus.Printf("request body : %v", requestBody)
logrus.Printf("Sending on wallet %v", w.Name)

jsonData, err := json.Marshal(requestBody)
if err != nil 
    log.Errorf("failed to marshal JSON request: %v", err)
    return BitcoinTransaction, fmt.Errorf("failed to marshal JSON request: %v", err)


logrus.Printf("JSON body : %v", jsonData)

client := &http.Client

req, err := http.NewRequest("POST", rpcURL, bytes.NewBuffer(jsonData))
if err != nil 
    log.Errorf("failed to create HTTP request: %v", err)
    return BitcoinTransaction, fmt.Errorf("failed to create HTTP request: %v", err)


req.SetBasicAuth(username, password)
req.Header.Set("Content-Type", "application/json")

// Send HTTP request
resp, err := client.Do(req)
if err != nil 
    log.Errorf("failed to send HTTP request: %v", err)
    return BitcoinTransaction, fmt.Errorf("failed to send HTTP request: %v", err)

//...

Log

(00) bitcoin      | 2024-02-22T22:36:59Z (rpc) ThreadRPCServer 
 method=loadwallet user=rpcuser
(00) rest-server  | time="2024-02-22T22:36:59Z" level=info msg="balance 
= 0.0003933699999999999, amount = 0.0001"
(00) bitcoin      | 2024-02-22T22:36:59Z Using SQLite Version 3.38.5
(00) bitcoin      | 2024-02-22T22:36:59Z Using wallet 
 /home/bitcoin/.bitcoin/testnet3/wallets/111-20240221
(00) bitcoin      | 2024-02-22T22:36:59Z (rpc) ThreadRPCServer 
method=sendtoaddress user=rpcuser
(00) rest-server  | time="2024-02-22T22:36:59Z" level=info msg="request 
 body : map(id:1 jsonrpc:1.0 method:sendtoaddress params: 
(tb1qljunwv4hv8nx3mwsrup4fse5zhahyjtmx0fwzl 0.0001 ))"
(00) rest-server  | time="2024-02-22T22:37:00Z" level=info 
msg="unexpected status code: 500"

Related Articles

Back to top button