-
Notifications
You must be signed in to change notification settings - Fork 3
/
transaction.go
95 lines (77 loc) · 1.88 KB
/
transaction.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
package privatebtc
import (
"context"
"fmt"
"sync"
"golang.org/x/sync/errgroup"
)
// Transaction represents a BTC transaction.
type Transaction struct {
TxID string
Hash string
BlockHash string
Vout []TransactionVout
Vin []TransactionVin
}
// GetTransactionFee returns the transaction fee.
func (tx Transaction) GetTransactionFee(totalInputs float64) float64 {
var totalOutputs float64
for _, vout := range tx.Vout {
totalOutputs += vout.Value
}
return totalInputs - totalOutputs
}
// TotalInputsValue sums up the value of all inputs in the transaction by checking the value of
// the outputs that they spend from.
func (tx Transaction) TotalInputsValue(ctx context.Context, client RPCClient) (float64, error) {
var totalInputs float64
// sync
var (
inputsMutex sync.Mutex
eg, egCtx = errgroup.WithContext(ctx)
)
for _, vin := range tx.Vin {
vin := vin
eg.Go(func() error {
vinTx, err := client.GetTransaction(egCtx, vin.TxID)
if err != nil {
return fmt.Errorf("get transaction %s: %w", vin.TxID, err)
}
for _, vout := range vinTx.Vout {
if vout.N == vin.Vout {
inputsMutex.Lock()
totalInputs += vout.Value
inputsMutex.Unlock()
}
}
return nil
})
}
if err := eg.Wait(); err != nil {
return 0, err
}
return totalInputs, nil
}
// TransactionVin represents a BTC transaction input.
type TransactionVin struct {
TxID string
Vout uint32
}
// TransactionVout represents a BTC transaction output.
type TransactionVout struct {
Value float64
N uint32
ScriptPubKey struct {
Address string
}
}
// MempoolTransaction represents a BTC mempool transaction.
type MempoolTransaction struct {
Hash string
Outputs []MempoolTransactionOutput
}
// MempoolTransactionOutput represents a BTC mempool transaction output.
type MempoolTransactionOutput struct {
Address string
Value float64
}