From 1a8a4b86e733b1e47baa7b2ccb994940b5d20784 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Sat, 3 Oct 2020 19:14:42 -0400 Subject: [PATCH] feat: add query for previous savings rate distribution time (#679) --- x/cdp/alias.go | 63 +++++++++++++++++++------------------- x/cdp/client/cli/query.go | 28 +++++++++++++++++ x/cdp/client/rest/query.go | 19 ++++++++++++ x/cdp/keeper/querier.go | 21 +++++++++++++ x/cdp/types/querier.go | 23 +++++++------- 5 files changed, 112 insertions(+), 42 deletions(-) diff --git a/x/cdp/alias.go b/x/cdp/alias.go index 034aa448..6c3bbce3 100644 --- a/x/cdp/alias.go +++ b/x/cdp/alias.go @@ -8,37 +8,38 @@ import ( ) const ( - BaseDigitFactor = keeper.BaseDigitFactor - AttributeKeyCdpID = types.AttributeKeyCdpID - AttributeKeyDeposit = types.AttributeKeyDeposit - AttributeKeyError = types.AttributeKeyError - AttributeValueCategory = types.AttributeValueCategory - DefaultParamspace = types.DefaultParamspace - EventTypeBeginBlockerFatal = types.EventTypeBeginBlockerFatal - EventTypeCdpClose = types.EventTypeCdpClose - EventTypeCdpDeposit = types.EventTypeCdpDeposit - EventTypeCdpDraw = types.EventTypeCdpDraw - EventTypeCdpLiquidation = types.EventTypeCdpLiquidation - EventTypeCdpRepay = types.EventTypeCdpRepay - EventTypeCdpWithdrawal = types.EventTypeCdpWithdrawal - EventTypeCreateCdp = types.EventTypeCreateCdp - LiquidatorMacc = types.LiquidatorMacc - ModuleName = types.ModuleName - QuerierRoute = types.QuerierRoute - QueryGetAccounts = types.QueryGetAccounts - QueryGetCdp = types.QueryGetCdp - QueryGetCdpDeposits = types.QueryGetCdpDeposits - QueryGetCdps = types.QueryGetCdps - QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType - QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization - QueryGetParams = types.QueryGetParams - QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed - RestCollateralType = types.RestCollateralType - RestOwner = types.RestOwner - RestRatio = types.RestRatio - RouterKey = types.RouterKey - SavingsRateMacc = types.SavingsRateMacc - StoreKey = types.StoreKey + BaseDigitFactor = keeper.BaseDigitFactor + AttributeKeyCdpID = types.AttributeKeyCdpID + AttributeKeyDeposit = types.AttributeKeyDeposit + AttributeKeyError = types.AttributeKeyError + AttributeValueCategory = types.AttributeValueCategory + DefaultParamspace = types.DefaultParamspace + EventTypeBeginBlockerFatal = types.EventTypeBeginBlockerFatal + EventTypeCdpClose = types.EventTypeCdpClose + EventTypeCdpDeposit = types.EventTypeCdpDeposit + EventTypeCdpDraw = types.EventTypeCdpDraw + EventTypeCdpLiquidation = types.EventTypeCdpLiquidation + EventTypeCdpRepay = types.EventTypeCdpRepay + EventTypeCdpWithdrawal = types.EventTypeCdpWithdrawal + EventTypeCreateCdp = types.EventTypeCreateCdp + LiquidatorMacc = types.LiquidatorMacc + ModuleName = types.ModuleName + QuerierRoute = types.QuerierRoute + QueryGetAccounts = types.QueryGetAccounts + QueryGetCdp = types.QueryGetCdp + QueryGetCdpDeposits = types.QueryGetCdpDeposits + QueryGetCdps = types.QueryGetCdps + QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType + QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization + QueryGetParams = types.QueryGetParams + QueryGetPreviousSavingsDistributionTime = types.QueryGetPreviousSavingsDistributionTime + QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed + RestCollateralType = types.RestCollateralType + RestOwner = types.RestOwner + RestRatio = types.RestRatio + RouterKey = types.RouterKey + SavingsRateMacc = types.SavingsRateMacc + StoreKey = types.StoreKey ) var ( diff --git a/x/cdp/client/cli/query.go b/x/cdp/client/cli/query.go index 1cc68b18..7ec5a41e 100644 --- a/x/cdp/client/cli/query.go +++ b/x/cdp/client/cli/query.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -41,6 +42,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { QueryParamsCmd(queryRoute, cdc), QueryGetAccounts(queryRoute, cdc), QueryGetSavingsRateDistributed(queryRoute, cdc), + QueryGetSavingsRateDistTime(queryRoute, cdc), )...) return cdpQueryCmd @@ -309,3 +311,29 @@ func QueryGetSavingsRateDistributed(queryRoute string, cdc *codec.Codec) *cobra. }, } } + +// QueryGetSavingsRateDistributed queries the total amount of savings rate distributed in USDX +func QueryGetSavingsRateDistTime(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "savings-rate-dist-time", + Short: "get the previous savings rate distribution time", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + + // Query + res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetPreviousSavingsDistributionTime), nil) + if err != nil { + return err + } + cliCtx = cliCtx.WithHeight(height) + + // Decode and print results + var out time.Time + if err := cdc.UnmarshalJSON(res, &out); err != nil { + return fmt.Errorf("failed to unmarshal time.Time: %w", err) + } + return cliCtx.PrintOutput(out) + }, + } +} diff --git a/x/cdp/client/rest/query.go b/x/cdp/client/rest/query.go index 7774d8eb..fe4645c4 100644 --- a/x/cdp/client/rest/query.go +++ b/x/cdp/client/rest/query.go @@ -19,6 +19,7 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc("/cdp/accounts", getAccountsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/cdp/parameters", getParamsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/cdp/savingsRateDist", getSavingsRateDistributedHandler(cliCtx)).Methods("GET") + r.HandleFunc("/cdp/savingsRateDistTime", getSavingsRateDistTimeHandler(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/{%s}/{%s}", types.RestOwner, types.RestCollateralType), queryCdpHandlerFn(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/cdp/cdps"), queryCdpsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/cdp/cdps/collateralType/{%s}", types.RestCollateralType), queryCdpsByCollateralTypeHandlerFn(cliCtx)).Methods("GET") // legacy @@ -216,6 +217,24 @@ func getSavingsRateDistributedHandler(cliCtx context.CLIContext) http.HandlerFun } } +func getSavingsRateDistTimeHandler(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetPreviousSavingsDistributionTime), nil) + cliCtx = cliCtx.WithHeight(height) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + rest.PostProcessResponse(w, cliCtx, res) + } +} + func queryCdpsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) diff --git a/x/cdp/keeper/querier.go b/x/cdp/keeper/querier.go index edb870c2..09f9b7aa 100644 --- a/x/cdp/keeper/querier.go +++ b/x/cdp/keeper/querier.go @@ -1,6 +1,8 @@ package keeper import ( + "fmt" + abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/client" @@ -32,6 +34,8 @@ func NewQuerier(keeper Keeper) sdk.Querier { return queryGetAccounts(ctx, req, keeper) case types.QueryGetSavingsRateDistributed: return queryGetSavingsRateDistributed(ctx, req, keeper) + case types.QueryGetPreviousSavingsDistributionTime: + return queryGetPreviousSavingsDistributionTime(ctx, req, keeper) default: return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint %s", types.ModuleName, path[0]) } @@ -197,6 +201,23 @@ func queryGetSavingsRateDistributed(ctx sdk.Context, req abci.RequestQuery, keep return bz, nil } +// query get savings rate distributed in the cdp store +func queryGetPreviousSavingsDistributionTime(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) { + // Get savings rate distributed + savingsRateDistTime, found := keeper.GetPreviousSavingsDistribution(ctx) + + if !found { + return nil, fmt.Errorf("previous distribution time not found") + } + + // Encode results + bz, err := codec.MarshalJSONIndent(types.ModuleCdc, savingsRateDistTime) + if err != nil { + return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) + } + return bz, nil +} + // query cdps in store and filter by request params func queryGetCdps(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) { var params types.QueryCdpsParams diff --git a/x/cdp/types/querier.go b/x/cdp/types/querier.go index 4785d84f..704c6785 100644 --- a/x/cdp/types/querier.go +++ b/x/cdp/types/querier.go @@ -6,17 +6,18 @@ import ( // Querier routes for the cdp module const ( - QueryGetCdp = "cdp" - QueryGetCdps = "cdps" - QueryGetCdpDeposits = "deposits" - QueryGetCdpsByCollateralization = "ratio" // legacy query, maintained for REST API - QueryGetCdpsByCollateralType = "collateralType" // legacy query, maintained for REST API - QueryGetParams = "params" - QueryGetAccounts = "accounts" - QueryGetSavingsRateDistributed = "savings-rate-dist" - RestOwner = "owner" - RestCollateralType = "collateral-type" - RestRatio = "ratio" + QueryGetCdp = "cdp" + QueryGetCdps = "cdps" + QueryGetCdpDeposits = "deposits" + QueryGetCdpsByCollateralization = "ratio" // legacy query, maintained for REST API + QueryGetCdpsByCollateralType = "collateralType" // legacy query, maintained for REST API + QueryGetParams = "params" + QueryGetAccounts = "accounts" + QueryGetSavingsRateDistributed = "savings-rate-dist" + QueryGetPreviousSavingsDistributionTime = "savings-rate-dist-time" + RestOwner = "owner" + RestCollateralType = "collateral-type" + RestRatio = "ratio" ) // QueryCdpParams params for query /cdp/cdp