This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tx.go
173 lines (141 loc) · 3.82 KB
/
tx.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
package btc
import (
"encoding/hex"
"fmt"
"math/big"
"net/http"
"time"
"github.com/goccy/go-json"
"github.com/magicpool-co/pool/pkg/blockchair"
"github.com/magicpool-co/pool/pkg/crypto/tx/btctx"
"github.com/magicpool-co/pool/types"
)
const (
txVersion = 0x1
)
type feeResponse interface {
getRate() uint64
}
type blockchainInfoFeeResponse struct {
Limits struct {
Min int `json:"min"`
Max int `json:"max"`
} `json:"limits"`
Regular int `json:"regular"`
Priority uint64 `json:"priority"`
}
func (b *blockchainInfoFeeResponse) getRate() uint64 {
return b.Priority
}
type bitgoFeeResponse struct {
FeePerKB uint64 `json:"feePerKB"`
NumBlocks int `json:"numBlocks"`
Confidence int `json:"confidence"`
Multiplier int `json:"multiplier"`
}
func (b *bitgoFeeResponse) getRate() uint64 {
if b.Confidence > 60 {
return b.FeePerKB / 1000
}
return 0
}
type bitcoinerLiveFeeResponse struct {
Timestamp int `json:"timestamp"`
Estimates map[int]struct {
SatsPerVByte uint64 `json:"sats_per_vbyte"`
} `json:"estimates"`
}
func (b *bitcoinerLiveFeeResponse) getRate() uint64 {
if val, ok := b.Estimates[30]; ok {
return val.SatsPerVByte / 1000
}
return 0
}
func getFeeRate() (uint64, error) {
feeSources := map[string]feeResponse{
"https://api.blockchain.info/mempool/fees": new(blockchainInfoFeeResponse),
"https://bitcoiner.live/api/fees/estimates/latest": new(bitgoFeeResponse),
"https://www.bitgo.com/api/v2/btc/tx/fee": new(bitcoinerLiveFeeResponse),
}
for url, data := range feeSources {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
continue
}
client := http.Client{
Timeout: time.Duration(3 * time.Second),
}
res, err := client.Do(req)
if err != nil {
continue
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(data)
if err != nil || data.getRate() == 0 {
continue
}
return data.getRate(), nil
}
return 0, fmt.Errorf("unable to find BTC fee rate")
}
func (node Node) GetTxExplorerURL(txid string) string {
return "https://blockchair.com/bitcoin/transaction/" + txid
}
func (node Node) GetAddressExplorerURL(address string) string {
return "https://blockchair.com/bitcoin/address/" + address
}
func (node Node) GetTx(txid string) (*types.TxResponse, error) {
tx, err := blockchair.New(node.blockchairKey).GetTxBTC(txid)
if err != nil {
return nil, err
}
outputs := make([]*types.UTXOResponse, len(tx.Outputs))
for i, output := range tx.Outputs {
outputs[i] = &types.UTXOResponse{
Hash: output.TransactionHash,
Index: output.Index,
Value: output.Value,
Address: output.Recipient,
}
}
var height uint64
var confirmed bool
if tx.Tx.BlockID != -1 {
confirmed = true
height = uint64(tx.Tx.BlockID)
}
res := &types.TxResponse{
Hash: tx.Tx.Hash,
BlockNumber: height,
Value: new(big.Int).SetUint64(tx.Tx.OutputTotal),
Fee: new(big.Int).SetUint64(tx.Tx.Fee),
FeeBalance: new(big.Int),
Confirmed: confirmed,
Outputs: outputs,
}
return res, nil
}
func (node Node) GetBalance() (*big.Int, error) {
address, err := blockchair.New(node.blockchairKey).GetAddressBTC(node.address)
if err != nil {
return nil, err
}
return new(big.Int).SetUint64(address.Balance), nil
}
func (node Node) CreateTx(inputs []*types.TxInput, outputs []*types.TxOutput) (string, string, error) {
feeRate, err := getFeeRate()
if err != nil {
return "", "", err
}
baseTx := btctx.NewTransaction(txVersion, 0, node.prefixP2PKH, node.prefixP2SH, true)
rawTx, err := btctx.GenerateTx(node.privKey, baseTx, inputs, outputs, feeRate)
if err != nil {
return "", "", err
}
tx := hex.EncodeToString(rawTx)
txid := btctx.CalculateTxID(tx)
return txid, tx, nil
}
func (node Node) BroadcastTx(tx string) (string, error) {
return blockchair.New(node.blockchairKey).BroadcastTxBTC(tx)
}