2019-11-25 19:46:02 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2020-05-24 18:29:48 +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-05-24 18:29:48 +00:00
|
|
|
"github.com/gorilla/mux"
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
)
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// define routes that get registered by the main application
|
|
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
2020-06-19 19:30:10 +00:00
|
|
|
r.HandleFunc("/cdp/accounts", getAccountsHandlerFn(cliCtx)).Methods("GET")
|
2020-02-03 15:41:28 +00:00
|
|
|
r.HandleFunc("/cdp/parameters", getParamsHandlerFn(cliCtx)).Methods("GET")
|
2020-08-21 19:42:46 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/{%s}/{%s}", types.RestOwner, types.RestCollateralType), queryCdpHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/cdp/cdps/collateralType/{%s}", types.RestCollateralType), queryCdpsHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/cdp/cdps/ratio/{%s}/{%s}", types.RestCollateralType, types.RestRatio), queryCdpsByRatioHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/deposits/{%s}/{%s}", types.RestOwner, types.RestCollateralType), queryCdpDepositsHandlerFn(cliCtx)).Methods("GET")
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
func queryCdpHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-02-04 17:56:22 +00:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
ownerBech32 := vars[types.RestOwner]
|
2020-08-21 19:42:46 +00:00
|
|
|
collateralType := vars[types.RestCollateralType]
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
owner, err := sdk.AccAddressFromBech32(ownerBech32)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
params := types.NewQueryCdpParams(owner, collateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
2020-05-24 18:29:48 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
2020-01-12 15:35:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:41:28 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetCdp), bz)
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
func queryCdpsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2019-11-25 19:46:02 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-02-04 17:56:22 +00:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
vars := mux.Vars(r)
|
2020-08-21 19:42:46 +00:00
|
|
|
collateralType := vars[types.RestCollateralType]
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
params := types.NewQueryCdpsParams(collateralType)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
2020-05-24 18:29:48 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
2020-01-12 15:35:34 +00:00
|
|
|
return
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 15:41:28 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetCdps), bz)
|
2020-01-12 15:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
|
|
return
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryCdpsByRatioHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-02-04 17:56:22 +00:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
vars := mux.Vars(r)
|
2020-08-21 19:42:46 +00:00
|
|
|
collateralType := vars[types.RestCollateralType]
|
2020-01-12 15:35:34 +00:00
|
|
|
ratioStr := vars[types.RestRatio]
|
|
|
|
|
|
|
|
ratioDec, sdkError := sdk.NewDecFromStr(ratioStr)
|
|
|
|
if sdkError != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, sdkError.Error())
|
|
|
|
return
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
params := types.NewQueryCdpsByRatioParams(collateralType, ratioDec)
|
2020-01-12 15:35:34 +00:00
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
2020-05-24 18:29:48 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
2019-11-25 19:46:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:41:28 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetCdpsByCollateralization), bz)
|
2019-11-25 19:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 20:08:17 +00:00
|
|
|
func queryCdpDepositsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-02-04 17:56:22 +00:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2020-01-28 20:08:17 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
ownerBech32 := vars[types.RestOwner]
|
2020-08-21 19:42:46 +00:00
|
|
|
collateralType := vars[types.RestCollateralType]
|
2020-01-28 20:08:17 +00:00
|
|
|
|
|
|
|
owner, err := sdk.AccAddressFromBech32(ownerBech32)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
params := types.NewQueryCdpDeposits(owner, collateralType)
|
2020-01-28 20:08:17 +00:00
|
|
|
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if err != nil {
|
2020-05-24 18:29:48 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
|
2020-01-28 20:08:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetCdpDeposits), bz)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
func getParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-02-04 17:56:22 +00:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetParams), nil)
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 18:29:48 +00:00
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 19:30:10 +00:00
|
|
|
|
|
|
|
func getAccountsHandlerFn(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.QueryGetAccounts), nil)
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|