2019-06-18 06:44:09 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-06-18 06:44:09 -07:00
|
|
|
"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.
|
|
|
|
func NewQuerier(k Keeper) 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:
|
2019-06-18 06:44:09 -07:00
|
|
|
return queryBalance(ctx, req, k)
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
case types.QueryAllBalances:
|
|
|
|
return queryAllBalance(ctx, req, k)
|
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
case types.QueryTotalSupply:
|
|
|
|
return queryTotalSupply(ctx, req, k)
|
|
|
|
|
|
|
|
case types.QuerySupplyOf:
|
|
|
|
return querySupplyOf(ctx, req, k)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
func queryBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
2020-06-05 11:10:04 -07:00
|
|
|
var params types.QueryBalanceRequest
|
2019-06-18 06:44:09 -07:00
|
|
|
|
|
|
|
if err := types.ModuleCdc.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-01-30 13:31:16 -08:00
|
|
|
balance := k.GetBalance(ctx, params.Address, params.Denom)
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, balance)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryAllBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
2020-06-05 11:10:04 -07:00
|
|
|
var params types.QueryAllBalancesRequest
|
2020-01-30 13:31:16 -08:00
|
|
|
|
|
|
|
if err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms); err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
balances := k.GetAllBalances(ctx, params.Address)
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, 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
|
|
|
|
|
|
|
func queryTotalSupply(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
var params types.QueryTotalSupplyParams
|
|
|
|
|
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
totalSupply := k.GetSupply(ctx).GetTotal()
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(totalSupply), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
totalSupply = sdk.Coins{}
|
|
|
|
} else {
|
|
|
|
totalSupply = totalSupply[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := totalSupply.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func querySupplyOf(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
var params types.QuerySupplyOfParams
|
|
|
|
|
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
supply := k.GetSupply(ctx).GetTotal().AmountOf(params.Denom)
|
|
|
|
|
|
|
|
res, err := supply.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|