-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbalance.go
49 lines (39 loc) · 997 Bytes
/
balance.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
package main
import (
"io/ioutil"
"net/http"
"strconv"
"github.com/tidwall/gjson"
)
const (
bitcoinCashAPI = "https://bch-tchain.api.btc.com/v3"
defaultPageSize = 50
)
// get balance for the specified address, and the address should be
// base58 encoded format
func getBalance(addr string) (int64, error) {
url := bitcoinCashAPI + "/address/" + addr
res, err := http.Get(url)
if err != nil {
return 0, err
}
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return 0, err
}
return gjson.Get(string(content), "data.balance").Int(), nil
}
// get raw string of unspent list for the specified address
func getUnspent(addr string, page int) (string, error) {
url := bitcoinCashAPI + "/address/" + addr + "/unspent?pagesize=" +
strconv.Itoa(defaultPageSize) + "&page=" + strconv.Itoa(page)
res, err := http.Get(url)
if err != nil {
return "", err
}
content, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(content), nil
}