/
history.go
120 lines (105 loc) · 2.97 KB
/
history.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package wallet
import (
"encoding/hex"
"sort"
"time"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/types/amount"
"github.com/pactus-project/pactus/types/tx/payload"
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
)
type HistoryInfo struct {
TxID string
Time *time.Time
PayloadType string
Desc string
Amount amount.Amount
}
type transaction struct {
BlockHeight uint32 `json:"height"`
BlockTime uint32 `json:"time"`
PayloadType string `json:"type"`
Data string `json:"data"`
}
type activity struct {
TxID string `json:"id"`
Desc string `json:"desc"`
Amount amount.Amount `json:"amount"`
}
type pending struct {
TxID string `json:"id"`
Amount amount.Amount `json:"amount"`
Data string `json:"data"`
}
type history struct {
Transactions map[string]transaction `json:"transactions"`
Activities map[string][]activity `json:"activities"`
Pendings map[string][]pending `json:"pendings"`
}
func (h *history) hasTransaction(id string) bool {
_, ok := h.Transactions[id]
return ok
}
func (h *history) addActivity(addr string, amt amount.Amount, trx *pactus.GetTransactionResponse) {
if h.Activities == nil {
h.Activities = map[string][]activity{}
h.Transactions = map[string]transaction{}
}
if len(h.Activities[addr]) == 0 {
h.Activities[addr] = make([]activity, 0, 1)
}
act := activity{
TxID: hex.EncodeToString(trx.Transaction.Id),
Amount: amt,
}
h.Activities[addr] = append(h.Activities[addr], act)
sort.Slice(h.Activities[addr], func(i, j int) bool {
return h.Transactions[h.Activities[addr][i].TxID].BlockTime <
h.Transactions[h.Activities[addr][j].TxID].BlockTime
})
h.Transactions[act.TxID] = transaction{
BlockHeight: trx.BlockHeight,
BlockTime: trx.BlockTime,
PayloadType: payload.Type(trx.Transaction.PayloadType).String(),
Data: hex.EncodeToString(trx.Transaction.Data),
}
}
func (h *history) addPending(addr string, amt amount.Amount, txID hash.Hash, data []byte) {
if h.Pendings == nil {
h.Pendings = map[string][]pending{}
}
if len(h.Pendings[addr]) == 0 {
h.Pendings[addr] = make([]pending, 0, 1)
}
pnd := pending{
TxID: txID.String(),
Amount: amt,
Data: hex.EncodeToString(data),
}
h.Pendings[addr] = append(h.Pendings[addr], pnd)
}
func (h *history) getAddrHistory(addr string) []HistoryInfo {
addrActs := h.Activities[addr]
addrPnds := h.Pendings[addr]
history := make([]HistoryInfo, 0, len(addrActs)+len(addrPnds))
for _, pnd := range addrPnds {
history = append(history, HistoryInfo{
Amount: pnd.Amount,
TxID: pnd.TxID,
Desc: "Pending...",
Time: nil,
})
}
for _, act := range addrActs {
trx := h.Transactions[act.TxID]
tme := time.Unix(int64(trx.BlockTime), 0)
history = append(history, HistoryInfo{
Amount: act.Amount,
TxID: act.TxID,
Desc: act.Desc,
PayloadType: trx.PayloadType,
Time: &tme,
})
}
return history
}