forked from asuleymanov/steem-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
98 lines (78 loc) · 2.33 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
89
90
91
92
93
94
95
96
97
98
package rpc
import (
"errors"
// RPC
"github.com/smallnest/steem-api/apis/database"
"github.com/smallnest/steem-api/apis/follow"
"github.com/smallnest/steem-api/apis/market"
"github.com/smallnest/steem-api/apis/networkbroadcast"
"github.com/smallnest/steem-api/transactions"
"github.com/smallnest/steem-api/transports"
"github.com/smallnest/steem-api/transports/websocket"
)
// Client can be used to access Steem remote APIs.
//
// There is a public field for every Steem API available,
// e.g. Client.Database corresponds to database_api.
type Client struct {
cc transports.CallCloser
// Database represents database_api.
Database *database.API
// Follow represents follow_api.
Follow *follow.API
// Follow represents market_history_api.
Market *market.API
// NetworkBroadcast represents network_broadcast_api.
NetworkBroadcast *networkbroadcast.API
//Chain Id
Chain *transactions.Chain
// Current keys for operations, wif format
CurrentKeys *Keys
}
// NewClient creates a new RPC client that use the given CallCloser internally.
func NewClient(url []string, chain string) (*Client, error) {
call, err := initclient(url)
if err != nil {
return nil, err
}
client := &Client{cc: call}
client.Database = database.NewAPI(client.cc)
client.Follow = follow.NewAPI(client.cc)
client.Market = market.NewAPI(client.cc)
client.NetworkBroadcast = networkbroadcast.NewAPI(client.cc)
client.Chain, err = initChainID(chain)
if err != nil {
client.Chain = transactions.SteemChain
}
return client, nil
}
// Close should be used to close the client when no longer needed.
// It simply calls Close() on the underlying CallCloser.
func (client *Client) Close() error {
return client.cc.Close()
}
//SetKeys you can specify keys for signing transactions.
func (client *Client) SetKeys(keys *Keys) {
client.CurrentKeys = keys
}
func initclient(url []string) (*websocket.Transport, error) {
// Инициализация Websocket
t, err := websocket.NewTransport(url)
if err != nil {
return nil, err
}
return t, nil
}
func initChainID(str string) (*transactions.Chain, error) {
var ChainID transactions.Chain
// Определяем ChainId
switch str {
case "steem":
ChainID = *transactions.SteemChain
case "test":
ChainID = *transactions.TestChain
default:
return nil, errors.New("Chain not found")
}
return &ChainID, nil
}