[R4R] cdp rest fixes (#360)

* fix: use correct path for cdp queries

* fix: avoid params being confused for cdp denom

* fix: use consistent terminology

* fix: struct tag mismatch for repayment

* fix: use plural when querying multiple cdps

* fix: use correct type for query by ratio

* wip: refactor cdp query paths

* wip: routing queries

* fix: route prefixes

* wip: address review comments

* Update x/cdp/client/rest/query.go

Co-Authored-By: Ruaridh <rhuairahrighairidh@users.noreply.github.com>

* use post instead of put

Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
This commit is contained in:
Kevin Davis 2020-02-03 10:41:28 -05:00 committed by GitHub
parent 4599caca07
commit 31e185c632
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 16 deletions

View File

@ -16,11 +16,11 @@ import (
// define routes that get registered by the main application // define routes that get registered by the main application
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/cdp/{%s}/{%s}", types.RestOwner, types.RestCollateralDenom), queryCdpHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/cdp/parameters", getParamsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/{%s}", types.RestCollateralDenom), queryCdpsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/{%s}/{%s}", types.RestOwner, types.RestCollateralDenom), queryCdpHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/{%s}/ratio/{%s}", types.RestCollateralDenom, types.RestRatio), queryCdpsByRatioHandlerFn(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/cdp/cdps/denom/{%s}", types.RestCollateralDenom), queryCdpsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/cdp/deposits/{%s}/{%s}", types.RestOwner, types.RestCollateralDenom), queryCdpDepositsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/cdp/cdps/ratio/{%s}/{%s}", types.RestCollateralDenom, types.RestRatio), queryCdpsByRatioHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/cdp/params", getParamsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/cdp/cdps/cdp/deposits/{%s}/{%s}", types.RestOwner, types.RestCollateralDenom), queryCdpDepositsHandlerFn(cliCtx)).Methods("GET")
} }
func queryCdpHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { func queryCdpHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
@ -43,7 +43,7 @@ func queryCdpHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return return
} }
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s", types.QueryGetCdp), bz) res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetCdp), bz)
if err != nil { if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error()) rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return return
@ -68,7 +68,7 @@ func queryCdpsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return return
} }
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s", types.QueryGetCdps), bz) res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetCdps), bz)
if err != nil { if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error()) rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return return
@ -99,7 +99,7 @@ func queryCdpsByRatioHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return return
} }
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s", types.QueryGetCdps), bz) res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/cdp/%s", types.QueryGetCdpsByCollateralization), bz)
if err != nil { if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error()) rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return return

View File

@ -17,7 +17,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
// PostCdpReq defines the properties of cdp request's body. // PostCdpReq defines the properties of cdp request's body.
type PostCdpReq struct { type PostCdpReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Owner sdk.AccAddress `json:"owner" yaml:"owner"` Sender sdk.AccAddress `json:"sender" yaml:"sender"`
Collateral sdk.Coins `json:"collateral" yaml:"collateral"` Collateral sdk.Coins `json:"collateral" yaml:"collateral"`
Principal sdk.Coins `json:"principal" yaml:"principal"` Principal sdk.Coins `json:"principal" yaml:"principal"`
} }
@ -51,5 +51,5 @@ type PostRepayReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Owner sdk.AccAddress `json:"owner" yaml:"owner"` Owner sdk.AccAddress `json:"owner" yaml:"owner"`
Denom string `json:"denom" yaml:"denom"` Denom string `json:"denom" yaml:"denom"`
Payment sdk.Coins `json:"principal" yaml:"principal"` Payment sdk.Coins `json:"payment" yaml:"payment"`
} }

View File

@ -14,11 +14,11 @@ import (
) )
func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/cdp", postCdpHandlerFn(cliCtx)).Methods("PUT") r.HandleFunc("/cdp", postCdpHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/cdp/{owner}/{denom}/deposits", postDepositHandlerFn(cliCtx)).Methods("PUT") r.HandleFunc("/cdp/{owner}/{denom}/deposits", postDepositHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/cdp/{owner}/{denom}/withdraw", postWithdrawHandlerFn(cliCtx)).Methods("PUT") r.HandleFunc("/cdp/{owner}/{denom}/withdraw", postWithdrawHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/cdp/{owner}/{denom}/draw", postDrawHandlerFn(cliCtx)).Methods("PUT") r.HandleFunc("/cdp/{owner}/{denom}/draw", postDrawHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/cdp/{owner}/{denom}/wipe", postRepayHandlerFn(cliCtx)).Methods("PUT") r.HandleFunc("/cdp/{owner}/{denom}/repay", postRepayHandlerFn(cliCtx)).Methods("POST")
} }
@ -36,7 +36,7 @@ func postCdpHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
// Create and return msg // Create and return msg
msg := types.NewMsgCreateCDP( msg := types.NewMsgCreateCDP(
requestBody.Owner, requestBody.Sender,
requestBody.Collateral, requestBody.Collateral,
requestBody.Principal, requestBody.Principal,
) )