2019-11-25 19:46:02 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"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-01-21 17:41:37 +00:00
|
|
|
const RestAuctionID = "auction-id"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
2020-01-21 17:41:37 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/auction/auctions/{%s}", RestAuctionID), queryAuctionHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc("/auction/auctions", queryAuctionsHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc("/auction/parameters", 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)
|
|
|
|
if len(vars[RestAuctionID]) == 0 {
|
|
|
|
err := fmt.Errorf("%s required but not specified", RestAuctionID)
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
auctionID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[RestAuctionID])
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(types.QueryAuctionParams{AuctionID: auctionID})
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
|
|
|
res, height, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and return results
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryAuctionsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Get all auctions
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("/custom/auction/%s", types.QueryGetAuctions), nil)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-01-21 17:41:37 +00:00
|
|
|
// Return auctions
|
2019-11-25 19:46:02 +00:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/auction/%s", types.QueryGetParams), nil)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Return the params
|
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)
|
|
|
|
}
|
|
|
|
}
|