-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_bitcoin.go
79 lines (70 loc) · 2.8 KB
/
update_bitcoin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package toolslib
import (
"bytes"
"encoding/json"
"github.com/btcsuite/btcd/btcec"
"github.com/deso-smart/deso-backend/v3/routes"
"github.com/deso-smart/deso-core/v3/lib"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
)
// _generateUnsignedBTCPriceUpdate...
func _generateUnsignedBTCPriceUpdate(updaterPubKey *btcec.PublicKey, newUSDCentsPerBitcoin uint64,
params *lib.DeSoParams, node string) (*routes.UpdateGlobalParamsResponse, error) {
endpoint := node + routes.RoutePathUpdateGlobalParams
// Setup request
payload := &routes.UpdateGlobalParamsRequest{
UpdaterPublicKeyBase58Check: lib.PkToString(updaterPubKey.SerializeCompressed(), params),
USDCentsPerBitcoin: int64(newUSDCentsPerBitcoin),
MinFeeRateNanosPerKB: 1000,
}
postBody, err := json.Marshal(payload)
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedBTCPriceUpdate() failed to marshal json")
}
postBuffer := bytes.NewBuffer(postBody)
// Execute request
resp, err := http.Post(endpoint, "application/json", postBuffer)
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedBTCPriceUpdate() failed to execute request")
}
if resp.StatusCode != 200 {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, errors.Errorf("_generateUnsignedBTCPriceUpdate(): Received non 200 response code: "+
"Status Code: %v Body: %v", resp.StatusCode, string(bodyBytes))
}
// Process response
updateBitcoinUSDExchangeRateResponse := routes.UpdateGlobalParamsResponse{}
err = json.NewDecoder(resp.Body).Decode(&updateBitcoinUSDExchangeRateResponse)
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedBTCPriceUpdate(): failed decoding body")
}
err = resp.Body.Close()
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedBTCPriceUpdate(): failed closing body")
}
return &updateBitcoinUSDExchangeRateResponse, nil
}
// UpdateBitcoinUSDExchangeRate...
func UpdateBitcoinUSDExchangeRate(updaterPubKey *btcec.PublicKey, updaterPrivKey *btcec.PrivateKey, newUSDCentsPerBitcoin uint64,
params *lib.DeSoParams, node string) error {
// Request an unsigned transaction from the node
unsignedUpdateBitcoinUSDExchangeRate, err := _generateUnsignedBTCPriceUpdate(updaterPubKey, newUSDCentsPerBitcoin, params, node)
if err != nil {
return errors.Wrap(err, "UpdateBitcoinUSDExchangeRate() failed to generate unsigned transaction")
}
txn := unsignedUpdateBitcoinUSDExchangeRate.Transaction
// Sign the transaction
signature, err := txn.Sign(updaterPrivKey)
if err != nil {
return errors.Wrap(err, "UpdateBitcoinUSDExchangeRate() failed to sign the transaction")
}
txn.Signature.SetSignature(signature)
// Submit the transaction to the node
err = SubmitTransactionToNode(txn, node)
if err != nil {
return errors.Wrap(err, "UpdateBitcoinUSDExchangeRate() failed to submit transaction")
}
return nil
}