-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
196 lines (154 loc) · 5.33 KB
/
module.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
195
196
// Copyright 2024 NASD Inc.
//
// Use of this source code is governed by a BSL-style
// license that can be found in the LICENSE file or at
// https://mariadb.com/bsl11.
package halo
import (
"context"
"encoding/json"
"fmt"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/event"
"cosmossdk.io/core/header"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
modulev1 "github.com/noble-assets/halo/v3/api/module/v1"
"github.com/noble-assets/halo/v3/client/cli"
"github.com/noble-assets/halo/v3/keeper"
"github.com/noble-assets/halo/v3/types"
"github.com/noble-assets/halo/v3/types/aggregator"
"github.com/noble-assets/halo/v3/types/entitlements"
"github.com/spf13/cobra"
)
// ConsensusVersion defines the current x/halo module consensus version.
const ConsensusVersion = 1
var (
_ module.AppModuleBasic = AppModule{}
_ appmodule.AppModule = AppModule{}
_ module.HasConsensusVersion = AppModule{}
_ module.HasGenesis = AppModule{}
_ module.HasGenesisBasics = AppModuleBasic{}
_ module.HasServices = AppModule{}
)
//
type AppModuleBasic struct {
addressCodec address.Codec
}
func NewAppModuleBasic(addressCodec address.Codec) AppModuleBasic {
return AppModuleBasic{addressCodec: addressCodec}
}
func (AppModuleBasic) Name() string { return types.ModuleName }
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}
func (AppModuleBasic) RegisterInterfaces(reg codectypes.InterfaceRegistry) {
types.RegisterInterfaces(reg)
}
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
if err := aggregator.RegisterQueryHandlerClient(context.Background(), mux, aggregator.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
if err := entitlements.RegisterQueryHandlerClient(context.Background(), mux, entitlements.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
}
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesisState())
}
func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var genesis types.GenesisState
if err := cdc.UnmarshalJSON(bz, &genesis); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}
return genesis.Validate(b.addressCodec)
}
//
type AppModule struct {
AppModuleBasic
keeper *keeper.Keeper
}
func NewAppModule(keeper *keeper.Keeper, addressCodec address.Codec) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(addressCodec),
keeper: keeper,
}
}
func (AppModule) IsOnePerModuleType() {}
func (AppModule) IsAppModule() {}
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
func (m AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) {
var genesis types.GenesisState
cdc.MustUnmarshalJSON(bz, &genesis)
InitGenesis(ctx, m.keeper, m.addressCodec, genesis)
}
func (m AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
genesis := ExportGenesis(ctx, m.keeper)
return cdc.MustMarshalJSON(genesis)
}
func (m AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(m.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(m.keeper))
aggregator.RegisterMsgServer(cfg.MsgServer(), keeper.NewAggregatorMsgServer(m.keeper))
aggregator.RegisterQueryServer(cfg.QueryServer(), keeper.NewAggregatorQueryServer(m.keeper))
entitlements.RegisterMsgServer(cfg.MsgServer(), keeper.NewEntitlementsMsgServer(m.keeper))
entitlements.RegisterQueryServer(cfg.QueryServer(), keeper.NewEntitlementsQueryServer(m.keeper))
}
//
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd()
}
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}
//
func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(ProvideModule),
)
}
type ModuleInputs struct {
depinject.In
Config *modulev1.Module
Cdc codec.Codec
StoreService store.KVStoreService
EventService event.Service
HeaderService header.Service
InterfaceRegistry codectypes.InterfaceRegistry
AddressCodec address.Codec
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
}
type ModuleOutputs struct {
depinject.Out
Keeper *keeper.Keeper
Module appmodule.AppModule
Restrictions banktypes.SendRestrictionFn
}
func ProvideModule(in ModuleInputs) ModuleOutputs {
k := keeper.NewKeeper(
in.Cdc,
in.StoreService,
in.EventService,
in.HeaderService,
in.Config.Denom,
in.Config.Underlying,
in.AddressCodec,
in.AccountKeeper,
in.BankKeeper,
in.InterfaceRegistry,
)
m := NewAppModule(k, in.AddressCodec)
return ModuleOutputs{Keeper: k, Module: m, Restrictions: k.SendRestrictionFn}
}