2

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"
1
  • Can you see the error message from the Bitcoin Core debug.log? Log file can be found in your Bitcoin datadir, for example in Linux the testnet logs are in ~/.bitcoin/testnet/debug.log Commented Feb 23 at 7:02

1 Answer 1

0

From the provided logs, it seems that your code is reaching the point where it sends the sendtoaddress RPC request to the Bitcoin node. However, the issue might be related to the response handling part of your code, which is not included in the snippet you provided.

You should continue the logs and inspect the response received from the Bitcoin node. Check the status code, and if it's not in the 2xx range, it could indicate an issue with the request. Additionally, parse and analyze the response body to get more information about the problem.

Here are a few steps to help you debug:

After resp, err := client.Do(req), check the HTTP status code: if resp.StatusCode < 200 || resp.StatusCode >= 300 { log.Errorf("Non-successful HTTP response: %v", resp.Status) // Handle the error appropriately }

Read and parse the response body: body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Errorf("Failed to read response body: %v", err) // Handle the error appropriately }

log.Printf("Response Body: %s", body)

This should give you more information about what might be going wrong with the sendtoaddress RPC call. Keep an eye out for any error messages or additional details in the response body that can help diagnose the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.