-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
170 lines (146 loc) · 5.13 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
package sdk
import (
"fmt"
"github.com/irisnet/irishub-sdk-go/modules/coinswap"
"github.com/irisnet/irishub-sdk-go/modules/gov"
"github.com/irisnet/irishub-sdk-go/modules/htlc"
"github.com/irisnet/irishub-sdk-go/modules/nft"
"github.com/irisnet/irishub-sdk-go/modules/oracle"
"github.com/irisnet/irishub-sdk-go/modules/random"
"github.com/irisnet/irishub-sdk-go/modules/record"
"github.com/irisnet/irishub-sdk-go/modules/staking"
"github.com/tendermint/tendermint/libs/log"
"github.com/irisnet/irishub-sdk-go/codec"
cdctypes "github.com/irisnet/irishub-sdk-go/codec/types"
cryptocodec "github.com/irisnet/irishub-sdk-go/crypto/codec"
"github.com/irisnet/irishub-sdk-go/modules"
"github.com/irisnet/irishub-sdk-go/modules/bank"
"github.com/irisnet/irishub-sdk-go/modules/keys"
"github.com/irisnet/irishub-sdk-go/modules/service"
"github.com/irisnet/irishub-sdk-go/modules/token"
"github.com/irisnet/irishub-sdk-go/types"
txtypes "github.com/irisnet/irishub-sdk-go/types/tx"
)
type IRISHUBClient struct {
logger log.Logger
moduleManager map[string]types.Module
encodingConfig types.EncodingConfig
types.BaseClient
Key keys.Client
Bank bank.Client
Token token.Client
Staking staking.Client
Gov gov.Client
Service service.Client
Record record.Client
Random random.Client
NFT nft.Client
Oracle oracle.Client
HTLC htlc.Client
Swap coinswap.Client
}
func NewIRISHUBClient(cfg types.ClientConfig) IRISHUBClient {
encodingConfig := makeEncodingConfig()
// create a instance of baseClient
baseClient := modules.NewBaseClient(cfg, encodingConfig, nil)
keysClient := keys.NewClient(baseClient)
bankClient := bank.NewClient(baseClient, encodingConfig.Marshaler)
tokenClient := token.NewClient(baseClient, encodingConfig.Marshaler)
stakingClient := staking.NewClient(baseClient, encodingConfig.Marshaler)
govClient := gov.NewClient(baseClient, encodingConfig.Marshaler)
serviceClient := service.NewClient(baseClient, encodingConfig.Marshaler)
recordClient := record.NewClient(baseClient, encodingConfig.Marshaler)
nftClient := nft.NewClient(baseClient, encodingConfig.Marshaler)
randomClient := random.NewClient(baseClient, encodingConfig.Marshaler)
oracleClient := oracle.NewClient(baseClient, encodingConfig.Marshaler)
htlcClient := htlc.NewClient(baseClient, encodingConfig.Marshaler)
swapClient := coinswap.NewClient(baseClient, encodingConfig.Marshaler, bankClient.TotalSupply)
client := &IRISHUBClient{
logger: baseClient.Logger(),
BaseClient: baseClient,
moduleManager: make(map[string]types.Module),
encodingConfig: encodingConfig,
Key: keysClient,
Bank: bankClient,
Token: tokenClient,
Staking: stakingClient,
Gov: govClient,
Service: serviceClient,
Record: recordClient,
Random: randomClient,
NFT: nftClient,
Oracle: oracleClient,
HTLC: htlcClient,
Swap: swapClient,
}
client.RegisterModule(
bankClient,
tokenClient,
stakingClient,
govClient,
serviceClient,
recordClient,
nftClient,
randomClient,
oracleClient,
htlcClient,
swapClient,
)
return *client
}
func (client *IRISHUBClient) SetLogger(logger log.Logger) {
client.BaseClient.SetLogger(logger)
}
func (client *IRISHUBClient) Codec() *codec.LegacyAmino {
return client.encodingConfig.Amino
}
func (client *IRISHUBClient) AppCodec() codec.Marshaler {
return client.encodingConfig.Marshaler
}
func (client *IRISHUBClient) EncodingConfig() types.EncodingConfig {
return client.encodingConfig
}
func (client *IRISHUBClient) Manager() types.BaseClient {
return client.BaseClient
}
func (client *IRISHUBClient) 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 (client *IRISHUBClient) 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)
}