Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use ctx.Codec when marshaling for generated batch json in CLI #514

Merged
merged 6 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions x/ecocredit/client/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func (s *IntegrationTestSuite) TestTxCreateBatch() {
s.commonTxFlags()...,
),
expectErr: true,
expectedErrMsg: "Invalid retirement location",
expectedErrMsg: "Invalid location",
},
{
name: "invalid issuance recipient",
Expand Down Expand Up @@ -597,7 +597,7 @@ func (s *IntegrationTestSuite) TestTxCreateBatch() {
s.commonTxFlags()...,
),
expectErr: true,
expectedErrMsg: "Invalid retirement location: abcde",
expectedErrMsg: "Invalid location: abcde",
},
{
name: "missing from flag",
Expand Down Expand Up @@ -819,7 +819,7 @@ func (s *IntegrationTestSuite) TestTxSend() {
s.commonTxFlags()...,
),
expectErr: true,
expectedErrMsg: "Invalid retirement location: abcde",
expectedErrMsg: "Invalid location: abcde",
},
{
name: "missing from flag",
Expand Down Expand Up @@ -958,7 +958,7 @@ func (s *IntegrationTestSuite) TestTxRetire() {
s.commonTxFlags()...,
),
expectErr: true,
expectedErrMsg: "Invalid retirement location: abcde",
expectedErrMsg: "Invalid location: abcde",
},
{
name: "missing from flag",
Expand Down
58 changes: 25 additions & 33 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package client

import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
"time"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -193,13 +193,17 @@ Required Flags:
ProjectLocation: projectLocation,
}


// Marshal and output JSON of message
msgJson, err := json.MarshalIndent(msg, "", " ")
ctx := sdkclient.GetClientContextFromCmd(cmd)
msgJson, err := ctx.Codec.MarshalJSON(msg)
if err != nil {
return err
}

fmt.Print(string(msgJson))
var formattedJson bytes.Buffer
json.Indent(&formattedJson, msgJson, "", " ")
fmt.Println(formattedJson.String())

return nil
},
Expand All @@ -219,43 +223,31 @@ Required Flags:
}

func TxCreateBatchCmd() *cobra.Command {
var (
startDate = time.Unix(10000, 10000).UTC()
endDate = time.Unix(10000, 10050).UTC()
)
createExampleBatchJSON, err := json.MarshalIndent(
ecocredit.MsgCreateBatch{
// Leave issuer empty, because we'll use --from flag
Issuer: "",
ClassId: "1BX53GF",
Issuance: []*ecocredit.MsgCreateBatch_BatchIssuance{
{
Recipient: "regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw",
TradableAmount: "1000",
RetiredAmount: "15",
RetirementLocation: "ST-UVW XY Z12",
},
},
Metadata: []byte{0x1, 0x2},
StartDate: &startDate,
EndDate: &endDate,
ProjectLocation: "AB-CDE FG1 345",
},
" ",
" ",
)
if err != nil {
panic("Couldn't marshal MsgCreateBatch to JSON")
}

return txflags(&cobra.Command{
Use: "create-batch [msg-create-batch-json-file]",
Short: "Issues a new credit batch",
Long: fmt.Sprintf(`Issues a new credit batch.

Parameters:
msg-create-batch-json-file: Path to a file containing a JSON object
representing MsgCreateBatch. The JSON has format:
%s`, createExampleBatchJSON),
representing MsgCreateBatch. The JSON has format:
{
"class_id"": "C01",
"issuance": [
{
"recipient": "regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw",
"tradable_amount": "1000",
"retired_amount": "15",
"retirement_location": "ST-UVW XY Z12",
},
],
"metadata": "AQI=",
"start_date": "1990-01-01",
"end_date": "1995-10-31",
"project_location": "AB-CDE FG1 345",
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Long help text argument doesn't have access to the *cobra.Command, so its not possible to retrieve the sdkcontext and the JSON marshaler... So I hardcoded an example JSON help text here.

`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := sdkclient.GetClientTxContext(cmd)
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var reLocation = regexp.MustCompile(`^([A-Z]{2})(?:-([A-Z0-9]{1,3})(?: ([a-zA-Z0
func validateLocation(location string) error {
matches := reLocation.FindStringSubmatch(location)
if matches == nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Invalid retirement location: %s.\nLocation should have format <country-code>[-<region-code>[ <postal-code>]].\n", location)
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Invalid location: %s.\nLocation should have format <country-code>[-<region-code>[ <postal-code>]].\n", location)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error gets thrown sometimes for an invalid project_location, so I updated this to make it generic to project and/or retirement location.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a few test cases need to be updated for this

}

return nil
Expand Down