2019-11-25 19:46:02 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2020-04-23 16:35:58 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-23 16:35:58 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// NewQuerier returns a new querier function
|
2019-11-25 19:46:02 +00:00
|
|
|
func NewQuerier(keeper Keeper) sdk.Querier {
|
2020-04-23 16:35:58 +00:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
|
2019-11-25 19:46:02 +00:00
|
|
|
switch path[0] {
|
2020-01-12 15:35:34 +00:00
|
|
|
case types.QueryGetCdp:
|
|
|
|
return queryGetCdp(ctx, req, keeper)
|
2019-11-25 19:46:02 +00:00
|
|
|
case types.QueryGetCdps:
|
2020-01-12 15:35:34 +00:00
|
|
|
return queryGetCdpsByDenom(ctx, req, keeper)
|
|
|
|
case types.QueryGetCdpsByCollateralization:
|
|
|
|
return queryGetCdpsByRatio(ctx, req, keeper)
|
2019-11-25 19:46:02 +00:00
|
|
|
case types.QueryGetParams:
|
|
|
|
return queryGetParams(ctx, req, keeper)
|
2020-01-28 20:08:17 +00:00
|
|
|
case types.QueryGetCdpDeposits:
|
|
|
|
return queryGetDeposits(ctx, req, keeper)
|
2019-11-25 19:46:02 +00:00
|
|
|
default:
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint %s", types.ModuleName, path[0])
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// query a specific cdp
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryGetCdp(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-01-12 15:35:34 +00:00
|
|
|
var requestParams types.QueryCdpParams
|
2020-04-23 16:35:58 +00:00
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, &requestParams)
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
|
|
|
|
if !valid {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrInvalidCollateral, requestParams.CollateralDenom)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cdp, found := keeper.GetCdpByOwnerAndDenom(ctx, requestParams.Owner, requestParams.CollateralDenom)
|
|
|
|
if !found {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrCdpNotFound, "owner %s, denom %s", requestParams.Owner, requestParams.CollateralDenom)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 17:47:08 +00:00
|
|
|
augmentedCDP, err := keeper.LoadAugmentedCDP(ctx, cdp)
|
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrLoadingAugmentedCDP, fmt.Sprintf("%v: %d", err.Error(), cdp.ID))
|
2020-01-28 17:47:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, augmentedCDP)
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:08:17 +00:00
|
|
|
// query deposits on a particular cdp
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryGetDeposits(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-01-28 20:08:17 +00:00
|
|
|
var requestParams types.QueryCdpDeposits
|
2020-04-23 16:35:58 +00:00
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, &requestParams)
|
2020-01-28 20:08:17 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-01-28 20:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
|
|
|
|
if !valid {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrInvalidCollateral, requestParams.CollateralDenom)
|
2020-01-28 20:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cdp, found := keeper.GetCdpByOwnerAndDenom(ctx, requestParams.Owner, requestParams.CollateralDenom)
|
|
|
|
if !found {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrCdpNotFound, "owner %s, denom %s", requestParams.Owner, requestParams.CollateralDenom)
|
2020-01-28 20:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deposits := keeper.GetDeposits(ctx, cdp.ID)
|
|
|
|
|
2020-04-23 16:35:58 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, deposits)
|
2020-01-28 20:08:17 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-01-28 20:08:17 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// query cdps with matching denom and ratio LESS THAN the input ratio
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryGetCdpsByRatio(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-01-12 15:35:34 +00:00
|
|
|
var requestParams types.QueryCdpsByRatioParams
|
2020-04-23 16:35:58 +00:00
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, &requestParams)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
2020-01-12 15:35:34 +00:00
|
|
|
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
|
|
|
|
if !valid {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrInvalidCollateral, requestParams.CollateralDenom)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-28 17:47:08 +00:00
|
|
|
ratio, err := keeper.CalculateCollateralizationRatioFromAbsoluteRatio(ctx, requestParams.CollateralDenom, requestParams.Ratio)
|
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(err, "couldn't get collateralization ratio from absolute ratio")
|
2020-01-28 17:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cdps := keeper.GetAllCdpsByDenomAndRatio(ctx, requestParams.CollateralDenom, ratio)
|
|
|
|
// augment CDPs by adding collateral value and collateralization ratio
|
|
|
|
var augmentedCDPs types.AugmentedCDPs
|
|
|
|
for _, cdp := range cdps {
|
|
|
|
augmentedCDP, err := keeper.LoadAugmentedCDP(ctx, cdp)
|
|
|
|
if err == nil {
|
|
|
|
augmentedCDPs = append(augmentedCDPs, augmentedCDP)
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, augmentedCDPs)
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// query all cdps with matching collateral denom
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryGetCdpsByDenom(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-01-12 15:35:34 +00:00
|
|
|
var requestParams types.QueryCdpsParams
|
2020-04-23 16:35:58 +00:00
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, &requestParams)
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
|
|
|
|
if !valid {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrInvalidCollateral, requestParams.CollateralDenom)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
cdps := keeper.GetAllCdpsByDenom(ctx, requestParams.CollateralDenom)
|
2020-01-28 17:47:08 +00:00
|
|
|
// augment CDPs by adding collateral value and collateralization ratio
|
|
|
|
var augmentedCDPs types.AugmentedCDPs
|
|
|
|
for _, cdp := range cdps {
|
|
|
|
augmentedCDP, err := keeper.LoadAugmentedCDP(ctx, cdp)
|
|
|
|
if err == nil {
|
|
|
|
augmentedCDPs = append(augmentedCDPs, augmentedCDP)
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 16:35:58 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, augmentedCDPs)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// query params in the cdp store
|
2020-04-23 16:35:58 +00:00
|
|
|
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2019-11-25 19:46:02 +00:00
|
|
|
// Get params
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
2020-04-23 16:35:58 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, params)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:35:58 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|