forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
320 lines (266 loc) · 8.98 KB
/
transactions.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package dcrlibwallet
import (
"encoding/json"
"sort"
"github.com/asdine/storm"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/planetdecred/dcrlibwallet/txhelper"
"github.com/planetdecred/dcrlibwallet/walletdata"
)
const (
// Export constants for use in mobile apps
// since gomobile excludes fields from sub packages.
TxFilterAll = walletdata.TxFilterAll
TxFilterSent = walletdata.TxFilterSent
TxFilterReceived = walletdata.TxFilterReceived
TxFilterTransferred = walletdata.TxFilterTransferred
TxFilterStaking = walletdata.TxFilterStaking
TxFilterCoinBase = walletdata.TxFilterCoinBase
TxFilterRegular = walletdata.TxFilterRegular
TxFilterMixed = walletdata.TxFilterMixed
TxFilterVoted = walletdata.TxFilterVoted
TxFilterRevoked = walletdata.TxFilterRevoked
TxFilterImmature = walletdata.TxFilterImmature
TxFilterLive = walletdata.TxFilterLive
TxFilterUnmined = walletdata.TxFilterUnmined
TxFilterExpired = walletdata.TxFilterExpired
TxFilterTickets = walletdata.TxFilterTickets
TxDirectionInvalid = txhelper.TxDirectionInvalid
TxDirectionSent = txhelper.TxDirectionSent
TxDirectionReceived = txhelper.TxDirectionReceived
TxDirectionTransferred = txhelper.TxDirectionTransferred
TxTypeRegular = txhelper.TxTypeRegular
TxTypeCoinBase = txhelper.TxTypeCoinBase
TxTypeTicketPurchase = txhelper.TxTypeTicketPurchase
TxTypeVote = txhelper.TxTypeVote
TxTypeRevocation = txhelper.TxTypeRevocation
TxTypeMixed = txhelper.TxTypeMixed
TicketStatusUnmined = "unmined"
TicketStatusImmature = "immature"
TicketStatusLive = "live"
TicketStatusVotedOrRevoked = "votedrevoked"
TicketStatusExpired = "expired"
)
func (wallet *Wallet) PublishUnminedTransactions() error {
n, err := wallet.Internal().NetworkBackend()
if err != nil {
log.Error(err)
return err
}
return wallet.Internal().PublishUnminedTransactions(wallet.shutdownContext(), n)
}
func (wallet *Wallet) GetTransaction(txHash string) (string, error) {
transaction, err := wallet.GetTransactionRaw(txHash)
if err != nil {
log.Error(err)
return "", err
}
result, err := json.Marshal(transaction)
if err != nil {
return "", err
}
return string(result), nil
}
func (wallet *Wallet) GetTransactionRaw(txHash string) (*Transaction, error) {
hash, err := chainhash.NewHashFromStr(txHash)
if err != nil {
log.Error(err)
return nil, err
}
txSummary, _, blockHash, err := wallet.Internal().TransactionSummary(wallet.shutdownContext(), hash)
if err != nil {
log.Error(err)
return nil, err
}
return wallet.decodeTransactionWithTxSummary(txSummary, blockHash)
}
func (wallet *Wallet) GetTransactions(offset, limit, txFilter int32, newestFirst bool) (string, error) {
transactions, err := wallet.GetTransactionsRaw(offset, limit, txFilter, newestFirst)
if err != nil {
return "", err
}
jsonEncodedTransactions, err := json.Marshal(&transactions)
if err != nil {
return "", err
}
return string(jsonEncodedTransactions), nil
}
func (wallet *Wallet) GetTransactionsRaw(offset, limit, txFilter int32, newestFirst bool) (transactions []Transaction, err error) {
err = wallet.walletDataDB.Read(offset, limit, txFilter, newestFirst, wallet.RequiredConfirmations(), wallet.GetBestBlock(), &transactions)
return
}
func (mw *MultiWallet) GetTransactions(offset, limit, txFilter int32, newestFirst bool) (string, error) {
transactions, err := mw.GetTransactionsRaw(offset, limit, txFilter, newestFirst)
if err != nil {
return "", err
}
jsonEncodedTransactions, err := json.Marshal(&transactions)
if err != nil {
return "", err
}
return string(jsonEncodedTransactions), nil
}
func (mw *MultiWallet) GetTransactionsRaw(offset, limit, txFilter int32, newestFirst bool) ([]Transaction, error) {
transactions := make([]Transaction, 0)
for _, wallet := range mw.wallets {
walletTransactions, err := wallet.GetTransactionsRaw(offset, limit, txFilter, newestFirst)
if err != nil {
return nil, err
}
transactions = append(transactions, walletTransactions...)
}
// sort transaction by timestamp in descending order
sort.Slice(transactions[:], func(i, j int) bool {
if newestFirst {
return transactions[i].Timestamp > transactions[j].Timestamp
}
return transactions[i].Timestamp < transactions[j].Timestamp
})
if len(transactions) > int(limit) && limit > 0 {
transactions = transactions[:limit]
}
return transactions, nil
}
func (wallet *Wallet) CountTransactions(txFilter int32) (int, error) {
return wallet.walletDataDB.Count(txFilter, wallet.RequiredConfirmations(), wallet.GetBestBlock(), &Transaction{})
}
func (wallet *Wallet) TicketHasVotedOrRevoked(ticketHash string) (bool, error) {
err := wallet.walletDataDB.FindOne("TicketSpentHash", ticketHash, &Transaction{})
if err != nil {
if err == storm.ErrNotFound {
return false, nil
}
return false, err
}
return true, nil
}
func (wallet *Wallet) TicketSpender(ticketHash string) (*Transaction, error) {
var spender Transaction
err := wallet.walletDataDB.FindOne("TicketSpentHash", ticketHash, &spender)
if err != nil {
if err == storm.ErrNotFound {
return nil, nil
}
return nil, err
}
return &spender, nil
}
func (wallet *Wallet) TransactionOverview() (txOverview *TransactionOverview, err error) {
txOverview = &TransactionOverview{}
txOverview.Sent, err = wallet.CountTransactions(TxFilterSent)
if err != nil {
return
}
txOverview.Received, err = wallet.CountTransactions(TxFilterReceived)
if err != nil {
return
}
txOverview.Transferred, err = wallet.CountTransactions(TxFilterTransferred)
if err != nil {
return
}
txOverview.Mixed, err = wallet.CountTransactions(TxFilterMixed)
if err != nil {
return
}
txOverview.Staking, err = wallet.CountTransactions(TxFilterStaking)
if err != nil {
return
}
txOverview.Coinbase, err = wallet.CountTransactions(TxFilterCoinBase)
if err != nil {
return
}
txOverview.All = txOverview.Sent + txOverview.Received + txOverview.Transferred + txOverview.Mixed +
txOverview.Staking + txOverview.Coinbase
return txOverview, nil
}
func (wallet *Wallet) TxMatchesFilter(tx *Transaction, txFilter int32) bool {
bestBlock := wallet.GetBestBlock()
// tickets with block height less than this are matured.
maturityBlock := bestBlock - int32(wallet.chainParams.TicketMaturity)
// tickets with block height less than this are expired.
expiryBlock := bestBlock - int32(wallet.chainParams.TicketMaturity+uint16(wallet.chainParams.TicketExpiry))
switch txFilter {
case TxFilterSent:
return tx.Type == TxTypeRegular && tx.Direction == TxDirectionSent
case TxFilterReceived:
return tx.Type == TxTypeRegular && tx.Direction == TxDirectionReceived
case TxFilterTransferred:
return tx.Type == TxTypeRegular && tx.Direction == TxDirectionTransferred
case TxFilterStaking:
switch tx.Type {
case TxTypeTicketPurchase:
fallthrough
case TxTypeVote:
fallthrough
case TxTypeRevocation:
return true
}
return false
case TxFilterCoinBase:
return tx.Type == TxTypeCoinBase
case TxFilterRegular:
return tx.Type == TxTypeRegular
case TxFilterMixed:
return tx.Type == TxTypeMixed
case TxFilterVoted:
return tx.Type == TxTypeVote
case TxFilterRevoked:
return tx.Type == TxTypeRevocation
case walletdata.TxFilterImmature:
return tx.Type == TxTypeTicketPurchase &&
(tx.BlockHeight > maturityBlock) // not matured
case TxFilterLive:
// ticket is live if we don't have the spender hash and it hasn't expired.
// we cannot detect missed tickets over spv.
return tx.Type == TxTypeTicketPurchase &&
tx.TicketSpender == "" &&
tx.BlockHeight > 0 &&
tx.BlockHeight <= maturityBlock &&
tx.BlockHeight > expiryBlock // not expired
case TxFilterUnmined:
return tx.Type == TxTypeTicketPurchase && tx.BlockHeight == -1
case TxFilterExpired:
return tx.Type == TxTypeTicketPurchase &&
tx.TicketSpender == "" &&
tx.BlockHeight > 0 &&
tx.BlockHeight <= expiryBlock
case TxFilterTickets:
return tx.Type == TxTypeTicketPurchase
case TxFilterAll:
return true
}
return false
}
func (wallet *Wallet) TxMatchesFilter2(direction, blockHeight int32, txType, ticketSpender string, txFilter int32) bool {
tx := Transaction{
Type: txType,
Direction: direction,
BlockHeight: blockHeight,
TicketSpender: ticketSpender,
}
return wallet.TxMatchesFilter(&tx, txFilter)
}
func (tx Transaction) Confirmations(bestBlock int32) int32 {
if tx.BlockHeight == BlockHeightInvalid {
return 0
}
return (bestBlock - tx.BlockHeight) + 1
}
func (tx Transaction) TicketStatus(ticketMaturity, ticketExpiry, bestBlock int32) string {
if tx.Type != TxTypeTicketPurchase {
return ""
}
confirmations := tx.Confirmations(bestBlock)
if confirmations == 0 {
return TicketStatusUnmined
} else if confirmations <= ticketMaturity {
return TicketStatusImmature
} else if confirmations > (ticketMaturity + ticketExpiry) {
return TicketStatusExpired
} else if tx.TicketSpender != "" {
return TicketStatusVotedOrRevoked
}
return TicketStatusLive
}