forked from blocktree/tron-adapter
/
api.go
113 lines (97 loc) · 3.17 KB
/
api.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
/*
* Copyright 2018 The openwallet Authors
* This file is part of the openwallet library.
*
* The openwallet library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The openwallet library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package tron
import (
"errors"
"fmt"
"github.com/blocktree/openwallet/v2/log"
"github.com/blocktree/openwallet/v2/openwallet"
"github.com/imroc/req"
"github.com/shopspring/decimal"
"github.com/tidwall/gjson"
"net/http"
)
// A Client is a Tron RPC client. It performs RPCs over HTTP using JSON
// request and responses. A Client must be configured with a secret token
// to authenticate with other Cores on the network.
type Client struct {
BaseURL string
AccessToken string
Debug bool
client *req.Req
}
// NewClient create new client to connect
func NewClient(url, token string, debug bool) *Client {
c := Client{
BaseURL: url,
AccessToken: token,
Debug: debug,
}
api := req.New()
//trans, _ := api.Client().Transport.(*http.Transport)
//trans.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
c.client = api
return &c
}
// Call calls a remote procedure on another node, specified by the path.
func (c *Client) Call(path string, param interface{}) (*gjson.Result, error) {
if c == nil || c.client == nil {
return nil, errors.New("API url is not setup. ")
}
url := c.BaseURL + path
authHeader := req.Header{"Accept": "application/json", "TRON-PRO-API-KEY": c.AccessToken}
r, err := req.Post(url, req.BodyJSON(¶m), authHeader)
if err != nil {
log.Errorf("Failed: %+v >\n", err)
return nil, err
}
if c.Debug {
log.Std.Info("%+v", r)
}
if r.Response().StatusCode != http.StatusOK {
message := gjson.ParseBytes(r.Bytes()).String()
message = fmt.Sprintf("[%s]%s", r.Response().Status, message)
log.Error(message)
return nil, errors.New(message)
}
res := gjson.ParseBytes(r.Bytes())
return &res, nil
}
//getBalance 获取地址余额
func (wm *WalletManager) getBalance(address string) (*openwallet.Balance, error) {
account, _, err := wm.GetTRXAccount(address)
if err != nil {
return nil, err
}
balance := decimal.New(account.Balance, 0)
balance = balance.Shift(-wm.Decimal())
obj := openwallet.Balance{}
obj.Address = address
obj.ConfirmBalance = balance.String()
obj.UnconfirmBalance = "0"
obj.Balance = balance.String()
return &obj, nil
}
func newBalanceByExplorer(json *gjson.Result) *openwallet.Balance {
obj := openwallet.Balance{}
//解析json
obj.Address = gjson.Get(json.Raw, "addrStr").String()
obj.ConfirmBalance = gjson.Get(json.Raw, "balance").String()
obj.UnconfirmBalance = gjson.Get(json.Raw, "unconfirmedBalance").String()
u, _ := decimal.NewFromString(obj.ConfirmBalance)
b, _ := decimal.NewFromString(obj.UnconfirmBalance)
obj.Balance = u.Add(b).StringFixed(6)
return &obj
}