forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledger_entry_change.go
60 lines (54 loc) · 1.76 KB
/
ledger_entry_change.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
package xdr
import (
"encoding/base64"
"fmt"
)
// EntryType is a helper to get at the entry type for a change.
func (change *LedgerEntryChange) EntryType() (LedgerEntryType, error) {
key, err := change.LedgerKey()
return key.Type, err
}
// LedgerKey returns the key for the ledger entry that was changed
// in `change`. LedgerKey implements `Keyer`
func (change *LedgerEntryChange) LedgerKey() (LedgerKey, error) {
switch change.Type {
case LedgerEntryChangeTypeLedgerEntryCreated:
change := change.MustCreated()
return change.LedgerKey()
case LedgerEntryChangeTypeLedgerEntryRemoved:
return change.MustRemoved(), nil
case LedgerEntryChangeTypeLedgerEntryUpdated:
change := change.MustUpdated()
return change.LedgerKey()
case LedgerEntryChangeTypeLedgerEntryState:
change := change.MustState()
return change.LedgerKey()
default:
return LedgerKey{}, fmt.Errorf("unknown change type: %v", change.Type)
}
}
// MarshalBinaryBase64 marshals XDR into a binary form and then encodes it
// using base64.
func (change LedgerEntryChange) MarshalBinaryBase64() (string, error) {
b, err := change.MarshalBinary()
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}
// GetLedgerEntry returns the ledger entry that was changed in `change`, along
// with a boolean indicating whether the entry value was valid.
func (change *LedgerEntryChange) GetLedgerEntry() (LedgerEntry, bool) {
switch change.Type {
case LedgerEntryChangeTypeLedgerEntryCreated:
return change.GetCreated()
case LedgerEntryChangeTypeLedgerEntryState:
return change.GetState()
case LedgerEntryChangeTypeLedgerEntryUpdated:
return change.GetUpdated()
case LedgerEntryChangeTypeLedgerEntryRemoved:
return LedgerEntry{}, false
default:
return LedgerEntry{}, false
}
}