2019-11-25 19:46:02 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2022-01-08 00:39:27 +00:00
|
|
|
"fmt"
|
2021-10-23 18:09:59 +00:00
|
|
|
"sort"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-11-25 19:46:02 +00:00
|
|
|
"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"
|
2022-01-08 00:39:27 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-04-23 16:35:58 +00:00
|
|
|
|
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
|
2022-01-08 00:39:27 +00:00
|
|
|
func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) 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:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetCdp(ctx, req, keeper, legacyQuerierCdc)
|
2019-11-25 19:46:02 +00:00
|
|
|
case types.QueryGetCdps:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetCdps(ctx, req, keeper, legacyQuerierCdc)
|
2020-09-17 00:45:10 +00:00
|
|
|
case types.QueryGetCdpDeposits:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetDeposits(ctx, req, keeper, legacyQuerierCdc)
|
2020-09-17 00:45:10 +00:00
|
|
|
case types.QueryGetCdpsByCollateralType: // legacy, maintained for REST API
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetCdpsByCollateralType(ctx, req, keeper, legacyQuerierCdc)
|
2020-09-17 00:45:10 +00:00
|
|
|
case types.QueryGetCdpsByCollateralization: // legacy, maintained for REST API
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetCdpsByRatio(ctx, req, keeper, legacyQuerierCdc)
|
2019-11-25 19:46:02 +00:00
|
|
|
case types.QueryGetParams:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetParams(ctx, req, keeper, legacyQuerierCdc)
|
2020-06-19 19:30:10 +00:00
|
|
|
case types.QueryGetAccounts:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetAccounts(ctx, req, keeper, legacyQuerierCdc)
|
2021-10-23 18:09:59 +00:00
|
|
|
case types.QueryGetTotalPrincipal:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetTotalPrincipal(ctx, req, keeper, legacyQuerierCdc)
|
2021-10-23 18:09:59 +00:00
|
|
|
case types.QueryGetTotalCollateral:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetTotalCollateral(ctx, req, keeper, legacyQuerierCdc)
|
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
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetCdp(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-01-12 15:35:34 +00:00
|
|
|
var requestParams types.QueryCdpParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.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
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
_, valid := keeper.GetCollateral(ctx, requestParams.CollateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
if !valid {
|
2020-09-17 00:45:10 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrInvalidCollateral, requestParams.CollateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
cdp, found := keeper.GetCdpByOwnerAndCollateralType(ctx, requestParams.Owner, requestParams.CollateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
if !found {
|
2020-08-21 19:42:46 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrCdpNotFound, "owner %s, denom %s", requestParams.Owner, requestParams.CollateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 14:19:19 +00:00
|
|
|
augmentedCDP := keeper.LoadAugmentedCDP(ctx, cdp)
|
2020-01-28 17:47:08 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, 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
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetDeposits(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-01-28 20:08:17 +00:00
|
|
|
var requestParams types.QueryCdpDeposits
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.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
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
_, valid := keeper.GetCollateral(ctx, requestParams.CollateralType)
|
2020-01-28 20:08:17 +00:00
|
|
|
if !valid {
|
2020-09-17 00:45:10 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrInvalidCollateral, requestParams.CollateralType)
|
2020-01-28 20:08:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
cdp, found := keeper.GetCdpByOwnerAndCollateralType(ctx, requestParams.Owner, requestParams.CollateralType)
|
2020-01-28 20:08:17 +00:00
|
|
|
if !found {
|
2020-08-21 19:42:46 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrCdpNotFound, "owner %s, denom %s", requestParams.Owner, requestParams.CollateralType)
|
2020-01-28 20:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deposits := keeper.GetDeposits(ctx, cdp.ID)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, 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
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetCdpsByRatio(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-01-12 15:35:34 +00:00
|
|
|
var requestParams types.QueryCdpsByRatioParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.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
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
_, valid := keeper.GetCollateral(ctx, requestParams.CollateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
if !valid {
|
2020-09-17 00:45:10 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrInvalidCollateral, requestParams.CollateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
ratio, err := keeper.CalculateCollateralizationRatioFromAbsoluteRatio(ctx, requestParams.CollateralType, requestParams.Ratio, "liquidation")
|
2020-01-28 17:47:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
cdps := keeper.GetAllCdpsByCollateralTypeAndRatio(ctx, requestParams.CollateralType, ratio)
|
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 {
|
2020-05-13 14:19:19 +00:00
|
|
|
augmentedCDP := keeper.LoadAugmentedCDP(ctx, cdp)
|
|
|
|
augmentedCDPs = append(augmentedCDPs, augmentedCDP)
|
2020-01-28 17:47:08 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, 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-09-17 00:45:10 +00:00
|
|
|
// query all cdps with matching collateral type
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetCdpsByCollateralType(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-09-17 00:45:10 +00:00
|
|
|
var requestParams types.QueryCdpsByCollateralTypeParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.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
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
_, valid := keeper.GetCollateral(ctx, requestParams.CollateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
if !valid {
|
2020-09-17 00:45:10 +00:00
|
|
|
return nil, sdkerrors.Wrap(types.ErrInvalidCollateral, requestParams.CollateralType)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
cdps := keeper.GetAllCdpsByCollateralType(ctx, requestParams.CollateralType)
|
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 {
|
2020-05-13 14:19:19 +00:00
|
|
|
augmentedCDP := keeper.LoadAugmentedCDP(ctx, cdp)
|
|
|
|
augmentedCDPs = append(augmentedCDPs, augmentedCDP)
|
2020-01-28 17:47:08 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, 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
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2019-11-25 19:46:02 +00:00
|
|
|
// Get params
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, 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
|
|
|
|
}
|
2020-06-19 19:30:10 +00:00
|
|
|
|
|
|
|
// query cdp module accounts
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetAccounts(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
|
|
|
cdpAccAccount := keeper.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
|
|
|
|
liquidatorAccAccount := keeper.accountKeeper.GetModuleAccount(ctx, types.LiquidatorMacc)
|
2020-06-19 19:30:10 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
accounts := []authtypes.ModuleAccount{
|
|
|
|
*cdpAccAccount.(*authtypes.ModuleAccount),
|
|
|
|
*liquidatorAccAccount.(*authtypes.ModuleAccount),
|
2020-06-19 19:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode results
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, accounts)
|
2020-06-19 19:30:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-09-17 00:45:10 +00:00
|
|
|
|
|
|
|
// query cdps in store and filter by request params
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetCdps(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-09-17 00:45:10 +00:00
|
|
|
var params types.QueryCdpsParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-09-17 00:45:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter CDPs
|
2022-01-08 00:39:27 +00:00
|
|
|
filteredCDPs, err := FilterCDPs(ctx, keeper, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
2020-09-17 00:45:10 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, filteredCDPs)
|
2020-09-17 00:45:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 18:09:59 +00:00
|
|
|
// query total amount of principal (ie. usdx) that has been minted with a particular collateral type
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetTotalPrincipal(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-10-23 18:09:59 +00:00
|
|
|
var params types.QueryGetTotalPrincipalParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2021-10-23 18:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var queryCollateralTypes []string
|
|
|
|
|
|
|
|
if params.CollateralType != "" {
|
|
|
|
// Single collateralType provided
|
|
|
|
queryCollateralTypes = append(queryCollateralTypes, params.CollateralType)
|
|
|
|
} else {
|
|
|
|
// No collateralType provided, respond with all of them
|
|
|
|
keeperParams := keeper.GetParams(ctx)
|
|
|
|
|
|
|
|
for _, collateral := range keeperParams.CollateralParams {
|
|
|
|
queryCollateralTypes = append(queryCollateralTypes, collateral.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var collateralPrincipals types.TotalPrincipals
|
2021-10-23 18:09:59 +00:00
|
|
|
|
|
|
|
for _, queryType := range queryCollateralTypes {
|
|
|
|
// Hardcoded to default USDX
|
|
|
|
principalAmount := keeper.GetTotalPrincipal(ctx, queryType, types.DefaultStableDenom)
|
|
|
|
// Wrap it in an sdk.Coin
|
|
|
|
totalAmountCoin := sdk.NewCoin(types.DefaultStableDenom, principalAmount)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
totalPrincipal := types.NewTotalPrincipal(queryType, totalAmountCoin)
|
2021-10-23 18:09:59 +00:00
|
|
|
collateralPrincipals = append(collateralPrincipals, totalPrincipal)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, collateralPrincipals)
|
2021-10-23 18:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// query total amount of collateral (ie. btcb) that has been deposited with a particular collateral type
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetTotalCollateral(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-10-23 18:09:59 +00:00
|
|
|
var request types.QueryGetTotalCollateralParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &request)
|
2021-10-23 18:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
denomCollateralTypes := make(map[string][]string)
|
|
|
|
|
|
|
|
// collect collateral types for each denom
|
|
|
|
for _, collateralParam := range params.CollateralParams {
|
|
|
|
denomCollateralTypes[collateralParam.Denom] =
|
|
|
|
append(denomCollateralTypes[collateralParam.Denom], collateralParam.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort collateral types alphabetically
|
|
|
|
for _, collateralTypes := range denomCollateralTypes {
|
|
|
|
sort.Slice(collateralTypes, func(i int, j int) bool {
|
|
|
|
return collateralTypes[i] < collateralTypes[j]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// get total collateral in all cdps
|
2022-01-08 00:39:27 +00:00
|
|
|
cdpAccount := keeper.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
|
|
|
|
totalCdpCollateral := keeper.bankKeeper.GetAllBalances(ctx, cdpAccount.GetAddress())
|
2021-10-23 18:09:59 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
var response types.TotalCollaterals
|
2021-10-23 18:09:59 +00:00
|
|
|
|
|
|
|
for denom, collateralTypes := range denomCollateralTypes {
|
|
|
|
// skip any denoms that do not match the requested collateral type
|
|
|
|
if request.CollateralType != "" {
|
|
|
|
match := false
|
|
|
|
for _, ctype := range collateralTypes {
|
|
|
|
if ctype == request.CollateralType {
|
|
|
|
match = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !match {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
totalCollateral := totalCdpCollateral.AmountOf(denom)
|
|
|
|
|
|
|
|
// we need to query individual cdps for denoms with more than one collateral type
|
|
|
|
for i := len(collateralTypes) - 1; i > 0; i-- {
|
|
|
|
cdps := keeper.GetAllCdpsByCollateralType(ctx, collateralTypes[i])
|
|
|
|
|
|
|
|
collateral := sdk.ZeroInt()
|
|
|
|
|
|
|
|
for _, cdp := range cdps {
|
|
|
|
collateral = collateral.Add(cdp.Collateral.Amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
totalCollateral = totalCollateral.Sub(collateral)
|
|
|
|
|
|
|
|
// if we have no collateralType filter, or the filter matches, include it in the response
|
|
|
|
if request.CollateralType == "" || collateralTypes[i] == request.CollateralType {
|
2022-01-08 00:39:27 +00:00
|
|
|
response = append(response, types.NewTotalCollateral(collateralTypes[i], sdk.NewCoin(denom, collateral)))
|
2021-10-23 18:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip the rest of the cdp queries if we have a matching filter
|
|
|
|
if collateralTypes[i] == request.CollateralType {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.CollateralType == "" || collateralTypes[0] == request.CollateralType {
|
|
|
|
// all leftover total collateral belongs to the first collateral type
|
2022-01-08 00:39:27 +00:00
|
|
|
response = append(response, types.NewTotalCollateral(collateralTypes[0], sdk.NewCoin(denom, totalCollateral)))
|
2021-10-23 18:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort to ensure deterministic response
|
|
|
|
sort.Slice(response, func(i int, j int) bool {
|
|
|
|
return response[i].CollateralType < response[j].CollateralType
|
|
|
|
})
|
|
|
|
|
|
|
|
// encode response
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, response)
|
2021-10-23 18:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
// FilterCDPs queries the store for all CDPs that match query params
|
2022-01-08 00:39:27 +00:00
|
|
|
func FilterCDPs(ctx sdk.Context, k Keeper, params types.QueryCdpsParams) (types.AugmentedCDPs, error) {
|
2020-09-17 00:45:10 +00:00
|
|
|
var matchCollateralType, matchOwner, matchID, matchRatio types.CDPs
|
|
|
|
|
|
|
|
// match cdp owner (if supplied)
|
|
|
|
if len(params.Owner) > 0 {
|
|
|
|
denoms := k.GetCollateralTypes(ctx)
|
|
|
|
for _, denom := range denoms {
|
|
|
|
cdp, found := k.GetCdpByOwnerAndCollateralType(ctx, params.Owner, denom)
|
|
|
|
if found {
|
|
|
|
matchOwner = append(matchOwner, cdp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// match cdp collateral denom (if supplied)
|
|
|
|
if len(params.CollateralType) > 0 {
|
|
|
|
// if owner is specified only iterate over already matched cdps for efficiency
|
|
|
|
if len(params.Owner) > 0 {
|
|
|
|
for _, cdp := range matchOwner {
|
|
|
|
if cdp.Type == params.CollateralType {
|
|
|
|
matchCollateralType = append(matchCollateralType, cdp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-08 00:39:27 +00:00
|
|
|
_, found := k.GetCollateral(ctx, params.CollateralType)
|
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("invalid collateral type")
|
|
|
|
}
|
2020-09-17 00:45:10 +00:00
|
|
|
matchCollateralType = k.GetAllCdpsByCollateralType(ctx, params.CollateralType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// match cdp ID (if supplied)
|
|
|
|
if params.ID != 0 {
|
|
|
|
denoms := k.GetCollateralTypes(ctx)
|
|
|
|
for _, denom := range denoms {
|
|
|
|
cdp, found := k.GetCDP(ctx, denom, params.ID)
|
|
|
|
if found {
|
|
|
|
matchID = append(matchID, cdp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// match cdp ratio (if supplied)
|
2022-01-08 00:39:27 +00:00
|
|
|
if !params.Ratio.IsNil() && params.Ratio.GT(sdk.ZeroDec()) {
|
2020-09-17 00:45:10 +00:00
|
|
|
denoms := k.GetCollateralTypes(ctx)
|
|
|
|
for _, denom := range denoms {
|
|
|
|
ratio, err := k.CalculateCollateralizationRatioFromAbsoluteRatio(ctx, denom, params.Ratio, "liquidation")
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cdpsUnderRatio := k.GetAllCdpsByCollateralTypeAndRatio(ctx, denom, ratio)
|
|
|
|
matchRatio = append(matchRatio, cdpsUnderRatio...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var commonCDPs types.CDPs
|
|
|
|
// If no params specified, fetch all CDPs
|
2022-01-08 00:39:27 +00:00
|
|
|
if params.CollateralType == "" && len(params.Owner) == 0 && params.ID == 0 && params.Ratio.Equal(sdk.ZeroDec()) {
|
2020-09-17 00:45:10 +00:00
|
|
|
commonCDPs = k.GetAllCdps(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the intersection of any matched CDPs
|
2022-01-08 00:39:27 +00:00
|
|
|
if params.CollateralType != "" {
|
|
|
|
if len(matchCollateralType) == 0 {
|
|
|
|
return nil, nil
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
commonCDPs = matchCollateralType
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(params.Owner) > 0 {
|
|
|
|
if len(matchCollateralType) > 0 {
|
|
|
|
if len(commonCDPs) > 0 {
|
|
|
|
commonCDPs = FindIntersection(commonCDPs, matchOwner)
|
|
|
|
} else {
|
|
|
|
commonCDPs = matchOwner
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
commonCDPs = matchOwner
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
if params.ID != 0 {
|
2022-01-08 00:39:27 +00:00
|
|
|
if len(matchID) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(commonCDPs) > 0 {
|
|
|
|
commonCDPs = FindIntersection(commonCDPs, matchID)
|
2020-09-17 00:45:10 +00:00
|
|
|
} else {
|
2022-01-08 00:39:27 +00:00
|
|
|
commonCDPs = matchID
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
if !params.Ratio.IsNil() && params.Ratio.GT(sdk.ZeroDec()) {
|
|
|
|
if len(matchRatio) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(commonCDPs) > 0 {
|
|
|
|
commonCDPs = FindIntersection(commonCDPs, matchRatio)
|
2020-09-17 00:45:10 +00:00
|
|
|
} else {
|
2022-01-08 00:39:27 +00:00
|
|
|
commonCDPs = matchRatio
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2020-09-17 00:45:10 +00:00
|
|
|
// Load augmented CDPs
|
|
|
|
var augmentedCDPs types.AugmentedCDPs
|
|
|
|
for _, cdp := range commonCDPs {
|
|
|
|
augmentedCDP := k.LoadAugmentedCDP(ctx, cdp)
|
|
|
|
augmentedCDPs = append(augmentedCDPs, augmentedCDP)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply page and limit params
|
|
|
|
start, end := client.Paginate(len(augmentedCDPs), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
2022-01-08 00:39:27 +00:00
|
|
|
return nil, nil
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
return augmentedCDPs[start:end], nil
|
2020-09-17 00:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindIntersection finds the intersection of two CDP arrays in linear time complexity O(n + n)
|
|
|
|
func FindIntersection(x types.CDPs, y types.CDPs) types.CDPs {
|
|
|
|
cdpSet := make(types.CDPs, 0)
|
|
|
|
cdpMap := make(map[uint64]bool)
|
|
|
|
|
|
|
|
for i := 0; i < len(x); i++ {
|
|
|
|
cdp := x[i]
|
|
|
|
cdpMap[cdp.ID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(y); i++ {
|
|
|
|
cdp := y[i]
|
|
|
|
if _, found := cdpMap[cdp.ID]; found {
|
|
|
|
cdpSet = append(cdpSet, cdp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cdpSet
|
|
|
|
}
|