diff --git a/x/bep3/client/cli/query.go b/x/bep3/client/cli/query.go index 76a1d66f..5e4d9846 100644 --- a/x/bep3/client/cli/query.go +++ b/x/bep3/client/cli/query.go @@ -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) }, } } @@ -286,7 +286,7 @@ $ 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 { @@ -294,7 +294,7 @@ $ kvcli q bep3 swaps --page=2 --limit=100 } cliCtx = cliCtx.WithHeight(height) - return cliCtx.PrintOutput(matchingAtomicSwaps.String()) // nolint:errcheck + return cliCtx.PrintOutput(matchingAtomicSwaps) // nolint:errcheck }, } diff --git a/x/bep3/client/rest/query.go b/x/bep3/client/rest/query.go index c3ccc0a1..e4aa7f77 100644 --- a/x/bep3/client/rest/query.go +++ b/x/bep3/client/rest/query.go @@ -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()) diff --git a/x/bep3/keeper/querier.go b/x/bep3/keeper/querier.go index da1b0a25..c4ca9e46 100644 --- a/x/bep3/keeper/querier.go +++ b/x/bep3/keeper/querier.go @@ -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()) } @@ -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()) } diff --git a/x/bep3/keeper/querier_test.go b/x/bep3/keeper/querier_test.go index 4f449c37..5143f694 100644 --- a/x/bep3/keeper/querier_test.go +++ b/x/bep3/keeper/querier_test.go @@ -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() { @@ -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]) } } diff --git a/x/bep3/types/swap.go b/x/bep3/types/swap.go index 98ab3c8c..43e1fc68 100644 --- a/x/bep3/types/swap.go +++ b/x/bep3/types/swap.go @@ -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