-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
88 lines (75 loc) · 1.88 KB
/
client.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
package gotron
import (
"context"
"github.com/go-resty/resty/v2"
)
type Client struct {
c *resty.Client
}
func New(httpApi string, apiKey string) *Client {
c := resty.New()
c.SetBaseURL(httpApi)
if apiKey != "" {
c.SetHeader("TRON-PRO-API-KEY", apiKey)
}
return &Client{
c: c,
}
}
func (c *Client) Close() {
}
func (c *Client) Ping(ctx context.Context) error {
_, err := c.c.R().SetContext(ctx).Get("")
return err
}
func (c *Client) CreateTransaction(ctx context.Context, params *CreateTransactionParams) (*Transaction, error) {
resp, err := c.c.R().
SetContext(ctx).
SetBody(params).
SetResult(&Transaction{}).
Post("/wallet/createtransaction")
if err != nil {
return nil, err
}
return resp.Result().(*Transaction), nil
}
func (c *Client) BroadcastTransaction(ctx context.Context, txn *Transaction) (*BroadcastTransactionResult, error) {
resp, err := c.c.R().
SetContext(ctx).
SetBody(txn).
SetResult(&BroadcastTransactionResult{}).
Post("/wallet/broadcasttransaction")
if err != nil {
return nil, err
}
return resp.Result().(*BroadcastTransactionResult), nil
}
// 获取区块 如果区块不存在返回空对象
func (c *Client) GetBlock(ctx context.Context, solid bool, params *GetBlockParams) (*Block, error) {
var path string
if solid {
path = "/walletsolidity/getblock"
} else {
path = "/wallet/getblock"
}
resp, err := c.c.R().
SetContext(ctx).
SetBody(params).
SetResult(&Block{}).
Post(path)
if err != nil {
return nil, err
}
return resp.Result().(*Block), err
}
func (c *Client) TriggerSmartContract(ctx context.Context, params *TriggerSmartContractParams) (*TriggerSmartContractResult, error) {
resp, err := c.c.R().
SetContext(ctx).
SetBody(params).
SetResult(&TriggerSmartContractResult{}).
Post("/wallet/triggersmartcontract")
if err != nil {
return nil, err
}
return resp.Result().(*TriggerSmartContractResult), nil
}