forked from bianjieai/irita-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
194 lines (172 loc) · 5.93 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package sdk
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/bianjieai/irita-sdk-go/codec"
cdctypes "github.com/bianjieai/irita-sdk-go/codec/types"
cryptocodec "github.com/bianjieai/irita-sdk-go/crypto/codec"
"github.com/bianjieai/irita-sdk-go/modules"
"github.com/bianjieai/irita-sdk-go/modules/bank"
"github.com/bianjieai/irita-sdk-go/modules/identity"
"github.com/bianjieai/irita-sdk-go/modules/keys"
"github.com/bianjieai/irita-sdk-go/modules/nft"
"github.com/bianjieai/irita-sdk-go/modules/node"
"github.com/bianjieai/irita-sdk-go/modules/oracle"
"github.com/bianjieai/irita-sdk-go/modules/params"
"github.com/bianjieai/irita-sdk-go/modules/perm"
"github.com/bianjieai/irita-sdk-go/modules/record"
"github.com/bianjieai/irita-sdk-go/modules/service"
"github.com/bianjieai/irita-sdk-go/modules/token"
"github.com/bianjieai/irita-sdk-go/modules/wasm"
"github.com/bianjieai/irita-sdk-go/types"
txtypes "github.com/bianjieai/irita-sdk-go/types/tx"
)
var registers = []codec.RegisterInterfaces{
perm.RegisterInterfaces,
bank.RegisterInterfaces,
identity.RegisterInterfaces,
token.RegisterInterfaces,
record.RegisterInterfaces,
nft.RegisterInterfaces,
service.RegisterInterfaces,
node.RegisterInterfaces,
params.RegisterInterfaces,
wasm.RegisterInterfaces,
}
// IRITAClient define a group of api to access c network
type IRITAClient struct {
logger log.Logger
moduleManager map[string]types.Module
encodingConfig types.EncodingConfig
types.BaseClient
Bank bank.Client
Token token.Client
Record record.Client
NFT nft.Client
Service service.Client
Key keys.Client
Perm perm.Client
Identity identity.Client
Node node.Client
Params params.Client
WASM wasm.Client
Oracle oracle.Client
}
// AppCodec return a Marshaler of the protobuf
func AppCodec(rs ...codec.RegisterInterfaces) codec.Marshaler {
encodingConfig := makeEncodingConfig()
registers = append(registers, rs...)
for _, register := range registers {
register(encodingConfig.InterfaceRegistry)
}
return encodingConfig.Marshaler
}
// NewIRITAClient return a instance of the IRITAClient
func NewIRITAClient(cfg types.ClientConfig) IRITAClient {
encodingConfig := makeEncodingConfig()
//create a instance of baseClient
baseClient := modules.NewBaseClient(cfg, encodingConfig, nil)
permClient := perm.NewClient(baseClient, encodingConfig.Marshaler)
bankClient := bank.NewClient(baseClient, encodingConfig.Marshaler)
idClient := identity.NewClient(baseClient, encodingConfig.Marshaler)
tokenClient := token.NewClient(baseClient, encodingConfig.Marshaler)
keysClient := keys.NewClient(baseClient)
recordClient := record.NewClient(baseClient, encodingConfig.Marshaler)
nftClient := nft.NewClient(baseClient, encodingConfig.Marshaler)
serviceClient := service.NewClient(baseClient, encodingConfig.Marshaler)
nodeClient := node.NewClient(baseClient, encodingConfig.Marshaler)
oracleClient := oracle.NewClient(baseClient, encodingConfig.Marshaler)
paramsClient := params.NewClient(baseClient, encodingConfig.Marshaler)
wasmClient := wasm.NewClient(baseClient)
client := &IRITAClient{
logger: baseClient.Logger(),
BaseClient: baseClient,
Bank: bankClient,
Token: tokenClient,
Key: keysClient,
Record: recordClient,
NFT: nftClient,
Service: serviceClient,
Perm: permClient,
Identity: idClient,
Node: nodeClient,
Oracle: oracleClient,
Params: paramsClient,
WASM: wasmClient,
moduleManager: make(map[string]types.Module),
encodingConfig: encodingConfig,
}
client.RegisterModule(
permClient,
bankClient,
idClient,
tokenClient,
recordClient,
nftClient,
serviceClient,
nodeClient,
oracleClient,
paramsClient,
wasmClient,
)
return *client
}
// SetLogger set the logger for irita client
func (client *IRITAClient) SetLogger(logger log.Logger) {
client.BaseClient.SetLogger(logger)
}
// Codec return the codec of the amnio
func (client *IRITAClient) Codec() *codec.LegacyAmino {
return client.encodingConfig.Amino
}
// AppCodec return the codec of the protobuf
func (client *IRITAClient) AppCodec() codec.Marshaler {
return client.encodingConfig.Marshaler
}
// Manager return the BaseClient
func (client *IRITAClient) Manager() types.BaseClient {
return client.BaseClient
}
// RegisterModule regisger the module for irita client
func (client *IRITAClient) 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
}
}
// Module return the subclient by the module name
func (client *IRITAClient) Module(name string) types.Module {
return client.moduleManager[name]
}
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)
}