-
Notifications
You must be signed in to change notification settings - Fork 21
/
owner.go
91 lines (79 loc) · 2.49 KB
/
owner.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
)
// https://docs.rs/grin_wallet_api/3.0.0/grin_wallet_api
type OwnerAPI struct {
db *database
conf *config
}
func NewOwnerAPI(db *database, conf *config) *OwnerAPI {
return &OwnerAPI{
db: db,
conf: conf,
}
}
// deprecated v1 wallet owner api
func (o *OwnerAPI) getNewBalanceV1() uint64 {
req, _ := http.NewRequest("GET", "http://"+o.conf.Wallet.Address+":"+strconv.Itoa(o.conf.Wallet.OwnerAPIPort)+"/v1/wallet/owner/retrieve_summary_info?refresh", nil)
req.SetBasicAuth(o.conf.Node.AuthUser, o.conf.Node.AuthPass)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Error("failed to get balance from wallet, treat this as no income")
return 0
}
dec := json.NewDecoder(res.Body)
var summaryInfo []interface{}
_ = dec.Decode(&summaryInfo)
i, _ := summaryInfo[1].(map[string]interface{})
strSpendable := i["amount_currently_spendable"].(string)
spendable, _ := strconv.Atoi(strSpendable)
return uint64(spendable) // unit nanogrin
}
// The V2 Owner API (OwnerRpc) will be removed in grin-wallet 4.0.0. Please migrate to the V3 (OwnerRpcS) API as soon as possible.
func (o *OwnerAPI) getNewBalanceV2() uint64 {
req, _ := http.NewRequest("POST", "http://"+o.conf.Wallet.Address+":"+strconv.Itoa(o.conf.Wallet.OwnerAPIPort)+"/v2/wallet/owner", strings.NewReader(`{
"jsonrpc": "2.0",
"method": "retrieve_summary_info",
"params": [true, 10],
"id": 1
}`))
req.SetBasicAuth(o.conf.Node.AuthUser, o.conf.Node.AuthPass)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Error("failed to get balance from wallet for failing to post request, treat this as no income")
return 0
}
dec := json.NewDecoder(res.Body)
var summaryInfo jsonRPCResponse
_ = dec.Decode(&summaryInfo)
result := summaryInfo.Result
if result == nil {
log.Error(summaryInfo.Error)
log.Error("failed to get balance from wallet for failing to get result request, treat this as no income")
return 0
}
mapResult, ok := result.(map[string]interface{})
if ok || mapResult["Ok"] != nil {
theOk, ok := mapResult["Ok"].([]interface{})
if ok {
for i := range theOk {
if balanceMap, ok := theOk[i].(map[string]interface{}); ok {
strSpendable := balanceMap["amount_currently_spendable"].(string)
spendable, _ := strconv.Atoi(strSpendable)
return uint64(spendable) // unit nanogrin
}
}
}
}
return 0
}
func (o *OwnerAPI) getNewBalanceV3() uint64 {
log.Fatal("V3 support WIP!")
return 0
}