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

@ -8,37 +8,38 @@ import (
) )
const ( const (
BaseDigitFactor = keeper.BaseDigitFactor BaseDigitFactor = keeper.BaseDigitFactor
AttributeKeyCdpID = types.AttributeKeyCdpID AttributeKeyCdpID = types.AttributeKeyCdpID
AttributeKeyDeposit = types.AttributeKeyDeposit AttributeKeyDeposit = types.AttributeKeyDeposit
AttributeKeyError = types.AttributeKeyError AttributeKeyError = types.AttributeKeyError
AttributeValueCategory = types.AttributeValueCategory AttributeValueCategory = types.AttributeValueCategory
DefaultParamspace = types.DefaultParamspace DefaultParamspace = types.DefaultParamspace
EventTypeBeginBlockerFatal = types.EventTypeBeginBlockerFatal EventTypeBeginBlockerFatal = types.EventTypeBeginBlockerFatal
EventTypeCdpClose = types.EventTypeCdpClose EventTypeCdpClose = types.EventTypeCdpClose
EventTypeCdpDeposit = types.EventTypeCdpDeposit EventTypeCdpDeposit = types.EventTypeCdpDeposit
EventTypeCdpDraw = types.EventTypeCdpDraw EventTypeCdpDraw = types.EventTypeCdpDraw
EventTypeCdpLiquidation = types.EventTypeCdpLiquidation EventTypeCdpLiquidation = types.EventTypeCdpLiquidation
EventTypeCdpRepay = types.EventTypeCdpRepay EventTypeCdpRepay = types.EventTypeCdpRepay
EventTypeCdpWithdrawal = types.EventTypeCdpWithdrawal EventTypeCdpWithdrawal = types.EventTypeCdpWithdrawal
EventTypeCreateCdp = types.EventTypeCreateCdp EventTypeCreateCdp = types.EventTypeCreateCdp
LiquidatorMacc = types.LiquidatorMacc LiquidatorMacc = types.LiquidatorMacc
ModuleName = types.ModuleName ModuleName = types.ModuleName
QuerierRoute = types.QuerierRoute QuerierRoute = types.QuerierRoute
QueryGetAccounts = types.QueryGetAccounts QueryGetAccounts = types.QueryGetAccounts
QueryGetCdp = types.QueryGetCdp QueryGetCdp = types.QueryGetCdp
QueryGetCdpDeposits = types.QueryGetCdpDeposits QueryGetCdpDeposits = types.QueryGetCdpDeposits
QueryGetCdps = types.QueryGetCdps QueryGetCdps = types.QueryGetCdps
QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
QueryGetParams = types.QueryGetParams QueryGetParams = types.QueryGetParams
QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed QueryGetPreviousSavingsDistributionTime = types.QueryGetPreviousSavingsDistributionTime
RestCollateralType = types.RestCollateralType QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed
RestOwner = types.RestOwner RestCollateralType = types.RestCollateralType
RestRatio = types.RestRatio RestOwner = types.RestOwner
RouterKey = types.RouterKey RestRatio = types.RestRatio
SavingsRateMacc = types.SavingsRateMacc RouterKey = types.RouterKey
StoreKey = types.StoreKey SavingsRateMacc = types.SavingsRateMacc
StoreKey = types.StoreKey
) )
var ( var (

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -41,6 +42,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
QueryParamsCmd(queryRoute, cdc), QueryParamsCmd(queryRoute, cdc),
QueryGetAccounts(queryRoute, cdc), QueryGetAccounts(queryRoute, cdc),
QueryGetSavingsRateDistributed(queryRoute, cdc), QueryGetSavingsRateDistributed(queryRoute, cdc),
QueryGetSavingsRateDistTime(queryRoute, cdc),
)...) )...)
return cdpQueryCmd 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/accounts", getAccountsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/cdp/parameters", getParamsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/cdp/parameters", getParamsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/cdp/savingsRateDist", getSavingsRateDistributedHandler(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/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"), queryCdpsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/cdps/collateralType/{%s}", types.RestCollateralType), queryCdpsByCollateralTypeHandlerFn(cliCtx)).Methods("GET") // legacy 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 { func queryCdpsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0) _, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)

View File

@ -1,6 +1,8 @@
package keeper package keeper
import ( import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
@ -32,6 +34,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
return queryGetAccounts(ctx, req, keeper) return queryGetAccounts(ctx, req, keeper)
case types.QueryGetSavingsRateDistributed: case types.QueryGetSavingsRateDistributed:
return queryGetSavingsRateDistributed(ctx, req, keeper) return queryGetSavingsRateDistributed(ctx, req, keeper)
case types.QueryGetPreviousSavingsDistributionTime:
return queryGetPreviousSavingsDistributionTime(ctx, req, keeper)
default: default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint %s", types.ModuleName, path[0]) 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 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 // query cdps in store and filter by request params
func queryGetCdps(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) { func queryGetCdps(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
var params types.QueryCdpsParams var params types.QueryCdpsParams

View File

@ -6,17 +6,18 @@ import (
// Querier routes for the cdp module // Querier routes for the cdp module
const ( const (
QueryGetCdp = "cdp" QueryGetCdp = "cdp"
QueryGetCdps = "cdps" QueryGetCdps = "cdps"
QueryGetCdpDeposits = "deposits" QueryGetCdpDeposits = "deposits"
QueryGetCdpsByCollateralization = "ratio" // legacy query, maintained for REST API QueryGetCdpsByCollateralization = "ratio" // legacy query, maintained for REST API
QueryGetCdpsByCollateralType = "collateralType" // legacy query, maintained for REST API QueryGetCdpsByCollateralType = "collateralType" // legacy query, maintained for REST API
QueryGetParams = "params" QueryGetParams = "params"
QueryGetAccounts = "accounts" QueryGetAccounts = "accounts"
QueryGetSavingsRateDistributed = "savings-rate-dist" QueryGetSavingsRateDistributed = "savings-rate-dist"
RestOwner = "owner" QueryGetPreviousSavingsDistributionTime = "savings-rate-dist-time"
RestCollateralType = "collateral-type" RestOwner = "owner"
RestRatio = "ratio" RestCollateralType = "collateral-type"
RestRatio = "ratio"
) )
// QueryCdpParams params for query /cdp/cdp // QueryCdpParams params for query /cdp/cdp