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

     1  package reputation
     2  
     3  import (
     4  	wire "github.com/cosmos/cosmos-sdk/codec"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  	"github.com/lino-network/lino/types"
     7  	abci "github.com/tendermint/tendermint/abci/types"
     8  )
     9  
    10  const (
    11  	// ModuleKey is the name of the module
    12  	ModuleName = "reputation"
    13  
    14  	// RouterKey is the message route for gov
    15  	RouterKey = ModuleName
    16  
    17  	// QuerierRoute is the querier route for gov
    18  	QuerierRoute = ModuleName
    19  
    20  	QueryReputation = "rep"
    21  )
    22  
    23  // creates a querier for vote REST endpoints
    24  func NewQuerier(rm ReputationKeeper) sdk.Querier {
    25  	cdc := wire.New()
    26  	wire.RegisterCrypto(cdc)
    27  	return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
    28  		switch path[0] {
    29  		case QueryReputation:
    30  			return queryReputation(ctx, cdc, path[1:], req, rm)
    31  		default:
    32  			return nil, sdk.ErrUnknownRequest("unknown reputation query endpoint")
    33  		}
    34  	}
    35  }
    36  
    37  func queryReputation(ctx sdk.Context, cdc *wire.Codec, path []string, req abci.RequestQuery, rm ReputationKeeper) ([]byte, sdk.Error) {
    38  	if err := types.CheckPathContentAndMinLength(path, 1); err != nil {
    39  		return nil, err
    40  	}
    41  	reputation, err := rm.GetReputation(ctx, types.AccountKey(path[0]))
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	res, marshalErr := cdc.MarshalJSON(reputation)
    46  	if marshalErr != nil {
    47  		return nil, ErrQueryFailed()
    48  	}
    49  	return res, nil
    50  }