Skip to content

Commit

Permalink
Refactored swap module CLI and REST
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Aug 12, 2021
1 parent d77da0a commit 69ce2a0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions x/swap/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion x/swap/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
41 changes: 27 additions & 14 deletions x/swap/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions x/swap/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 69ce2a0

Please sign in to comment.