From 69ce2a02980ae092dfe16ac8c34c831b1aeb30dc Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Fri, 13 Aug 2021 02:21:38 +0530 Subject: [PATCH] Refactored swap module CLI and REST --- go.mod | 1 + x/swap/client/cli/query.go | 22 ++++++++++++++------ x/swap/client/cli/tx.go | 7 ++++++- x/swap/client/rest/query.go | 41 ++++++++++++++++++++++++------------- x/swap/client/rest/rest.go | 6 ++++-- 5 files changed, 54 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index a61190c5..c03209c8 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/spf13/cast v1.3.1 github.com/spf13/cobra v1.1.3 + github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.7.0 github.com/tendermint/tendermint v0.34.10 github.com/tendermint/tm-db v0.6.4 diff --git a/x/swap/client/cli/query.go b/x/swap/client/cli/query.go index 4cdffb8e..6a0677fe 100644 --- a/x/swap/client/cli/query.go +++ b/x/swap/client/cli/query.go @@ -31,8 +31,12 @@ func querySwap() *cobra.Command { qc = types.NewQueryServiceClient(ctx) ) - res, err := qc.QuerySwap(context.Background(), - types.NewQuerySwapRequest(types.BytesToHash(txHash))) + res, err := qc.QuerySwap( + context.Background(), + types.NewQuerySwapRequest( + types.BytesToHash(txHash), + ), + ) if err != nil { return err } @@ -65,8 +69,12 @@ func querySwaps() *cobra.Command { qc = types.NewQueryServiceClient(ctx) ) - res, err := qc.QuerySwaps(context.Background(), - types.NewQuerySwapsRequest(pagination)) + res, err := qc.QuerySwaps( + context.Background(), + types.NewQuerySwapsRequest( + pagination, + ), + ) if err != nil { return err } @@ -95,8 +103,10 @@ func queryParams() *cobra.Command { qc = types.NewQueryServiceClient(ctx) ) - res, err := qc.QueryParams(context.Background(), - types.NewQueryParamsRequest()) + res, err := qc.QueryParams( + context.Background(), + types.NewQueryParamsRequest(), + ) if err != nil { return err } diff --git a/x/swap/client/cli/tx.go b/x/swap/client/cli/tx.go index b59895b3..05984847 100644 --- a/x/swap/client/cli/tx.go +++ b/x/swap/client/cli/tx.go @@ -39,7 +39,12 @@ func txSwap() *cobra.Command { return err } - msg := types.NewMsgSwapRequest(ctx.FromAddress, types.BytesToHash(txHash), receiver, sdk.NewInt(amount)) + msg := types.NewMsgSwapRequest( + ctx.FromAddress, + types.BytesToHash(txHash), + receiver, + sdk.NewInt(amount), + ) if err := msg.ValidateBasic(); err != nil { return err } diff --git a/x/swap/client/rest/query.go b/x/swap/client/rest/query.go index 853e042f..b4108dd1 100644 --- a/x/swap/client/rest/query.go +++ b/x/swap/client/rest/query.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/cosmos/cosmos-sdk/client" + sdkquery "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/gorilla/mux" @@ -14,22 +15,23 @@ import ( func querySwap(ctx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) + var ( + qc = types.NewQueryServiceClient(ctx) + vars = mux.Vars(r) + ) txHash, err := hex.DecodeString(vars["txHash"]) - if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + if rest.CheckBadRequestError(w, err) { return } - var ( - qc = types.NewQueryServiceClient(ctx) + res, err := qc.QuerySwap( + context.Background(), + types.NewQuerySwapRequest( + types.BytesToHash(txHash), + ), ) - - res, err := qc.QuerySwap(context.Background(), - types.NewQuerySwapRequest(types.BytesToHash(txHash))) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + if rest.CheckInternalServerError(w, err) { return } @@ -43,10 +45,21 @@ func querySwaps(ctx client.Context) http.HandlerFunc { qc = types.NewQueryServiceClient(ctx) ) - res, err := qc.QuerySwaps(context.Background(), - types.NewQuerySwapsRequest(nil)) - if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + _, page, limit, err := rest.ParseHTTPArgs(r) + if rest.CheckBadRequestError(w, err) { + return + } + + res, err := qc.QuerySwaps( + context.Background(), + types.NewQuerySwapsRequest( + &sdkquery.PageRequest{ + Offset: uint64(page * limit), + Limit: uint64(limit), + }, + ), + ) + if rest.CheckInternalServerError(w, err) { return } diff --git a/x/swap/client/rest/rest.go b/x/swap/client/rest/rest.go index 838d3f95..4d3fb1a8 100644 --- a/x/swap/client/rest/rest.go +++ b/x/swap/client/rest/rest.go @@ -1,15 +1,17 @@ package rest import ( + "net/http" + "github.com/cosmos/cosmos-sdk/client" "github.com/gorilla/mux" ) func registerQueryRoutes(ctx client.Context, router *mux.Router) { router.HandleFunc("/swaps", querySwaps(ctx)). - Methods("GET") + Methods(http.MethodGet) router.HandleFunc("/swaps/{txHash}", querySwap(ctx)). - Methods("GET") + Methods(http.MethodGet) } func RegisterRoutes(ctx client.Context, router *mux.Router) {