-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
126 lines (109 loc) · 3.76 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
package sdk
import (
"github.com/irisnet/core-sdk-go/feegrant"
"github.com/tendermint/tendermint/libs/log"
"github.com/irisnet/core-sdk-go/bank"
"github.com/irisnet/core-sdk-go/client"
keys "github.com/irisnet/core-sdk-go/client"
commoncodec "github.com/irisnet/core-sdk-go/common/codec"
cryptotypes "github.com/irisnet/core-sdk-go/common/codec/types"
commoncryptocodec "github.com/irisnet/core-sdk-go/common/crypto/codec"
"github.com/irisnet/core-sdk-go/gov"
"github.com/irisnet/core-sdk-go/ibc/transfer"
"github.com/irisnet/core-sdk-go/staking"
"github.com/irisnet/core-sdk-go/types"
txtypes "github.com/irisnet/core-sdk-go/types/tx"
)
type Client struct {
logger log.Logger
moduleManager map[string]types.Module
encodingConfig types.EncodingConfig
types.BaseClient
Bank bank.Client
Key keys.Client
Staking staking.Client
Gov gov.Client
Transfer transfer.Client
FeeGrant feegrant.Client
}
func NewClient(cfg types.ClientConfig) Client {
encodingConfig := makeEncodingConfig()
// create a instance of baseClient
baseClient := client.NewBaseClient(cfg, encodingConfig, nil)
bankClient := bank.NewClient(baseClient, encodingConfig.Marshaler)
keysClient := keys.NewKeysClient(cfg, baseClient)
transferClient := transfer.NewClient(baseClient, encodingConfig.Marshaler)
stakingClient := staking.NewClient(baseClient, encodingConfig.Marshaler)
govClient := gov.NewClient(baseClient, encodingConfig.Marshaler)
feeGrantClient := feegrant.NewClient(baseClient, encodingConfig.Marshaler)
client := Client{
logger: baseClient.Logger(),
BaseClient: baseClient,
moduleManager: make(map[string]types.Module),
encodingConfig: encodingConfig,
Bank: bankClient,
Key: keysClient,
Staking: stakingClient,
Gov: govClient,
Transfer: transferClient,
FeeGrant: feeGrantClient,
}
client.RegisterModule(
bankClient,
stakingClient,
govClient,
transferClient,
feeGrantClient,
)
return client
}
func (client *Client) SetLogger(logger log.Logger) {
client.BaseClient.SetLogger(logger)
}
func (client *Client) Codec() *commoncodec.LegacyAmino {
return client.encodingConfig.Amino
}
func (client *Client) AppCodec() commoncodec.Marshaler {
return client.encodingConfig.Marshaler
}
func (client *Client) EncodingConfig() types.EncodingConfig {
return client.encodingConfig
}
func (client *Client) Manager() types.BaseClient {
return client.BaseClient
}
func (client *Client) RegisterModule(ms ...types.Module) {
for _, m := range ms {
m.RegisterInterfaceTypes(client.encodingConfig.InterfaceRegistry)
}
}
func (client *Client) Module(name string) types.Module {
return client.moduleManager[name]
}
func makeEncodingConfig() types.EncodingConfig {
amino := commoncodec.NewLegacyAmino()
interfaceRegistry := cryptotypes.NewInterfaceRegistry()
marshaler := commoncodec.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 *commoncodec.LegacyAmino) {
cdc.RegisterInterface((*types.Msg)(nil), nil)
cdc.RegisterInterface((*types.Tx)(nil), nil)
commoncryptocodec.RegisterCrypto(cdc)
}
// RegisterInterfaces registers the sdk message type.
func RegisterInterfaces(registry cryptotypes.InterfaceRegistry) {
registry.RegisterInterface("cosmos.v1beta1.Msg", (*types.Msg)(nil))
txtypes.RegisterInterfaces(registry)
commoncryptocodec.RegisterInterfaces(registry)
}