-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.go
More file actions
107 lines (91 loc) · 2.18 KB
/
api.go
File metadata and controls
107 lines (91 loc) · 2.18 KB
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
package main
import (
"context"
"encoding/hex"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
)
type UTXO struct {
Address string `json:"address"`
TxId string `json:"txId"`
OutputIndex uint32 `json:"outputIndex"`
Height uint32 `json:"height"`
Script string `json:"script"`
Satoshis int64 `json:"satoshis"`
Type string `json:"type"`
Time int64 `json:"time"`
}
func (minter *Minter) getUTXOs(ctx context.Context, scriptHashes g.ArrayStr) []UTXO {
var err error
request, err := minter.client.Post(ctx, "/utxos", gjson.MustEncode(g.MapStrAny{
"scriptHashs": scriptHashes,
}))
if err != nil {
panic(err)
}
if request.StatusCode != 200 {
panic(request.StatusCode)
}
var response []UTXO
err = gjson.DecodeTo(request.ReadAll(), &response)
if err != nil {
panic(err)
}
return response
}
type Fee struct {
Slow int64 `json:"slowFee"`
Average int64 `json:"avgFee"`
Fast int64 `json:"fastFee"`
}
func (minter *Minter) getFee(ctx context.Context) Fee {
var err error
request, err := minter.client.Get(ctx, "/fees")
if err != nil {
panic(err)
}
if request.StatusCode != 200 {
panic(request.StatusCode)
}
var response Fee
err = gjson.DecodeTo(request.ReadAll(), &response)
if err != nil {
panic(err)
}
return response
}
type BroadcastResult struct {
Success bool `json:"success"`
Error struct {
Code int `json:"code"`
Message struct {
Total string `json:"total"`
Height uint32 `json:"height"`
Note struct {
Protocol string `json:"p"`
Operator string `json:"op"`
Tick string `json:"tick"`
Amount string `json:"amt"`
} `json:"note"`
Result string `json:"result"`
} `json:"message"`
} `json:"errror"`
}
func (minter *Minter) broadcast(ctx context.Context, raw []byte) BroadcastResult {
var err error
request, err := minter.client.Post(ctx, "/broadcast", gjson.MustEncode(g.MapStrAny{
"rawHex": hex.EncodeToString(raw),
}))
if err != nil {
panic(err)
}
if request.StatusCode != 200 {
panic(request.StatusCode)
}
var response BroadcastResult
err = gjson.DecodeTo(request.ReadAll(), &response)
if err != nil {
panic(err)
}
return response
}