mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 08:15:19 +00:00
feat: add query for previous savings rate distribution time (#679)
This commit is contained in:
parent
04946493ae
commit
1a8a4b86e7
@ -32,6 +32,7 @@ const (
|
|||||||
QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType
|
QueryGetCdpsByCollateralType = types.QueryGetCdpsByCollateralType
|
||||||
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
|
QueryGetCdpsByCollateralization = types.QueryGetCdpsByCollateralization
|
||||||
QueryGetParams = types.QueryGetParams
|
QueryGetParams = types.QueryGetParams
|
||||||
|
QueryGetPreviousSavingsDistributionTime = types.QueryGetPreviousSavingsDistributionTime
|
||||||
QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed
|
QueryGetSavingsRateDistributed = types.QueryGetSavingsRateDistributed
|
||||||
RestCollateralType = types.RestCollateralType
|
RestCollateralType = types.RestCollateralType
|
||||||
RestOwner = types.RestOwner
|
RestOwner = types.RestOwner
|
||||||
|
@ -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)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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)
|
||||||
|
@ -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
|
||||||
|
@ -14,6 +14,7 @@ const (
|
|||||||
QueryGetParams = "params"
|
QueryGetParams = "params"
|
||||||
QueryGetAccounts = "accounts"
|
QueryGetAccounts = "accounts"
|
||||||
QueryGetSavingsRateDistributed = "savings-rate-dist"
|
QueryGetSavingsRateDistributed = "savings-rate-dist"
|
||||||
|
QueryGetPreviousSavingsDistributionTime = "savings-rate-dist-time"
|
||||||
RestOwner = "owner"
|
RestOwner = "owner"
|
||||||
RestCollateralType = "collateral-type"
|
RestCollateralType = "collateral-type"
|
||||||
RestRatio = "ratio"
|
RestRatio = "ratio"
|
||||||
|
Loading…
Reference in New Issue
Block a user