Merge PR #532: Fix expiry parsing

This commit is contained in:
Alexander Bezobchuk 2020-05-29 16:07:34 -04:00 committed by GitHub
parent 5302976109
commit 90681abcc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 17 deletions

View File

@ -31,6 +31,7 @@ const (
QueryOracles = types.QueryOracles
QueryRawPrices = types.QueryRawPrices
QueryPrice = types.QueryPrice
MaxExpiry = types.MaxExpiry
)
// nolint

View File

@ -3,6 +3,7 @@ package cli
import (
"bufio"
"fmt"
"strconv"
"time"
"github.com/spf13/cobra"
@ -41,7 +42,7 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
func GetCmdPostPrice(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "postprice [marketID] [price] [expiry]",
Short: "post the latest price for a particular market",
Short: "post the latest price for a particular market with a given expiry as a UNIX time",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
@ -52,17 +53,23 @@ func GetCmdPostPrice(cdc *codec.Codec) *cobra.Command {
if err != nil {
return err
}
expiryInt, ok := sdk.NewIntFromString(args[2])
if !ok {
return fmt.Errorf("invalid expiry - %s", args[2])
expiryInt, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
return fmt.Errorf("invalid expiry %s: %w", args[2], err)
}
expiry := tmtime.Canonical(time.Unix(expiryInt.Int64(), 0))
if expiryInt > types.MaxExpiry {
return fmt.Errorf("invalid expiry; got %d, max: %d", expiryInt, types.MaxExpiry)
}
expiry := tmtime.Canonical(time.Unix(expiryInt, 0))
msg := types.NewMsgPostPrice(cliCtx.GetFromAddress(), args[0], price, expiry)
err = msg.ValidateBasic()
if err != nil {
if err = msg.ValidateBasic(); err != nil {
return err
}
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

View File

@ -3,15 +3,14 @@ package rest
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/gorilla/mux"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/kava-labs/kava/x/pricefeed/types"
@ -48,17 +47,21 @@ func postPriceHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}
expiryInt, ok := sdk.NewIntFromString(req.Expiry)
if !ok {
rest.WriteErrorResponse(w, http.StatusBadRequest, "invalid expiry")
expiryInt, err := strconv.ParseInt(req.Expiry, 10, 64)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid expiry %s: %s", req.Expiry, err))
return
}
expiry := tmtime.Canonical(time.Unix(expiryInt.Int64(), 0))
// create the message
if expiryInt > types.MaxExpiry {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid expiry; got %d, max: %d", expiryInt, types.MaxExpiry))
return
}
expiry := tmtime.Canonical(time.Unix(expiryInt, 0))
msg := types.NewMsgPostPrice(addr, req.MarketID, price, expiry)
err = msg.ValidateBasic()
if err != nil {
if err = msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

View File

@ -13,6 +13,9 @@ import (
const (
// TypeMsgPostPrice type of PostPrice msg
TypeMsgPostPrice = "post_price"
// MaxExpiry defines the max expiry time defined as UNIX time (9999-12-31 23:59:59 +0000 UTC)
MaxExpiry = 253402300799
)
// ensure Msg interface compliance at compile time