-
Notifications
You must be signed in to change notification settings - Fork 6
/
explorertypes.go
221 lines (198 loc) · 5.71 KB
/
explorertypes.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2017, The Dcrdata developers
// See LICENSE for details.
package explorer
import (
"github.com/HcashOrg/hcd/hcjson"
"github.com/HcashOrg/hcd/hcutil"
"github.com/HcashOrg/hcexplorer/db/dbtypes"
"github.com/HcashOrg/hcexplorer/txhelpers"
)
// BlockBasic models data for the explorer's explorer page
type BlockBasic struct {
Height int64 `json:"height"`
Size int32 `json:"size"`
Valid bool `json:"valid"`
Voters uint16 `json:"votes"`
Transactions int `json:"tx"`
FreshStake uint8 `json:"tickets"`
Revocations uint32 `json:"revocations"`
BlockTime int64 `json:"time"`
FormattedTime string `json:"formatted_time"`
FormattedBytes string
}
//RichList models data for the RichList's page
type RichList struct {
Address string `json:"address"`
Value float64 `json:"value"`
}
// TxBasic models data for transactions on the block page
type TxBasic struct {
TxID string
FormattedSize string
Total float64
Fee hcutil.Amount
FeeRate hcutil.Amount
VoteInfo *VoteInfo
Coinbase bool
}
//AddressTx models data for transactions on the address page
type AddressTx struct {
TxID string
FormattedSize string
Total float64
Confirmations uint64
Time int64
FormattedTime string
RecievedTotal float64
SentTotal float64
}
// TxInfo models data needed for display on the tx page
type TxInfo struct {
*TxBasic
SpendingTxns []TxInID
Type string
Vin []Vin
Vout []Vout
BlockHeight int64
BlockIndex uint32
Confirmations int64
Time int64
FormattedTime string
Mature string
VoteFundsLocked string
TicketMaturity int64
}
// TxInID models the identity of a spending transaction input
type TxInID struct {
Hash string
Index uint32
}
// VoteInfo models data about a SSGen transaction (vote)
type VoteInfo struct {
Validation BlockValidation `json:"block_validation"`
Version uint32 `json:"vote_version"`
Bits uint16 `json:"vote_bits"`
Choices []*txhelpers.VoteChoice `json:"vote_choices"`
}
// BlockValidation models data about a vote's decision on a block
type BlockValidation struct {
Hash string `json:"hash"`
Height int64 `json:"height"`
Validity bool `json:"validity"`
}
// Vin models basic data about a tx input for display
type Vin struct {
*hcjson.Vin
Addresses []string
FormattedAmount string
}
// Vout models basic data about a tx output for display
type Vout struct {
Addresses []string
Amount float64
FormattedAmount string
Type string
Spent bool
OP_RETURN string
}
// BlockInfo models data for display on the block page
type BlockInfo struct {
*BlockBasic
Hash string
Version int32
Confirmations int64
StakeRoot string
MerkleRoot string
Tx []*TxBasic
Tickets []*TxBasic
Revs []*TxBasic
Votes []*TxBasic
Nonce uint32
VoteBits uint16
FinalState string
PoolSize uint32
Bits string
SBits float64
Difficulty float64
ExtraData string
StakeVersion uint32
PreviousHash string
NextHash string
TotalSent float64
MiningFee hcutil.Amount
StakeValidationHeight int64
}
// AddressInfo models data for display on the address page
type AddressInfo struct {
Address string
Limit int64
Offset int64
Transactions []*AddressTx
NumFundingTxns int64
NumSpendingTxns int64
KnownFundingTxns int64
NumUnconfirmed int64
TotalReceived hcutil.Amount
TotalSent hcutil.Amount
Unspent hcutil.Amount
Balance *AddressBalance
Path string
}
// AddressBalance represents the number and value of spent and unspent outputs
// for an address.
type AddressBalance struct {
Address string
NumSpent int64
NumUnspent int64
TotalSpent int64
TotalUnspent int64
}
// ReduceAddressHistory generates a template AddressInfo from a slice of
// dbtypes.AddressRow. All fields except NumUnconfirmed and Transactions are set
// completely. Transactions is partially set, with each transaction having only
// the TxID and ReceivedTotal set. The rest of the data should be filled in by
// other means, such as RPC calls or database queries.
func ReduceAddressHistory(addrHist []*dbtypes.AddressRow) *AddressInfo {
if len(addrHist) == 0 {
return nil
}
var received, sent int64
var numFundingTxns, numSpendingTxns int64
var transactions []*AddressTx
for _, addrOut := range addrHist {
numFundingTxns++
coin := hcutil.Amount(addrOut.Value).ToCoin()
// Funding transaction
received += int64(addrOut.Value)
tx := AddressTx{
TxID: addrOut.FundingTxHash,
RecievedTotal: coin,
}
transactions = append(transactions, &tx)
// Is the outpoint spent?
if addrOut.SpendingTxHash == "" {
continue
}
// Spending transaction
numSpendingTxns++
sent += int64(addrOut.Value)
spendingTx := AddressTx{
TxID: addrOut.SpendingTxHash,
SentTotal: coin,
}
transactions = append(transactions, &spendingTx)
}
return &AddressInfo{
Address: addrHist[0].Address,
Transactions: transactions,
NumFundingTxns: numFundingTxns,
NumSpendingTxns: numSpendingTxns,
TotalReceived: hcutil.Amount(received),
TotalSent: hcutil.Amount(sent),
Unspent: hcutil.Amount(received - sent),
}
}
// WebsocketBlock wraps the new block info for use in the websocket
type WebsocketBlock struct {
Block BlockBasic `json:"block"`
}