github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/bank/module.go (about)

     1  package bank
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"math/rand"
     7  
     8  	"github.com/gorilla/mux"
     9  	"github.com/spf13/cobra"
    10  
    11  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    12  
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    15  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    16  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
    17  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank/client/cli"
    18  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank/client/rest"
    19  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank/internal/keeper"
    20  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank/internal/types"
    21  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank/simulation"
    22  	sim "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation"
    23  )
    24  
    25  var (
    26  	_ module.AppModule           = AppModule{}
    27  	_ module.AppModuleBasic      = AppModuleBasic{}
    28  	_ module.AppModuleSimulation = AppModule{}
    29  )
    30  
    31  // AppModuleBasic defines the basic application module used by the bank module.
    32  type AppModuleBasic struct{}
    33  
    34  // Name returns the bank module's name.
    35  func (AppModuleBasic) Name() string { return ModuleName }
    36  
    37  // RegisterCodec registers the bank module's types for the given codec.
    38  func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { RegisterCodec(cdc) }
    39  
    40  // DefaultGenesis returns default genesis state as raw bytes for the bank
    41  // module.
    42  func (AppModuleBasic) DefaultGenesis() json.RawMessage {
    43  	return ModuleCdc.MustMarshalJSON(DefaultGenesisState())
    44  }
    45  
    46  // ValidateGenesis performs genesis state validation for the bank module.
    47  func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
    48  	var data GenesisState
    49  	if err := ModuleCdc.UnmarshalJSON(bz, &data); err != nil {
    50  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err)
    51  	}
    52  
    53  	return ValidateGenesis(data)
    54  }
    55  
    56  // RegisterRESTRoutes registers the REST routes for the bank module.
    57  func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
    58  	rest.RegisterRoutes(ctx, rtr)
    59  }
    60  
    61  // GetTxCmd returns the root tx command for the bank module.
    62  func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
    63  	return cli.GetTxCmd(cdc)
    64  }
    65  
    66  // GetQueryCmd returns no root query command for the bank module.
    67  func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil }
    68  
    69  //____________________________________________________________________________
    70  
    71  // AppModule implements an application module for the bank module.
    72  type AppModule struct {
    73  	AppModuleBasic
    74  
    75  	keeper        Keeper
    76  	accountKeeper types.AccountKeeper
    77  	adapterKeeper *BankKeeperAdapter
    78  	supplyKeeper  SupplyKeeper
    79  }
    80  
    81  // NewAppModule creates a new AppModule object
    82  func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper, supplyKeeper SupplyKeeper) AppModule {
    83  	return AppModule{
    84  		AppModuleBasic: AppModuleBasic{},
    85  		keeper:         keeper,
    86  		accountKeeper:  accountKeeper,
    87  		adapterKeeper:  NewBankKeeperAdapter(keeper),
    88  		supplyKeeper:   supplyKeeper,
    89  	}
    90  }
    91  
    92  // Name returns the bank module's name.
    93  func (AppModule) Name() string { return ModuleName }
    94  
    95  // RegisterInvariants registers the bank module invariants.
    96  func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
    97  	keeper.RegisterInvariants(ir, am.accountKeeper)
    98  }
    99  
   100  // Route returns the message routing key for the bank module.
   101  func (AppModule) Route() string { return RouterKey }
   102  
   103  // NewHandler returns an sdk.Handler for the bank module.
   104  func (am AppModule) NewHandler() sdk.Handler { return nil }
   105  
   106  // QuerierRoute returns the bank module's querier route name.
   107  func (AppModule) QuerierRoute() string { return RouterKey }
   108  
   109  // NewQuerierHandler returns the bank module sdk.Querier.
   110  func (am AppModule) NewQuerierHandler() sdk.Querier {
   111  	return keeper.NewQuerier(am.keeper)
   112  }
   113  
   114  // InitGenesis performs genesis initialization for the bank module. It returns
   115  // no validator updates.
   116  func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
   117  	var genesisState GenesisState
   118  	ModuleCdc.MustUnmarshalJSON(data, &genesisState)
   119  	InitGenesis(ctx, am.keeper, genesisState)
   120  	return []abci.ValidatorUpdate{}
   121  }
   122  
   123  // ExportGenesis returns the exported genesis state as raw bytes for the bank
   124  // module.
   125  func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
   126  	gs := ExportGenesis(ctx, am.keeper)
   127  	return ModuleCdc.MustMarshalJSON(gs)
   128  }
   129  
   130  // BeginBlock performs a no-op.
   131  func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
   132  	keeper.BeginBlocker(ctx, req, am.keeper.GetInnerTxKeeper())
   133  }
   134  
   135  // EndBlock returns the end blocker for the bank module. It returns no validator
   136  // updates.
   137  func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   138  	return []abci.ValidatorUpdate{}
   139  }
   140  
   141  //____________________________________________________________________________
   142  
   143  // AppModuleSimulation functions
   144  
   145  // GenerateGenesisState creates a randomized GenState of the bank module.
   146  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   147  	simulation.RandomizedGenState(simState)
   148  }
   149  
   150  // ProposalContents doesn't return any content functions for governance proposals.
   151  func (AppModule) ProposalContents(_ module.SimulationState) []sim.WeightedProposalContent {
   152  	return nil
   153  }
   154  
   155  // RandomizedParams creates randomized bank param changes for the simulator.
   156  func (AppModule) RandomizedParams(r *rand.Rand) []sim.ParamChange {
   157  	return simulation.ParamChanges(r)
   158  }
   159  
   160  // RegisterStoreDecoder performs a no-op.
   161  func (AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {}
   162  
   163  // WeightedOperations returns the all the gov module operations with their respective weights.
   164  func (am AppModule) WeightedOperations(simState module.SimulationState) []sim.WeightedOperation {
   165  	return simulation.WeightedOperations(
   166  		simState.AppParams, simState.Cdc, am.accountKeeper, am.keeper,
   167  	)
   168  }