2019-11-25 19:46:02 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"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 {
|
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
|
|
|
|
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:
|
|
|
|
return nil, sdk.ErrUnknownRequest("unknown cdp query endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// query a specific cdp
|
|
|
|
func queryGetCdp(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
var requestParams types.QueryCdpParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err != nil {
|
2020-01-16 15:20:12 +00:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", 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 {
|
|
|
|
return nil, types.ErrInvalidCollateralDenom(keeper.codespace, requestParams.CollateralDenom)
|
|
|
|
}
|
|
|
|
|
|
|
|
cdp, found := keeper.GetCdpByOwnerAndDenom(ctx, requestParams.Owner, requestParams.CollateralDenom)
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrCdpNotFound(keeper.codespace, requestParams.Owner, requestParams.CollateralDenom)
|
|
|
|
}
|
|
|
|
|
2020-01-28 17:47:08 +00:00
|
|
|
augmentedCDP, err := keeper.LoadAugmentedCDP(ctx, cdp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, types.ErrLoadingAugmentedCDP(keeper.codespace, cdp.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, augmentedCDP)
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:08:17 +00:00
|
|
|
// query deposits on a particular cdp
|
|
|
|
func queryGetDeposits(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
var requestParams types.QueryCdpDeposits
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
|
|
|
|
if !valid {
|
|
|
|
return nil, types.ErrInvalidCollateralDenom(keeper.codespace, requestParams.CollateralDenom)
|
|
|
|
}
|
|
|
|
|
|
|
|
cdp, found := keeper.GetCdpByOwnerAndDenom(ctx, requestParams.Owner, requestParams.CollateralDenom)
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrCdpNotFound(keeper.codespace, requestParams.Owner, requestParams.CollateralDenom)
|
|
|
|
}
|
|
|
|
|
|
|
|
deposits := keeper.GetDeposits(ctx, cdp.ID)
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, deposits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// query cdps with matching denom and ratio LESS THAN the input ratio
|
|
|
|
func queryGetCdpsByRatio(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
var requestParams types.QueryCdpsByRatioParams
|
2019-11-25 19:46:02 +00:00
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err != nil {
|
2020-01-16 15:20:12 +00:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", 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 {
|
|
|
|
return nil, types.ErrInvalidCollateralDenom(keeper.codespace, requestParams.CollateralDenom)
|
|
|
|
}
|
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 {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could get collateralization ratio from absolute ratio", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, augmentedCDPs)
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
|
|
|
|
}
|
|
|
|
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
|
|
|
|
func queryGetCdpsByDenom(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
var requestParams types.QueryCdpsParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, &requestParams)
|
|
|
|
if err != nil {
|
2020-01-16 15:20:12 +00:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err.Error()))
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
_, valid := keeper.GetDenomPrefix(ctx, requestParams.CollateralDenom)
|
|
|
|
if !valid {
|
|
|
|
return nil, types.ErrInvalidCollateralDenom(keeper.codespace, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, augmentedCDPs)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// query params in the cdp store
|
2019-11-25 19:46:02 +00:00
|
|
|
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
// Get params
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|