-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_response.go
70 lines (61 loc) · 2.12 KB
/
tx_response.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
package hcnetcore
import (
"github.com/shantanu-hashcash/go/xdr"
)
const (
// TXStatusError represents the status value returned by hcnet-core when an error occurred from
// submitting a transaction
TXStatusError = "ERROR"
// TXStatusPending represents the status value returned by hcnet-core when a transaction has
// been accepted for processing
TXStatusPending = "PENDING"
// TXStatusDuplicate represents the status value returned by hcnet-core when a submitted
// transaction is a duplicate
TXStatusDuplicate = "DUPLICATE"
// TXStatusTryAgainLater represents the status value returned by hcnet-core when a submitted
// transaction was not included in the previous 4 ledgers and get banned for being added in the
// next few ledgers.
TXStatusTryAgainLater = "TRY_AGAIN_LATER"
)
// TXResponse represents the response returned from a submission request sent to hcnet-core's /tx
// endpoint
type TXResponse struct {
Exception string `json:"exception"`
Error string `json:"error"`
Status string `json:"status"`
// DiagnosticEvents is an optional base64-encoded XDR Variable-Length Array of DiagnosticEvents
DiagnosticEvents string `json:"diagnostic_events,omitempty"`
}
// IsException returns true if the response represents an exception response from hcnet-core
func (resp *TXResponse) IsException() bool {
return resp.Exception != ""
}
// DecodeDiagnosticEvents returns the decoded events
func DecodeDiagnosticEvents(events string) ([]xdr.DiagnosticEvent, error) {
var ret []xdr.DiagnosticEvent
if events == "" {
return ret, nil
}
err := xdr.SafeUnmarshalBase64(events, &ret)
if err != nil {
return nil, err
}
return ret, err
}
// DiagnosticEventsToSlice transforms the base64 diagnostic events into a slice of individual
// base64-encoded diagnostic events
func DiagnosticEventsToSlice(events string) ([]string, error) {
decoded, err := DecodeDiagnosticEvents(events)
if err != nil {
return nil, err
}
result := make([]string, len(decoded))
for i := 0; i < len(decoded); i++ {
encoded, err := xdr.MarshalBase64(decoded[i])
if err != nil {
return nil, err
}
result[i] = encoded
}
return result, nil
}