2019-11-25 19:46:02 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-05-25 02:27:11 +00:00
|
|
|
"strings"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2020-05-25 02:27:11 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
|
2020-01-16 17:52:29 +00:00
|
|
|
"github.com/kava-labs/kava/x/auction/types"
|
|
|
|
)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-02-03 15:54:00 +00:00
|
|
|
const restAuctionID = "auction-id"
|
2020-01-21 17:41:37 +00:00
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
2020-02-04 17:56:10 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/auctions", types.ModuleName), queryAuctionsHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/auctions/{%s}", types.ModuleName, restAuctionID), queryAuctionHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/parameters", types.ModuleName), getParamsHandlerFn(cliCtx)).Methods("GET")
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 17:41:37 +00:00
|
|
|
func queryAuctionHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2019-11-25 19:46:02 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-01-21 17:41:37 +00:00
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
vars := mux.Vars(r)
|
2020-02-03 15:54:00 +00:00
|
|
|
if len(vars[restAuctionID]) == 0 {
|
|
|
|
err := fmt.Errorf("%s required but not specified", restAuctionID)
|
2020-01-21 17:41:37 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-02-03 15:54:00 +00:00
|
|
|
auctionID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[restAuctionID])
|
2020-01-21 17:41:37 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(types.QueryAuctionParams{AuctionID: auctionID})
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
2020-02-27 17:59:11 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetAuction), bz)
|
2020-01-21 17:41:37 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-02-03 15:54:00 +00:00
|
|
|
|
|
|
|
// Decode and return results
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
|
2020-01-29 14:42:03 +00:00
|
|
|
var auction types.Auction
|
2020-02-01 15:49:36 +00:00
|
|
|
err = cliCtx.Codec.UnmarshalJSON(res, &auction)
|
2020-01-29 14:42:03 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
auctionWithPhase := types.NewAuctionWithPhase(auction)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, cliCtx.Codec.MustMarshalJSON(auctionWithPhase))
|
2020-01-21 17:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryAuctionsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-05-25 02:27:11 +00:00
|
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:41:37 +00:00
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-02-03 15:54:00 +00:00
|
|
|
|
2020-05-25 02:27:11 +00:00
|
|
|
var auctionType string
|
|
|
|
var auctionDenom string
|
|
|
|
var auctionPhase string
|
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestType); len(x) != 0 {
|
|
|
|
auctionType = strings.ToLower(strings.TrimSpace(x))
|
|
|
|
if auctionType != types.CollateralAuctionType &&
|
|
|
|
auctionType != types.SurplusAuctionType &&
|
|
|
|
auctionType != types.DebtAuctionType {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid auction type %s", x))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestDenom); len(x) != 0 {
|
|
|
|
auctionDenom = strings.TrimSpace(x)
|
|
|
|
err := sdk.ValidateDenom(auctionDenom)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if x := r.URL.Query().Get(RestPhase); len(x) != 0 {
|
|
|
|
auctionPhase = strings.ToLower(strings.TrimSpace(x))
|
|
|
|
if auctionType != types.CollateralAuctionType && len(auctionType) > 0 {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, "cannot apply phase flag to non-collateral auction type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if auctionPhase != types.ForwardAuctionPhase && auctionPhase != types.ReverseAuctionPhase {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("invalid auction phase %s", x))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params := types.NewQueryAllAuctionParams(page, limit, auctionType, auctionDenom, auctionPhase)
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetAuctions)
|
|
|
|
res, height, err := cliCtx.QueryWithData(route, bz)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
2020-05-25 02:27:11 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2019-11-25 19:46:02 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-03 15:54:00 +00:00
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
2020-01-29 14:42:03 +00:00
|
|
|
|
2020-05-25 02:27:11 +00:00
|
|
|
// Unmarshal to Auction and remarshal as AuctionWithPhase
|
2020-01-29 14:42:03 +00:00
|
|
|
var auctions types.Auctions
|
2020-02-01 15:49:36 +00:00
|
|
|
err = cliCtx.Codec.UnmarshalJSON(res, &auctions)
|
2020-01-29 14:42:03 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:54:00 +00:00
|
|
|
auctionsWithPhase := []types.AuctionWithPhase{} // using empty slice so json returns [] instead of null when there's no auctions
|
2020-01-29 14:42:03 +00:00
|
|
|
for _, a := range auctions {
|
|
|
|
auctionsWithPhase = append(auctionsWithPhase, types.NewAuctionWithPhase(a))
|
|
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, cliCtx.Codec.MustMarshalJSON(auctionsWithPhase))
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 17:52:29 +00:00
|
|
|
|
|
|
|
func getParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-01-21 17:41:37 +00:00
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-01-16 17:52:29 +00:00
|
|
|
// Get the params
|
2020-02-01 15:49:36 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetParams), nil)
|
2020-01-16 17:52:29 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-02-03 15:54:00 +00:00
|
|
|
// Decode and return results
|
2020-01-21 17:41:37 +00:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
2020-01-16 17:52:29 +00:00
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|