-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathmodule.go
218 lines (180 loc) · 6.57 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package module
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/protobuf/reflect/protoreflect"
abci "github.com/tendermint/tendermint/abci/types"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/orm/model/ormdb"
"github.com/cosmos/cosmos-sdk/orm/types/ormjson"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
climodule "github.com/regen-network/regen-ledger/types/module/client/cli"
restmodule "github.com/regen-network/regen-ledger/types/module/client/grpc_gateway"
servermodule "github.com/regen-network/regen-ledger/types/module/server"
"github.com/regen-network/regen-ledger/x/ecocredit/v2"
baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/v2/basket"
"github.com/regen-network/regen-ledger/x/ecocredit/v2/client"
coretypes "github.com/regen-network/regen-ledger/x/ecocredit/v2/core"
"github.com/regen-network/regen-ledger/x/ecocredit/v2/genesis"
marketplacetypes "github.com/regen-network/regen-ledger/x/ecocredit/v2/marketplace"
"github.com/regen-network/regen-ledger/x/ecocredit/v2/server"
"github.com/regen-network/regen-ledger/x/ecocredit/v2/simulation"
)
type Module struct {
paramSpace paramtypes.Subspace
accountKeeper ecocredit.AccountKeeper
bankKeeper ecocredit.BankKeeper
Keeper server.Keeper
}
// NewModule returns a new Module object.
func NewModule(
paramSpace paramtypes.Subspace,
accountKeeper ecocredit.AccountKeeper,
bankKeeper ecocredit.BankKeeper,
) *Module {
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(coretypes.ParamKeyTable())
}
return &Module{
paramSpace: paramSpace,
bankKeeper: bankKeeper,
accountKeeper: accountKeeper,
}
}
var _ module.AppModuleBasic = &Module{}
var _ servermodule.Module = &Module{}
var _ restmodule.Module = &Module{}
var _ climodule.Module = &Module{}
var _ module.AppModuleSimulation = &Module{}
func (a Module) Name() string {
return ecocredit.ModuleName
}
func (a Module) RegisterInterfaces(registry types.InterfaceRegistry) {
baskettypes.RegisterTypes(registry)
coretypes.RegisterTypes(registry)
marketplacetypes.RegisterTypes(registry)
}
func (a *Module) RegisterServices(configurator servermodule.Configurator) {
a.Keeper = server.RegisterServices(configurator, a.paramSpace, a.accountKeeper, a.bankKeeper)
}
//nolint:errcheck
func (a Module) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *runtime.ServeMux) {
ctx := context.Background()
baskettypes.RegisterQueryHandlerClient(ctx, mux, baskettypes.NewQueryClient(clientCtx))
marketplacetypes.RegisterQueryHandlerClient(ctx, mux, marketplacetypes.NewQueryClient(clientCtx))
coretypes.RegisterQueryHandlerClient(ctx, mux, coretypes.NewQueryClient(clientCtx))
}
func (a Module) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
db, err := ormdb.NewModuleDB(&ecocredit.ModuleSchema, ormdb.ModuleDBOptions{})
if err != nil {
panic(err)
}
jsonTarget := ormjson.NewRawMessageTarget()
err = db.DefaultJSON(jsonTarget)
if err != nil {
panic(err)
}
params := coretypes.DefaultParams()
err = genesis.MergeParamsIntoTarget(cdc, ¶ms, jsonTarget)
if err != nil {
panic(err)
}
creditTypes := coretypes.DefaultCreditTypes()
err = genesis.MergeCreditTypesIntoTarget(creditTypes, jsonTarget)
if err != nil {
panic(err)
}
allowedDenoms := marketplacetypes.DefaultAllowedDenoms()
err = genesis.MergeAllowedDenomsIntoTarget(allowedDenoms, jsonTarget)
if err != nil {
panic(err)
}
bz, err := jsonTarget.JSON()
if err != nil {
panic(err)
}
return bz
}
func (a Module) ValidateGenesis(cdc codec.JSONCodec, _ sdkclient.TxEncodingConfig, bz json.RawMessage) error {
db, err := ormdb.NewModuleDB(&ecocredit.ModuleSchema, ormdb.ModuleDBOptions{})
if err != nil {
return err
}
jsonSource, err := ormjson.NewRawMessageSource(bz)
if err != nil {
return err
}
err = db.ValidateJSON(jsonSource)
if err != nil {
return err
}
var params coretypes.Params
r, err := jsonSource.OpenReader(protoreflect.FullName(proto.MessageName(¶ms)))
if err != nil {
return err
}
if r == nil {
return nil
}
if err := (&jsonpb.Unmarshaler{AllowUnknownFields: true}).Unmarshal(r, ¶ms); err != nil {
return fmt.Errorf("failed to unmarshal %s params state: %w", ecocredit.ModuleName, err)
}
return genesis.ValidateGenesis(bz, params)
}
func (a Module) GetQueryCmd() *cobra.Command {
return client.QueryCmd(a.Name())
}
func (a Module) GetTxCmd() *cobra.Command {
return client.TxCmd(a.Name())
}
// ConsensusVersion implements AppModule/ConsensusVersion.
func (Module) ConsensusVersion() uint64 { return 2 }
/**** DEPRECATED ****/
func (a Module) RegisterRESTRoutes(sdkclient.Context, *mux.Router) {}
func (a Module) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
baskettypes.RegisterLegacyAminoCodec(cdc)
coretypes.RegisterLegacyAminoCodec(cdc)
marketplacetypes.RegisterLegacyAminoCodec(cdc)
}
// AppModuleSimulation functions
// GenerateGenesisState creates a randomized GenesisState of the ecocredit module.
func (Module) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}
// ProposalContents returns all the ecocredit content functions used to
// simulate proposals.
func (Module) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
return nil
}
// RandomizedParams creates randomized ecocredit param changes for the simulator.
func (Module) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
return simulation.ParamChanges()
}
// RegisterStoreDecoder registers a decoder for ecocredit module's types
func (Module) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
}
// WeightedOperations returns all the ecocredit module operations with their respective weights.
// NOTE: This is no longer needed for the modules which uses ADR-33, ecocredit module `WeightedOperations`
// registered in the `x/ecocredit/server` package.
func (Module) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return nil
}
// BeginBlock checks if there are any expired sell or buy orders and removes them from state.
func (a Module) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
err := server.BeginBlocker(ctx, a.Keeper)
if err != nil {
panic(err)
}
}