2020-04-24 15:20:34 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2021-01-18 19:12:37 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-04-24 15:20:34 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-04-24 15:20:34 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
2020-04-24 15:20:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier is the module level router for state queries
|
|
|
|
func NewQuerier(k Keeper) sdk.Querier {
|
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
|
|
|
|
switch path[0] {
|
|
|
|
case types.QueryGetParams:
|
|
|
|
return queryGetParams(ctx, req, k)
|
|
|
|
case types.QueryGetClaims:
|
|
|
|
return queryGetClaims(ctx, req, k)
|
|
|
|
default:
|
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query params in the store
|
|
|
|
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
// Get params
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
|
|
|
bz, err := codec.MarshalJSONIndent(k.cdc, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryGetClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
var requestParams types.QueryClaimsParams
|
|
|
|
err := k.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
2021-01-18 19:12:37 +00:00
|
|
|
var claims types.USDXMintingClaims
|
|
|
|
if len(requestParams.Owner) > 0 {
|
|
|
|
claim, _ := k.GetClaim(ctx, requestParams.Owner)
|
|
|
|
claims = append(claims, claim)
|
|
|
|
} else {
|
|
|
|
claims = k.GetAllClaims(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
var paginatedClaims types.USDXMintingClaims
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(claims), requestParams.Page, requestParams.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
paginatedClaims = types.USDXMintingClaims{}
|
|
|
|
} else {
|
|
|
|
paginatedClaims = claims[start:end]
|
|
|
|
}
|
2020-04-24 15:20:34 +00:00
|
|
|
|
2021-01-18 19:12:37 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(k.cdc, paginatedClaims)
|
2020-04-24 15:20:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|