-
Notifications
You must be signed in to change notification settings - Fork 2
/
transactions.go
44 lines (39 loc) · 1.26 KB
/
transactions.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
package toolslib
import (
"bytes"
"encoding/hex"
"encoding/json"
"github.com/deso-smart/deso-backend/v2/routes"
"github.com/deso-smart/deso-core/v2/lib"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
)
// SubmitTransactionToNode...
func SubmitTransactionToNode(txn *lib.MsgDeSoTxn, node string) error {
endpoint := node + routes.RoutePathSubmitTransaction
// Encode the signed transaction to hex
txnBytes, err := txn.ToBytes(false)
if err != nil {
return errors.Wrap(err, "SubmitTransactionToServer() failed to convert txn to bytes")
}
txnHex := hex.EncodeToString(txnBytes)
// Setup request
payload := &routes.SubmitTransactionRequest{txnHex}
postBody, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "SubmitTransactionToServer() failed to marshal struct")
}
postBuffer := bytes.NewBuffer(postBody)
// Execute request
resp, err := http.Post(endpoint, "application/json", postBuffer)
if err != nil {
return errors.Wrap(err, "SubmitTransactionToServer() failed to execute request")
}
if resp.StatusCode != 200 {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return errors.Errorf("_generateUnsignedUpdateProfile(): Received non 200 response code: "+
"Status Code: %v Body: %v", resp.StatusCode, string(bodyBytes))
}
return nil
}