-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
127 lines (102 loc) · 3.48 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package sdk
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/irisnet/service-sdk-go/base_modules/bank"
"github.com/irisnet/service-sdk-go/base_modules/token"
baseclient "github.com/irisnet/service-sdk-go/client"
"github.com/irisnet/service-sdk-go/codec"
cdctypes "github.com/irisnet/service-sdk-go/codec/types"
cryptocodec "github.com/irisnet/service-sdk-go/crypto/codec"
"github.com/irisnet/service-sdk-go/service"
"github.com/irisnet/service-sdk-go/types"
txtypes "github.com/irisnet/service-sdk-go/types/tx"
)
// ServiceClient exports service.ServiceClient
type ServiceClient = service.ServiceClient
// NewServiceClient contructs a service client
func NewServiceClient(cfg types.ClientConfig) ServiceClient {
return NewClient(cfg).Service.(ServiceClient)
}
type Client struct {
logger log.Logger
moduleManager map[string]types.Module
encodingConfig types.EncodingConfig
types.BaseClient
Bank bank.Client
Token token.Client
Service service.Client
}
func NewClient(cfg types.ClientConfig) Client {
encodingConfig := makeEncodingConfig()
// create an instance of baseClient
baseClient := baseclient.NewBaseClient(cfg, encodingConfig, nil)
bankClient := bank.NewClient(baseClient, encodingConfig.Marshaler)
tokenClient := token.NewClient(baseClient, encodingConfig.Marshaler)
serviceClient := service.NewClient(baseClient, encodingConfig.Marshaler)
client := &Client{
logger: baseClient.Logger(),
BaseClient: baseClient,
Bank: bankClient,
Token: tokenClient,
Service: serviceClient,
moduleManager: make(map[string]types.Module),
encodingConfig: encodingConfig,
}
client.RegisterModule(
bankClient,
tokenClient,
serviceClient,
)
return *client
}
func (client *Client) SetLogger(logger log.Logger) {
client.BaseClient.SetLogger(logger)
}
func (client *Client) Codec() *codec.LegacyAmino {
return client.encodingConfig.Amino
}
func (client *Client) AppCodec() codec.Marshaler {
return client.encodingConfig.Marshaler
}
func (client *Client) Manager() types.BaseClient {
return client.BaseClient
}
func (client *Client) RegisterModule(ms ...types.Module) {
for _, m := range ms {
_, ok := client.moduleManager[m.Name()]
if ok {
panic(fmt.Sprintf("%s has register", m.Name()))
}
// m.RegisterCodec(client.encodingConfig.Amino)
m.RegisterInterfaceTypes(client.encodingConfig.InterfaceRegistry)
client.moduleManager[m.Name()] = m
}
}
func makeEncodingConfig() types.EncodingConfig {
amino := codec.NewLegacyAmino()
interfaceRegistry := cdctypes.NewInterfaceRegistry()
marshaler := codec.NewProtoCodec(interfaceRegistry)
txCfg := txtypes.NewTxConfig(marshaler, txtypes.DefaultSignModes)
encodingConfig := types.EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: txCfg,
Amino: amino,
}
RegisterLegacyAminoCodec(encodingConfig.Amino)
RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}
// RegisterLegacyAminoCodec registers the sdk message type.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterInterface((*types.Msg)(nil), nil)
cdc.RegisterInterface((*types.Tx)(nil), nil)
cryptocodec.RegisterCrypto(cdc)
}
// RegisterInterfaces registers the sdk message type.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterInterface("cosmos.v1beta1.Msg", (*types.Msg)(nil))
txtypes.RegisterInterfaces(registry)
cryptocodec.RegisterInterfaces(registry)
}