Skip to content

Commit

Permalink
Update BEB3 CLI Client to Support JSON (#597)
Browse files Browse the repository at this point in the history
* add initial augmented swaps to bring cli & rest api's under same schema

* use an array type

* update to not cause breaking changes in REST API -- don't embed swap
fields
  • Loading branch information
nddeluca authored Jun 22, 2020
1 parent e81987c commit 2ea7545
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
8 changes: 4 additions & 4 deletions x/bep3/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ func QueryGetAtomicSwapCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return err
}

var atomicSwap types.AtomicSwap
var atomicSwap types.AugmentedAtomicSwap
cdc.MustUnmarshalJSON(res, &atomicSwap)

cliCtx = cliCtx.WithHeight(height)
return cliCtx.PrintOutput(atomicSwap.String())
return cliCtx.PrintOutput(atomicSwap)
},
}
}
Expand Down Expand Up @@ -286,15 +286,15 @@ $ kvcli q bep3 swaps --page=2 --limit=100
return err
}

var matchingAtomicSwaps types.AtomicSwaps
var matchingAtomicSwaps types.AugmentedAtomicSwaps
cdc.UnmarshalJSON(res, &matchingAtomicSwaps)

if len(matchingAtomicSwaps) == 0 {
return fmt.Errorf("No matching atomic swaps found")
}

cliCtx = cliCtx.WithHeight(height)
return cliCtx.PrintOutput(matchingAtomicSwaps.String()) // nolint:errcheck
return cliCtx.PrintOutput(matchingAtomicSwaps) // nolint:errcheck
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/bep3/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func queryAtomicSwapHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
// Decode and return results
cliCtx = cliCtx.WithHeight(height)

var swap types.AtomicSwap
var swap types.AugmentedAtomicSwap
err = cliCtx.Codec.UnmarshalJSON(res, &swap)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
Expand Down
12 changes: 10 additions & 2 deletions x/bep3/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ func queryAtomicSwap(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]b
return nil, sdkerrors.Wrapf(types.ErrAtomicSwapNotFound, "%d", requestParams.SwapID)
}

augmentedAtomicSwap := types.NewAugmentedAtomicSwap(atomicSwap)

// Encode results
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, atomicSwap)
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, augmentedAtomicSwap)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
Expand All @@ -103,7 +105,13 @@ func queryAtomicSwaps(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]
swaps = types.AtomicSwaps{}
}

bz, err := codec.MarshalJSONIndent(types.ModuleCdc, swaps)
augmentedSwaps := types.AugmentedAtomicSwaps{}

for _, swap := range swaps {
augmentedSwaps = append(augmentedSwaps, types.NewAugmentedAtomicSwap(swap))
}

bz, err := codec.MarshalJSONIndent(types.ModuleCdc, augmentedSwaps)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
Expand Down
8 changes: 4 additions & 4 deletions x/bep3/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ func (suite *QuerierTestSuite) TestQueryAtomicSwap() {
suite.NotNil(bz)

// Unmarshal the bytes into type atomic swap
var swap types.AtomicSwap
var swap types.AugmentedAtomicSwap
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &swap))

// Check the returned atomic swap's ID
suite.True(suite.isSwapID[hex.EncodeToString(swap.GetSwapID())])
suite.True(suite.isSwapID[swap.ID])
}

func (suite *QuerierTestSuite) TestQueryAssetSupplies() {
Expand Down Expand Up @@ -161,12 +161,12 @@ func (suite *QuerierTestSuite) TestQueryAtomicSwaps() {
suite.Nil(err)
suite.NotNil(bz)

var swaps types.AtomicSwaps
var swaps types.AugmentedAtomicSwaps
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &swaps))

suite.Equal(len(suite.swapIDs), len(swaps))
for _, swap := range swaps {
suite.True(suite.isSwapID[hex.EncodeToString(swap.GetSwapID())])
suite.True(suite.isSwapID[swap.ID])
}
}

Expand Down
39 changes: 39 additions & 0 deletions x/bep3/types/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,42 @@ func (direction SwapDirection) IsValid() bool {
}
return false
}

type AugmentedAtomicSwap struct {
ID string `json:"id" yaml:"id"`

// Embed AtomicSwap fields explicity in order to output as top level JSON fields
// This prevents breaking changes for clients using REST API
Amount sdk.Coins `json:"amount" yaml:"amount"`
RandomNumberHash tmbytes.HexBytes `json:"random_number_hash" yaml:"random_number_hash"`
ExpireHeight uint64 `json:"expire_height" yaml:"expire_height"`
Timestamp int64 `json:"timestamp" yaml:"timestamp"`
Sender sdk.AccAddress `json:"sender" yaml:"sender"`
Recipient sdk.AccAddress `json:"recipient" yaml:"recipient"`
SenderOtherChain string `json:"sender_other_chain" yaml:"sender_other_chain"`
RecipientOtherChain string `json:"recipient_other_chain" yaml:"recipient_other_chain"`
ClosedBlock int64 `json:"closed_block" yaml:"closed_block"`
Status SwapStatus `json:"status" yaml:"status"`
CrossChain bool `json:"cross_chain" yaml:"cross_chain"`
Direction SwapDirection `json:"direction" yaml:"direction"`
}

func NewAugmentedAtomicSwap(swap AtomicSwap) AugmentedAtomicSwap {
return AugmentedAtomicSwap{
ID: hex.EncodeToString(swap.GetSwapID()),
Amount: swap.Amount,
RandomNumberHash: swap.RandomNumberHash,
ExpireHeight: swap.ExpireHeight,
Timestamp: swap.Timestamp,
Sender: swap.Sender,
Recipient: swap.Recipient,
SenderOtherChain: swap.SenderOtherChain,
RecipientOtherChain: swap.RecipientOtherChain,
ClosedBlock: swap.ClosedBlock,
Status: swap.Status,
CrossChain: swap.CrossChain,
Direction: swap.Direction,
}
}

type AugmentedAtomicSwaps []AugmentedAtomicSwap

0 comments on commit 2ea7545

Please sign in to comment.