github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/ammswap/module.go (about) 1 package ammswap 2 3 import ( 4 "encoding/json" 5 6 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 7 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 8 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module" 10 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 11 "github.com/fibonacci-chain/fbc/x/ammswap/client/cli" 12 "github.com/fibonacci-chain/fbc/x/ammswap/client/rest" 13 "github.com/gorilla/mux" 14 "github.com/spf13/cobra" 15 ) 16 17 // Type check to ensure the interface is properly implemented 18 var ( 19 _ module.AppModule = AppModule{} 20 _ module.AppModuleBasic = AppModuleBasic{} 21 ) 22 23 // AppModuleBasic defines the basic application module used by the swap module. 24 type AppModuleBasic struct{} 25 26 // Name returns the swap module's name. 27 func (AppModuleBasic) Name() string { 28 return ModuleName 29 } 30 31 // RegisterCodec registers the swap module's types for the given codec. 32 func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { 33 RegisterCodec(cdc) 34 } 35 36 // DefaultGenesis returns default genesis state as raw bytes for the swap 37 // module. 38 func (AppModuleBasic) DefaultGenesis() json.RawMessage { 39 return ModuleCdc.MustMarshalJSON(DefaultGenesisState()) 40 } 41 42 // ValidateGenesis performs genesis state validation for the swap module. 43 func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { 44 var data GenesisState 45 err := ModuleCdc.UnmarshalJSON(bz, &data) 46 if err != nil { 47 return err 48 } 49 return ValidateGenesis(data) 50 } 51 52 // RegisterRESTRoutes registers the REST routes for the swap module. 53 func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { 54 rest.RegisterRoutes(ctx, rtr) 55 } 56 57 // GetTxCmd returns the root tx command for the swap module. 58 func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { 59 return cli.GetTxCmd(cdc) 60 } 61 62 // GetQueryCmd returns no root query command for the swap module. 63 func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { 64 return cli.GetQueryCmd(StoreKey, cdc) 65 } 66 67 //____________________________________________________________________________ 68 69 // AppModule implements an application module for the swap module. 70 type AppModule struct { 71 AppModuleBasic 72 73 keeper Keeper 74 } 75 76 // NewAppModule creates a new AppModule object 77 func NewAppModule(k Keeper) AppModule { 78 return AppModule{ 79 AppModuleBasic: AppModuleBasic{}, 80 keeper: k, 81 } 82 } 83 84 // RegisterInvariants registers the swap module invariants. 85 func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} 86 87 // Route returns the message routing key for the swap module. 88 func (AppModule) Route() string { 89 return RouterKey 90 } 91 92 // NewHandler returns an sdk.Handler for the swap module. 93 func (am AppModule) NewHandler() sdk.Handler { 94 return NewHandler(am.keeper) 95 } 96 97 // QuerierRoute returns the swap module's querier route name. 98 func (AppModule) QuerierRoute() string { 99 return QuerierRoute 100 } 101 102 // NewQuerierHandler returns the swap module sdk.Querier. 103 func (am AppModule) NewQuerierHandler() sdk.Querier { 104 return NewQuerier(am.keeper) 105 } 106 107 // InitGenesis performs genesis initialization for the swap module. It returns 108 // no validator updates. 109 func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { 110 var genesisState GenesisState 111 ModuleCdc.MustUnmarshalJSON(data, &genesisState) 112 InitGenesis(ctx, am.keeper, genesisState) 113 return []abci.ValidatorUpdate{} 114 } 115 116 // ExportGenesis returns the exported genesis state as raw bytes for the swap 117 // module. 118 func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { 119 gs := ExportGenesis(ctx, am.keeper) 120 return ModuleCdc.MustMarshalJSON(gs) 121 } 122 123 // BeginBlock returns the begin blocker for the swap module. 124 func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { 125 BeginBlocker(ctx, am.keeper) 126 } 127 128 // EndBlock returns the end blocker for the swap module. It returns no validator 129 // updates. 130 func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { 131 return []abci.ValidatorUpdate{} 132 }