forked from decred/dcrdata
/
explorertypes.go
272 lines (244 loc) · 7.75 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) 2017, The Dcrdata developers
// See LICENSE for details.
package explorer
import (
"sync"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrdata/db/dbtypes"
"github.com/decred/dcrdata/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
}
// TxBasic models data for transactions on the block page
type TxBasic struct {
TxID string
FormattedSize string
Total float64
Fee dcrutil.Amount
FeeRate dcrutil.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 {
*dcrjson.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
TxAvailable bool
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 dcrutil.Amount
StakeValidationHeight int64
}
// AddressInfo models data for display on the address page
type AddressInfo struct {
Address string
Limit int64
MaxTxLimit int64
Offset int64
Transactions []*AddressTx
NumFundingTxns int64 // The number of transactions paying to the address
NumSpendingTxns int64 // The number of transactions spending from the address
NumTransactions int64 // The number of transactions in the address
KnownTransactions int64 // The number of transactions in the address unlimited
KnownFundingTxns int64 // The number of transactions paying to the address unlimited
NumUnconfirmed int64 // The number of unconfirmed transactions in the address
TotalReceived dcrutil.Amount
TotalSent dcrutil.Amount
Unspent dcrutil.Amount
Balance *AddressBalance
Path string
Fullmode bool
}
// 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
}
// HomeInfo represents data used for the home page
type HomeInfo struct {
CoinSupply int64 `json:"coin_supply"`
StakeDiff float64 `json:"sdiff"`
IdxBlockInWindow int `json:"window_idx"`
IdxInRewardWindow int `json:"reward_idx"`
Difficulty float64 `json:"difficulty"`
DevFund int64 `json:"dev_fund"`
DevAddress string `json:"dev_address"`
TicketROI float64 `json:"roi"`
ROIPeriod string `json:"roi_period"`
NBlockSubsidy BlockSubsidy `json:"subsidy"`
Params ChainParams `json:"params"`
PoolInfo TicketPoolInfo `json:"pool_info"`
}
// BlockSubsidy is an implementation of dcrjson.GetBlockSubsidyResult
type BlockSubsidy struct {
Total int64 `json:"total"`
PoW int64 `json:"pow"`
PoS int64 `json:"pos"`
Dev int64 `json:"dev"`
}
// MempoolInfo models data to update mempool info on the home page
type MempoolInfo struct {
sync.RWMutex
NumTickets uint32 `json:"num_tickets"`
NumVotes uint32 `json:"num_votes"`
}
// ChainParams models simple data about the chain server's parameters used for some
// info on the front page
type ChainParams struct {
WindowSize int64 `json:"window_size"`
RewardWindowSize int64 `json:"reward_window_size"`
TargetPoolSize int64 `json:"target_pool_size"`
BlockTime int64 `json:"target_block_time"`
}
// 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 := dcrutil.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: dcrutil.Amount(received),
TotalSent: dcrutil.Amount(sent),
Unspent: dcrutil.Amount(received - sent),
}
}
// WebsocketBlock wraps the new block info for use in the websocket
type WebsocketBlock struct {
Block *BlockBasic `json:"block"`
Extra *HomeInfo `json:"extra"`
}
type TicketPoolInfo struct {
Size uint32 `json:"size"`
Value float64 `json:"value"`
ValAvg float64 `json:"valavg"`
Percentage float64 `json:"percent"`
Target uint16 `json:"target"`
PercentTarget float64 `json:"percent_target"`
}