-
Notifications
You must be signed in to change notification settings - Fork 44
/
gnfd_client_option.go
53 lines (44 loc) · 1.64 KB
/
gnfd_client_option.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
package client
import (
"google.golang.org/grpc"
"github.com/bnb-chain/greenfield/sdk/keys"
)
// GreenfieldClientOption configures how we set up the greenfield client.
type GreenfieldClientOption interface {
Apply(*GreenfieldClient)
}
// GreenfieldClientOptionFunc defines an applied function for setting the greenfield client.
type GreenfieldClientOptionFunc func(*GreenfieldClient)
// Apply set up the option field to the client instance.
func (f GreenfieldClientOptionFunc) Apply(client *GreenfieldClient) {
f(client)
}
// WithKeyManager returns a GreenfieldClientOption which configures a client key manager option.
func WithKeyManager(km keys.KeyManager) GreenfieldClientOption {
return GreenfieldClientOptionFunc(func(client *GreenfieldClient) {
client.keyManager = km
})
}
// WithGrpcConnectionAndDialOption returns a GreenfieldClientOption which configures a grpc client connection with grpc dail options.
func WithGrpcConnectionAndDialOption(grpcAddr string, opts ...grpc.DialOption) GreenfieldClientOption {
return GreenfieldClientOptionFunc(func(client *GreenfieldClient) {
client.grpcConn = grpcConn(grpcAddr, opts...)
})
}
// WithWebSocketClient returns a GreenfieldClientOption which specify that connection is a websocket connection
func WithWebSocketClient() GreenfieldClientOption {
return GreenfieldClientOptionFunc(func(client *GreenfieldClient) {
client.useWebSocket = true
})
}
// grpcConn is used to establish a connection with a given address and dial options.
func grpcConn(addr string, opts ...grpc.DialOption) *grpc.ClientConn {
conn, err := grpc.Dial(
addr,
opts...,
)
if err != nil {
panic(err)
}
return conn
}