github.com/lino-network/lino@v0.6.11/param/querier.go (about)

     1  package param
     2  
     3  import (
     4  	wire "github.com/cosmos/cosmos-sdk/codec"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  	abci "github.com/tendermint/tendermint/abci/types"
     7  )
     8  
     9  const (
    10  	// ModuleKey is the name of the module
    11  	ModuleName = "param"
    12  
    13  	// RouterKey is the message route for param
    14  	RouterKey = ModuleName
    15  
    16  	// QuerierRoute is the querier route for param
    17  	QuerierRoute = ModuleName
    18  
    19  	QueryAllocationParam = "allocation"
    20  	QueryDeveloperParam  = "developer"
    21  	QueryVoteParam       = "vote"
    22  	QueryProposalParam   = "proposal"
    23  	QueryValidatorParam  = "validator"
    24  	QueryBandwidthParam  = "bandwidth"
    25  	QueryAccountParam    = "account"
    26  	QueryPostParam       = "post"
    27  	QueryReputationParam = "reputation"
    28  	QueryPriceParam      = "price"
    29  )
    30  
    31  // creates a querier for account REST endpoints
    32  func NewQuerier(ph ParamHolder) sdk.Querier {
    33  	cdc := wire.New()
    34  	wire.RegisterCrypto(cdc)
    35  	return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
    36  		switch path[0] {
    37  		case QueryAllocationParam:
    38  			return queryAllocationParam(ctx, cdc, path[1:], req, ph)
    39  		case QueryDeveloperParam:
    40  			return queryDeveloperParam(ctx, cdc, path[1:], req, ph)
    41  		case QueryVoteParam:
    42  			return queryVoteParam(ctx, cdc, path[1:], req, ph)
    43  		case QueryProposalParam:
    44  			return queryProposalParam(ctx, cdc, path[1:], req, ph)
    45  		case QueryValidatorParam:
    46  			return queryValidatorParam(ctx, cdc, path[1:], req, ph)
    47  		case QueryBandwidthParam:
    48  			return queryBandwidthParam(ctx, cdc, path[1:], req, ph)
    49  		case QueryAccountParam:
    50  			return queryAccountParam(ctx, cdc, path[1:], req, ph)
    51  		case QueryPostParam:
    52  			return queryPostParam(ctx, cdc, path[1:], req, ph)
    53  		case QueryReputationParam:
    54  			return queryReputationParam(ctx, cdc, path[1:], req, ph)
    55  		case QueryPriceParam:
    56  			return queryPriceParam(ctx, cdc, path[1:], req, ph)
    57  		default:
    58  			return nil, sdk.ErrUnknownRequest("unknown param query endpoint")
    59  		}
    60  	}
    61  }
    62  
    63  func queryAllocationParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
    64  	globalAllocationParam := ph.GetGlobalAllocationParam(ctx)
    65  	res, marshalErr := cdc.MarshalJSON(globalAllocationParam)
    66  	if marshalErr != nil {
    67  		return nil, ErrQueryFailed()
    68  	}
    69  	return res, nil
    70  }
    71  
    72  func queryDeveloperParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
    73  	devParam, err := ph.GetDeveloperParam(ctx)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	res, marshalErr := cdc.MarshalJSON(devParam)
    78  	if marshalErr != nil {
    79  		return nil, ErrQueryFailed()
    80  	}
    81  	return res, nil
    82  }
    83  
    84  func queryVoteParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
    85  	voteParam := ph.GetVoteParam(ctx)
    86  	res, marshalErr := cdc.MarshalJSON(voteParam)
    87  	if marshalErr != nil {
    88  		return nil, ErrQueryFailed()
    89  	}
    90  	return res, nil
    91  }
    92  
    93  func queryProposalParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
    94  	proposalParam, err := ph.GetProposalParam(ctx)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	res, marshalErr := cdc.MarshalJSON(proposalParam)
    99  	if marshalErr != nil {
   100  		return nil, ErrQueryFailed()
   101  	}
   102  	return res, nil
   103  }
   104  
   105  func queryValidatorParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
   106  	valParam := ph.GetValidatorParam(ctx)
   107  	res, marshalErr := cdc.MarshalJSON(valParam)
   108  	if marshalErr != nil {
   109  		return nil, ErrQueryFailed()
   110  	}
   111  	return res, nil
   112  }
   113  
   114  func queryBandwidthParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
   115  	bandwidthParam, err := ph.GetBandwidthParam(ctx)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	res, marshalErr := cdc.MarshalJSON(bandwidthParam)
   120  	if marshalErr != nil {
   121  		return nil, ErrQueryFailed()
   122  	}
   123  	return res, nil
   124  }
   125  
   126  func queryAccountParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
   127  	accParam := ph.GetAccountParam(ctx)
   128  	res, marshalErr := cdc.MarshalJSON(accParam)
   129  	if marshalErr != nil {
   130  		return nil, ErrQueryFailed()
   131  	}
   132  	return res, nil
   133  }
   134  
   135  func queryPostParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
   136  	postParam, err := ph.GetPostParam(ctx)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  	res, marshalErr := cdc.MarshalJSON(postParam)
   141  	if marshalErr != nil {
   142  		return nil, ErrQueryFailed()
   143  	}
   144  	return res, nil
   145  }
   146  
   147  func queryReputationParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
   148  	repParam := ph.GetReputationParam(ctx)
   149  	res, marshalErr := cdc.MarshalJSON(repParam)
   150  	if marshalErr != nil {
   151  		return nil, ErrQueryFailed()
   152  	}
   153  	return res, nil
   154  }
   155  
   156  func queryPriceParam(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, ph ParamHolder) ([]byte, sdk.Error) {
   157  	priceParam := ph.GetPriceParam(ctx)
   158  	res, marshalErr := cdc.MarshalJSON(priceParam)
   159  	if marshalErr != nil {
   160  		return nil, ErrQueryFailed()
   161  	}
   162  	return res, nil
   163  }