forked from checksum0/go-electrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripthash.go
89 lines (70 loc) · 2.67 KB
/
scripthash.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
package electrum
import "context"
// GetBalanceResp represents the response to GetBalance().
type GetBalanceResp struct {
Result GetBalanceResult `json:"result"`
}
// GetBalanceResult represents the content of the result field in the response to GetBalance().
type GetBalanceResult struct {
Confirmed float64 `json:"confirmed"`
Unconfirmed float64 `json:"unconfirmed"`
}
// GetBalance returns the confirmed and unconfirmed balance for a scripthash.
// https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-scripthash-get-balance
func (s *Client) GetBalance(ctx context.Context, scripthash string) (GetBalanceResult, error) {
var resp GetBalanceResp
err := s.request(ctx, "blockchain.scripthash.get_balance", []interface{}{scripthash}, &resp)
if err != nil {
return GetBalanceResult{}, err
}
return resp.Result, err
}
// GetMempoolResp represents the response to GetHistory() and GetMempool().
type GetMempoolResp struct {
Result []*GetMempoolResult `json:"result"`
}
// GetMempoolResult represents the content of the result field in the response
// to GetHistory() and GetMempool().
type GetMempoolResult struct {
Hash string `json:"tx_hash"`
Height int32 `json:"height"`
Fee uint32 `json:"fee,omitempty"`
}
// GetHistory returns the confirmed and unconfirmed history for a scripthash.
func (s *Client) GetHistory(ctx context.Context, scripthash string) ([]*GetMempoolResult, error) {
var resp GetMempoolResp
err := s.request(ctx, "blockchain.scripthash.get_history", []interface{}{scripthash}, &resp)
if err != nil {
return nil, err
}
return resp.Result, err
}
// GetMempool returns the unconfirmed transacations of a scripthash.
func (s *Client) GetMempool(ctx context.Context, scripthash string) ([]*GetMempoolResult, error) {
var resp GetMempoolResp
err := s.request(ctx, "blockchain.scripthash.get_mempool", []interface{}{scripthash}, &resp)
if err != nil {
return nil, err
}
return resp.Result, err
}
// ListUnspentResp represents the response to ListUnspent()
type ListUnspentResp struct {
Result []*ListUnspentResult `json:"result"`
}
// ListUnspentResult represents the content of the result field in the response to ListUnspent()
type ListUnspentResult struct {
Height uint32 `json:"height"`
Position uint32 `json:"tx_pos"`
Hash string `json:"tx_hash"`
Value int64 `json:"value"`
}
// ListUnspent returns an ordered list of UTXOs for a scripthash.
func (s *Client) ListUnspent(ctx context.Context, scripthash string) ([]*ListUnspentResult, error) {
var resp ListUnspentResp
err := s.request(ctx, "blockchain.scripthash.listunspent", []interface{}{scripthash}, &resp)
if err != nil {
return nil, err
}
return resp.Result, err
}