forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txparser.go
111 lines (93 loc) · 2.84 KB
/
txparser.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
package dcrlibwallet
import (
"fmt"
w "decred.org/dcrwallet/v2/wallet"
"github.com/decred/dcrd/chaincfg/chainhash"
)
const BlockHeightInvalid int32 = -1
func (wallet *Wallet) decodeTransactionWithTxSummary(txSummary *w.TransactionSummary,
blockHash *chainhash.Hash) (*Transaction, error) {
var blockHeight int32 = BlockHeightInvalid
if blockHash != nil {
blockIdentifier := w.NewBlockIdentifierFromHash(blockHash)
blockInfo, err := wallet.Internal().BlockInfo(wallet.shutdownContext(), blockIdentifier)
if err != nil {
log.Error(err)
} else {
blockHeight = blockInfo.Height
}
}
walletInputs := make([]*WalletInput, len(txSummary.MyInputs))
for i, input := range txSummary.MyInputs {
accountNumber := int32(input.PreviousAccount)
accountName, err := wallet.AccountName(accountNumber)
if err != nil {
log.Error(err)
}
walletInputs[i] = &WalletInput{
Index: int32(input.Index),
AmountIn: int64(input.PreviousAmount),
WalletAccount: &WalletAccount{
AccountNumber: accountNumber,
AccountName: accountName,
},
}
}
walletOutputs := make([]*WalletOutput, len(txSummary.MyOutputs))
for i, output := range txSummary.MyOutputs {
accountNumber := int32(output.Account)
accountName, err := wallet.AccountName(accountNumber)
if err != nil {
log.Error(err)
}
walletOutputs[i] = &WalletOutput{
Index: int32(output.Index),
AmountOut: int64(output.Amount),
Internal: output.Internal,
Address: output.Address.String(),
WalletAccount: &WalletAccount{
AccountNumber: accountNumber,
AccountName: accountName,
},
}
}
walletTx := &TxInfoFromWallet{
WalletID: wallet.ID,
BlockHeight: blockHeight,
Timestamp: txSummary.Timestamp,
Hex: fmt.Sprintf("%x", txSummary.Transaction),
Inputs: walletInputs,
Outputs: walletOutputs,
}
decodedTx, err := wallet.DecodeTransaction(walletTx, wallet.chainParams)
if err != nil {
return nil, err
}
if decodedTx.TicketSpentHash != "" {
ticketPurchaseTx, err := wallet.GetTransactionRaw(decodedTx.TicketSpentHash)
if err != nil {
return nil, err
}
timeDifferenceInSeconds := decodedTx.Timestamp - ticketPurchaseTx.Timestamp
decodedTx.DaysToVoteOrRevoke = int32(timeDifferenceInSeconds / 86400) // seconds to days conversion
// calculate reward
var ticketInvestment int64
for _, input := range ticketPurchaseTx.Inputs {
if input.AccountNumber > -1 {
ticketInvestment += input.Amount
}
}
var ticketOutput int64
for _, output := range walletTx.Outputs {
if output.AccountNumber > -1 {
ticketOutput += output.AmountOut
}
}
reward := ticketOutput - ticketInvestment
decodedTx.VoteReward = reward
// update ticket with spender hash
ticketPurchaseTx.TicketSpender = decodedTx.Hash
wallet.walletDataDB.SaveOrUpdate(&Transaction{}, ticketPurchaseTx)
}
return decodedTx, nil
}