bitcoin core – Using sendtoaddress is failing using json-rpc and I don’t know why

[ad_1]

The steps i followed are i first load the wallet in a different method which works, then i check the balance of the wallet in a different method which works, and then I call this sendbitcoin method(below). I can see that the bitcoind server gets the message but it returns with a 500.

If i use bitcoin-cli it works however i need to add a -rpcwallet flag. It’s my understanding that since the wallet has been loaded i shouldn’t need to use flag. My wallets aren’t encrypted so i don’t believe i need to use a passphrase.

My bitcoin.conf file looks like this.

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 the method looks like.

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":  []interface{}{sendTo, 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)
}
//...

logs

[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"

[ad_2]

Source link

Leave a Comment