Friday, November 22, 2024

bitcoin core – Utilizing sendtoaddress is failing utilizing json-rpc and I do not know why

The steps i adopted are i first load the pockets in a unique methodology which works, then i test the stability of the pockets in a unique methodology which works, after which I name this sendbitcoin methodology(beneath). I can see that the bitcoind server will get the message nevertheless it returns with a 500.

If i exploit bitcoin-cli it really works nevertheless i want so as to add a -rpcwallet flag. It is my understanding that for the reason that pockets has been loaded i should not want to make use of flag. My wallets aren’t encrypted so i do not consider i want to make use of a passphrase.

My bitcoin.conf file appears 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 tactic appears like.

func (w *Pockets) SendBitcoin(sendTo string, quantity 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 surroundings variables aren't set")
    return BitcoinTransaction{}, errors.New("RPC_USER, RPC_PASSWORD, or BITCOIN_NODE_HOST surroundings variables aren't set")
}

// Put together the RPC request payload
requestBody := map[string]interface{}{
    "jsonrpc": "1.0",
    "methodology":  "sendtoaddress",
    "params":  []interface{}{sendTo, quantity},
    "id":      1,
}

logrus.Printf("request physique : %v", requestBody)
logrus.Printf("Sending on pockets %v", w.Identify)

jsonData, err := json.Marshal(requestBody)
if err != nil {
    log.Errorf("didn't marshal JSON request: %v", err)
    return BitcoinTransaction{}, fmt.Errorf("didn't marshal JSON request: %v", err)
}

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

shopper := &http.Consumer{}

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

req.SetBasicAuth(username, password)
req.Header.Set("Content material-Kind", "software/json")

// Ship HTTP request
resp, err := shopper.Do(req)
if err != nil {
    log.Errorf("didn't ship HTTP request: %v", err)
    return BitcoinTransaction{}, fmt.Errorf("didn't ship HTTP request: %v", err)
}
//...

logs

[00] bitcoin      | 2024-02-22T22:36:59Z [rpc] ThreadRPCServer 
 methodology=loadwallet consumer=rpcuser
[00] rest-server  | time="2024-02-22T22:36:59Z" stage=data msg="stability 
= 0.0003933699999999999, quantity = 0.0001"
[00] bitcoin      | 2024-02-22T22:36:59Z Utilizing SQLite Model 3.38.5
[00] bitcoin      | 2024-02-22T22:36:59Z Utilizing pockets 
 /house/bitcoin/.bitcoin/testnet3/wallets/111-20240221
[00] bitcoin      | 2024-02-22T22:36:59Z [rpc] ThreadRPCServer 
methodology=sendtoaddress consumer=rpcuser
[00] rest-server  | time="2024-02-22T22:36:59Z" stage=data msg="request 
 physique : map[id:1 jsonrpc:1.0 method:sendtoaddress params: 
[tb1qljunwv4hv8nx3mwsrup4fse5zhahyjtmx0fwzl 0.0001 ]]"
[00] rest-server  | time="2024-02-22T22:37:00Z" stage=data 
msg="surprising standing code: 500"

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles