mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
Update BEB3 CLI Client to Support JSON (#597)
* 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
This commit is contained in:
parent
e81987c31f
commit
2ea75458ff
@ -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
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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())
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user