feat: add query for previous savings rate distribution time (#679)

This commit is contained in:
Kevin Davis 2020-10-03 19:14:42 -04:00 committed by GitHub
parent 04946493ae
commit 1a8a4b86e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 112 additions and 42 deletions

View File

@ -32,6 +32,7 @@ const (
QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
QueryGetParams = types.QueryGetParams
QueryGetPreviousSavingsDistributionTime = types.QueryGetPreviousSavingsDistributionTime
QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed
RestCollateralType = types.RestCollateralType
RestOwner = types.RestOwner

View File

@ -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)
},
}
}

View File

@ -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)

View File

@ -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

View File

@ -14,6 +14,7 @@ const (
QueryGetParams = "params"
QueryGetAccounts = "accounts"
QueryGetSavingsRateDistributed = "savings-rate-dist"
QueryGetPreviousSavingsDistributionTime = "savings-rate-dist-time"
RestOwner = "owner"
RestCollateralType = "collateral-type"
RestRatio = "ratio"