github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/keeper/querier.go (about)

     1  package keeper
     2  
     3  // DONTCOVER
     4  // client is excluded from test coverage in the poc phase milestone 1 and will be included in milestone 2 with completeness
     5  
     6  import (
     7  	"github.com/cosmos/cosmos-sdk/codec"
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
    10  	abci "github.com/tendermint/tendermint/abci/types"
    11  
    12  	"github.com/gravity-devs/liquidity/x/liquidity/types"
    13  )
    14  
    15  // NewQuerier creates a querier for liquidity REST endpoints
    16  func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
    17  	return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
    18  		switch path[0] {
    19  		case types.QueryLiquidityPool:
    20  			return queryLiquidityPool(ctx, req, k, legacyQuerierCdc)
    21  		case types.QueryLiquidityPools:
    22  			return queryLiquidityPools(ctx, req, k, legacyQuerierCdc)
    23  		default:
    24  			return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path of liquidity module: %s", path[0])
    25  		}
    26  	}
    27  }
    28  
    29  // return query result data of the query liquidity pool
    30  func queryLiquidityPool(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
    31  	var params types.QueryLiquidityPoolParams
    32  
    33  	if err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params); err != nil {
    34  		return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
    35  	}
    36  
    37  	liquidityPool, found := k.GetPool(ctx, params.PoolId)
    38  	if !found {
    39  		return nil, types.ErrPoolNotExists
    40  	}
    41  
    42  	bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, liquidityPool)
    43  	if err != nil {
    44  		return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
    45  	}
    46  	return bz, nil
    47  }
    48  
    49  // return query result data of the query liquidity pools
    50  func queryLiquidityPools(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
    51  	var params types.QueryLiquidityPoolsParams
    52  
    53  	if err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params); err != nil {
    54  		return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
    55  	}
    56  
    57  	liquidityPools := k.GetAllPools(ctx)
    58  
    59  	bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, liquidityPools)
    60  	if err != nil {
    61  		return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
    62  	}
    63  	return bz, nil
    64  }