mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-28 09:15:24 +00:00
710a29980f
* fix: remove redundant debt limit param * wip: test pricefeed genesis * fix: pricefeed querier * fix: comments, naming * fix: query path * fix: store methods * fix: query methods * add params query to pricefeed module * minor formatting updates, removed commented out legacy code, etc. Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
package rest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
|
)
|
|
|
|
// define routes that get registered by the main application
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
|
r.HandleFunc(fmt.Sprintf("/%s/rawprices/{%s}", types.ModuleName, restName), queryRawPricesHandler(cliCtx)).Methods("GET")
|
|
r.HandleFunc(fmt.Sprintf("/%s/currentprice/{%s}", types.ModuleName, restName), queryCurrentPriceHandler(cliCtx)).Methods("GET")
|
|
r.HandleFunc(fmt.Sprintf("/%s/markets", types.ModuleName), queryMarketsHandler(cliCtx)).Methods("GET")
|
|
r.HandleFunc(fmt.Sprintf("/%s/params", types.ModuleName), queryParamsHandlerFn(cliCtx)).Methods("GET")
|
|
}
|
|
|
|
func queryRawPricesHandler(cliCtx context.CLIContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
paramType := vars[restName]
|
|
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/rawprices/%s", paramType), nil)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
}
|
|
}
|
|
|
|
func queryCurrentPriceHandler(cliCtx context.CLIContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
paramType := vars[restName]
|
|
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/price/%s", paramType), nil)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
}
|
|
}
|
|
|
|
func queryMarketsHandler(cliCtx context.CLIContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/markets/"), nil)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
}
|
|
}
|
|
|
|
func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Get the params
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/pricefeed/%s", types.QueryGetParams), nil)
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
// Return the params
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
}
|
|
}
|