2019-06-18 06:44:09 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
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"
|
2020-02-20 06:24:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2019-06-18 06:44:09 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier returns a new sdk.Keeper instance.
|
2020-08-26 02:39:38 -07:00
|
|
|
func NewQuerier(k Keeper, 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-06-18 06:44:09 -07:00
|
|
|
switch path[0] {
|
2020-01-30 13:31:16 -08:00
|
|
|
case types.QueryBalance:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryBalance(ctx, req, k, legacyQuerierCdc)
|
2019-06-18 06:44:09 -07:00
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
case types.QueryAllBalances:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryAllBalance(ctx, req, k, legacyQuerierCdc)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
case types.QueryTotalSupply:
|
2020-07-25 01:03:58 -07:00
|
|
|
return queryTotalSupply(ctx, req, k, legacyQuerierCdc)
|
2020-04-20 08:22:12 -07:00
|
|
|
|
|
|
|
case types.QuerySupplyOf:
|
2020-07-25 01:03:58 -07:00
|
|
|
return querySupplyOf(ctx, req, k, legacyQuerierCdc)
|
2020-04-20 08:22:12 -07:00
|
|
|
|
2019-06-18 06:44:09 -07:00
|
|
|
default:
|
2020-01-30 13:31:16 -08:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
|
2019-06-18 06:44:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-06-05 11:10:04 -07:00
|
|
|
var params types.QueryBalanceRequest
|
2019-06-18 06:44:09 -07:00
|
|
|
|
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-06-18 06:44:09 -07:00
|
|
|
}
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
address, err := sdk.AccAddressFromBech32(params.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
balance := k.GetBalance(ctx, address, params.Denom)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, balance)
|
2020-01-30 13:31:16 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-10-28 07:09:04 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryAllBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-06-05 11:10:04 -07:00
|
|
|
var params types.QueryAllBalancesRequest
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
if err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms); err != nil {
|
2020-01-30 13:31:16 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
address, err := sdk.AccAddressFromBech32(params.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
balances := k.GetAllBalances(ctx, address)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, balances)
|
2019-06-18 06:44:09 -07:00
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-06-18 06:44:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-04-20 08:22:12 -07:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func queryTotalSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-04-06 07:43:08 -07:00
|
|
|
var params types.QueryTotalSupplyRequest
|
2020-04-20 08:22:12 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-04-20 08:22:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-04-06 07:43:08 -07:00
|
|
|
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(ctx, params.Pagination)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
2020-04-20 08:22:12 -07:00
|
|
|
|
2021-04-06 07:43:08 -07:00
|
|
|
supplyRes := &types.QueryTotalSupplyResponse{
|
|
|
|
Supply: totalSupply,
|
|
|
|
Pagination: pageRes,
|
2020-04-20 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2021-04-06 07:43:08 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, supplyRes)
|
2020-04-20 08:22:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func querySupplyOf(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-04-20 08:22:12 -07:00
|
|
|
var params types.QuerySupplyOfParams
|
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-04-20 08:22:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-03-03 01:58:16 -08:00
|
|
|
amount := k.GetSupply(ctx, params.Denom)
|
|
|
|
supply := sdk.NewCoin(params.Denom, amount.Amount)
|
2020-04-20 08:22:12 -07:00
|
|
|
|
2020-07-25 01:03:58 -07:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, supply)
|
2020-04-20 08:22:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
2020-06-26 09:30:49 -07:00
|
|
|
return bz, nil
|
2020-04-20 08:22:12 -07:00
|
|
|
}
|