-
Notifications
You must be signed in to change notification settings - Fork 0
/
calls.go
276 lines (226 loc) · 6.8 KB
/
calls.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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
)
type BlockHeaderResp struct {
ID int `json:"id"`
Jsonrpc string `json:"jsonrpc"`
// The raw block header as a hexadecimal string.
Result string `json:"result"`
}
func (resp *BlockHeaderResp) String() string {
tmpl := `
ID: %v
Jsonrpc: %v
Result: %v
`
return fmt.Sprintf(tmpl, resp.ID, resp.Jsonrpc, resp.Result)
}
// Return the block header at the given height.
// * The height of the block, a non-negative integer.
func (client *ElectrumxClient) GetBlockHeader(height int) (*BlockHeaderResp, error) {
if err := client.call1(0, "blockchain.block.header", height); err != nil {
return nil, err
}
resp, err := client.recv()
if err != nil {
return nil, err
}
rez := BlockHeaderResp{}
if err := json.Unmarshal(resp, &rez); err != nil {
return nil, err
}
return &rez, nil
}
type BlockHeadersResp struct {
ID int `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result BlockHeadersResult `json:"result"`
}
func (resp *BlockHeadersResp) String() string {
tmpl := `
ID: %v
Jsonrpc: %v
Count: %v
Hex: %v
Max: %v
`
return fmt.Sprintf(tmpl, resp.ID, resp.Jsonrpc, resp.Result.Count, resp.Result.Hex, resp.Result.Max)
}
type BlockHeadersResult struct {
// The number of headers returned, between zero and the number requested. If
// the chain has not extended sufficiently far, only the available headers will be
// returned. If more headers than max were requested at most max will be returned.
Count int `json:"count"`
// The binary block headers concatenated together in-order as a hexadecimal string.
Hex string `json:"hex"`
// The maximum number of headers the server will return in a single request.
Max int `json:"max"`
}
// Return a concatenated chunk of block headers from the main chain.
// * start_height - the height of the first header requested, a non-negative integer.
// * count - the number of headers requested, a non-negative integer.
func (client *ElectrumxClient) GetBlockHeaders(startHeight int, count int) (*BlockHeadersResp, error) {
if err := client.call2(0, "blockchain.block.headers", startHeight, count); err != nil {
return nil, err
}
resp, err := client.recv()
if err != nil {
return nil, err
}
rez := BlockHeadersResp{}
if err := json.Unmarshal(resp, &rez); err != nil {
return nil, err
}
return &rez, nil
}
type EstimateFeeResp struct {
ID int `json:"id"`
Jsonrpc string `json:"jsonrpc"`
// The estimated transaction fee in coin units per kilobyte, as a floating point number.
// If the daemon does not have enough information to make an estimate, the integer
// -1 is returned.
Result float64 `json:"result"`
}
func (resp *EstimateFeeResp) String() string {
tmpl := `
ID: %v
Jsonrpc: %v
Result: %v
`
return fmt.Sprintf(tmpl, resp.ID, resp.Jsonrpc, resp.Result)
}
// Return the estimated transaction fee per kilobyte for a transaction to be confirmed
// within a certain number of blocks.
// * number - the number of blocks to target for confirmation.
func (client *ElectrumxClient) EstimateFee(number int) (*EstimateFeeResp, error) {
if err := client.call1(0, "blockchain.estimatefee", number); err != nil {
return nil, err
}
resp, err := client.recv()
if err != nil {
return nil, err
}
rez := EstimateFeeResp{}
if err := json.Unmarshal(resp, &rez); err != nil {
return nil, err
}
return &rez, nil
}
type RelayFeeResp struct {
ID int `json:"id"`
Jsonrpc string `json:"jsonrpc"`
// The fee in whole coin units (BTC, not satoshis for Bitcoin) as a floating point number.
Result float64 `json:"result"`
}
func (resp *RelayFeeResp) String() string {
tmpl := `
ID: %v
Jsonrpc: %v
Result: %v
`
return fmt.Sprintf(tmpl, resp.ID, resp.Jsonrpc, resp.Result)
}
// Return the minimum fee a low-priority transaction must pay in order to be accepted
// to the daemon’s memory pool.
func (client *ElectrumxClient) RelayFee() (*RelayFeeResp, error) {
if err := client.call0(0, "blockchain.relayfee"); err != nil {
return nil, err
}
resp, err := client.recv()
if err != nil {
return nil, err
}
rez := RelayFeeResp{}
if err := json.Unmarshal(resp, &rez); err != nil {
return nil, err
}
return &rez, nil
}
// A dictionary with keys confirmed and unconfirmed. The value of each is the
// appropriate balance in coin units as a string.
type ScriptHashGetBalanceResult struct {
Confirmed int `json:"confirmed"`
Unconfirmed int `json:"unconfirmed"`
}
type ScriptHashGetBalanceResp struct {
ID int `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result ScriptHashGetBalanceResult `json:"result"`
}
func (resp *ScriptHashGetBalanceResp) String() string {
tmpl := `
ID: %v
Jsonrpc: %v
Confirmed: %v
Unconfirmed: %v
`
return fmt.Sprintf(tmpl, resp.ID, resp.Jsonrpc, resp.Result.Confirmed, resp.Result.Unconfirmed)
}
// Return the confirmed and unconfirmed balances of a script hash.
// * scriptHash - the script hash as a hexadecimal string.
func (client *ElectrumxClient) ScriptHashGetBalance(scriptHash []byte) (*ScriptHashGetBalanceResp, error) {
if err := client.call1(0, "blockchain.scripthash.get_balance", wrap(hex.EncodeToString(scriptHash))); err != nil {
return nil, err
}
resp, err := client.recv()
if err != nil {
return nil, err
}
rez := ScriptHashGetBalanceResp{}
if err := json.Unmarshal(resp, &rez); err != nil {
return nil, err
}
return &rez, nil
}
func wrap(val string) string {
return fmt.Sprintf(`"%v"`, val)
}
type ScriptHashGetHistoryResult struct {
Height int `json:"height"`
TxHash string `json:"tx_hash"`
}
type ScriptHashGetHistoryResp struct {
ID int `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result []*ScriptHashGetHistoryResult `json:"result"`
}
func (resp *ScriptHashGetHistoryResp) String() string {
result := "\n"
for _, item := range resp.Result {
result += fmt.Sprintln(item)
}
tmpl := `
ID: %v
Jsonrpc: %v
Result: %v
`
return fmt.Sprintf(tmpl, resp.ID, resp.Jsonrpc, result)
}
func (client *ElectrumxClient) ScriptHashGetHistory(scriptHash []byte) (*ScriptHashGetHistoryResp, error) {
if err := client.call1(0, "blockchain.scripthash.get_history", wrap(hex.EncodeToString(scriptHash))); err != nil {
return nil, err
}
resp, err := client.recv()
if err != nil {
return nil, err
}
rez := ScriptHashGetHistoryResp{}
if err := json.Unmarshal(resp, &rez); err != nil {
return nil, err
}
return &rez, nil
}
// NOT SUPPORT ??
func (client *ElectrumxClient) ScriptHashGetMempool(scriptHash []byte) (string, error) {
if err := client.call1(0, "blockchain.scripthash.get_mempool", wrap(hex.EncodeToString(scriptHash))); err != nil {
return "", err
}
resp, err := client.recv()
if err != nil {
return "", err
}
return string(resp), nil
}