2019-07-29 08:27:01 -07:00
|
|
|
package keeper
|
2019-02-28 11:40:32 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-12-27 09:57:54 -08:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-02-28 11:40:32 -08:00
|
|
|
)
|
|
|
|
|
2019-07-29 08:27:01 -07:00
|
|
|
// NewQuerier creates a querier for auth REST endpoints
|
2020-08-26 02:39:38 -07:00
|
|
|
func NewQuerier(k AccountKeeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
|
2019-12-27 09:57:54 -08:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
|
2019-02-28 11:40:32 -08:00
|
|
|
switch path[0] {
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.QueryAccount:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryAccount(ctx, req, k, legacyQuerierCdc)
|
2020-02-26 07:33:56 -08:00
|
|
|
|
|
|
|
case types.QueryParams:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryParams(ctx, k, legacyQuerierCdc)
|
2020-02-26 07:33:56 -08:00
|
|
|
|
2019-02-28 11:40:32 -08:00
|
|
|
default:
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
|
2019-02-28 11:40:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryAccount(ctx sdk.Context, req abci.RequestQuery, k AccountKeeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-07-08 04:12:38 -07:00
|
|
|
var params types.QueryAccountRequest
|
2020-07-25 01:03:58 -07:00
|
|
|
if err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms); err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-02-28 11:40:32 -08:00
|
|
|
}
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
addr, err := sdk.AccAddressFromBech32(params.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
account := k.GetAccount(ctx, addr)
|
2019-02-28 19:19:18 -08:00
|
|
|
if account == nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "account %s does not exist", params.Address)
|
2019-02-28 19:19:18 -08:00
|
|
|
}
|
2019-02-28 11:40:32 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, account)
|
2019-02-28 11:40:32 -08:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-02-28 11:40:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-02-26 07:33:56 -08:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryParams(ctx sdk.Context, k AccountKeeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-02-26 07:33:56 -08:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, params)
|
2020-02-26 07:33:56 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|