From 654d35fb3cad4b817e848ea2149285dc6557dee5 Mon Sep 17 00:00:00 2001 From: Ru Horlick Date: Fri, 27 Aug 2021 13:23:12 +0100 Subject: [PATCH] Take credit class admin from --from flag --- x/ecocredit/client/testsuite/tx.go | 44 ++++++------------------------ x/ecocredit/client/tx.go | 33 ++++++++++++++-------- x/ecocredit/params.go | 4 +-- x/ecocredit/params_test.go | 6 ++-- 4 files changed, 35 insertions(+), 52 deletions(-) diff --git a/x/ecocredit/client/testsuite/tx.go b/x/ecocredit/client/testsuite/tx.go index d86b40fe0c..ed91d747a2 100644 --- a/x/ecocredit/client/testsuite/tx.go +++ b/x/ecocredit/client/testsuite/tx.go @@ -86,7 +86,6 @@ func (s *IntegrationTestSuite) SetupSuite() { out, err := cli.ExecTestCLICmd(val.ClientCtx, client.TxCreateClassCmd(), append( []string{ - val.Address.String(), val.Address.String(), validCreditType, validMetadata, @@ -198,56 +197,34 @@ func (s *IntegrationTestSuite) TestTxCreateClass() { expectedErrMsg string expectedClassInfo *ecocredit.ClassInfo }{ - { - name: "missing admin", - args: []string{}, - expectErr: true, - expectedErrMsg: "accepts 4 arg(s), received 0", - }, { name: "missing issuer", - args: []string{val0.Address.String()}, + args: []string{}, expectErr: true, - expectedErrMsg: "accepts 4 arg(s), received 1", + expectedErrMsg: "accepts 3 arg(s), received 0", }, { name: "missing credit type", - args: []string{validCreditType}, + args: []string{val0.Address.String()}, expectErr: true, - expectedErrMsg: "accepts 4 arg(s), received 1", + expectedErrMsg: "accepts 3 arg(s), received 1", }, { name: "missing metadata", - args: []string{val0.Address.String(), val0.Address.String()}, + args: []string{val0.Address.String(), validCreditType}, expectErr: true, - expectedErrMsg: "accepts 4 arg(s), received 2", + expectedErrMsg: "accepts 3 arg(s), received 2", }, { name: "too many args", - args: []string{"abcde", "abcde", "abcde", "abcde", "dlskjf"}, - expectErr: true, - expectedErrMsg: "accepts 4 arg(s), received 5", - }, - { - name: "invalid admin", - args: append( - []string{ - "abcde", - val0.Address.String(), - validCreditType, - validMetadata, - makeFlagFrom(val0.Address.String()), - }, - s.commonTxFlags()..., - ), + args: []string{"abcde", "abcde", "abcde", "abcde"}, expectErr: true, - expectedErrMsg: "decoding bech32 failed: invalid bech32 string length 5", + expectedErrMsg: "accepts 3 arg(s), received 4", }, { name: "invalid issuer", args: append( []string{ - val0.Address.String(), "abcde", validCreditType, validMetadata, @@ -262,7 +239,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() { name: "invalid metadata", args: append( []string{ - val0.Address.String(), val0.Address.String(), validCreditType, "=", @@ -277,7 +253,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() { name: "missing from flag", args: append( []string{ - val0.Address.String(), val0.Address.String(), validCreditType, validMetadata, @@ -291,7 +266,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() { name: "single issuer", args: append( []string{ - val0.Address.String(), val0.Address.String(), validCreditType, validMetadata, @@ -310,7 +284,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() { name: "multiple issuers", args: append( []string{ - val0.Address.String(), strings.Join( []string{ val0.Address.String(), @@ -335,7 +308,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() { name: "with amino-json", args: append( []string{ - val0.Address.String(), val0.Address.String(), validCreditType, validMetadata, diff --git a/x/ecocredit/client/tx.go b/x/ecocredit/client/tx.go index aba3074cd9..c8b7b9986e 100644 --- a/x/ecocredit/client/tx.go +++ b/x/ecocredit/client/tx.go @@ -48,29 +48,40 @@ func txflags(cmd *cobra.Command) *cobra.Command { func TxCreateClassCmd() *cobra.Command { return txflags(&cobra.Command{ - Use: "create-class [admin] [issuer[,issuer]*] [credit type] [metadata]", - Short: "Creates a new credit class", - Long: `Creates a new credit class. + Use: "create-class [issuer[,issuer]*] [credit type] [metadata]", + Short: "Creates a new credit class with transaction author (--from) as admin", + Long: `Creates a new credit class with transaction author (--from) as admin. +The transaction author must pay the fee associated with creating a new credit class. Parameters: - admin: address of the account which can manage the credit class issuer: comma separated (no spaces) list of issuer account addresses. Example: "addr1,addr2" credit type: the credit class type (e.g. carbon, biodiversity, etc) metadata: base64 encoded metadata - arbitrary data attached to the credit class info`, - Args: cobra.ExactArgs(4), + Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { - issuers := strings.Split(args[1], ",") + // Get the class admin from the --from flag + admin, err := cmd.Flags().GetString(flags.FlagFrom) + if err != nil { + return sdkerrors.ErrInvalidRequest.Wrap(err.Error()) + } + + // Parse the comma-separated list of issuers + issuers := strings.Split(args[0], ",") for i := range issuers { issuers[i] = strings.TrimSpace(issuers[i]) } - if args[2] == "" { + + // Check credit type is provided + if args[1] == "" { return sdkerrors.ErrInvalidRequest.Wrap("credit type is required") } - creditType := args[2] - if args[3] == "" { + creditType := args[1] + + // Check that metadata is provided and decode it + if args[2] == "" { return errors.New("base64_metadata is required") } - b, err := base64.StdEncoding.DecodeString(args[3]) + b, err := base64.StdEncoding.DecodeString(args[2]) if err != nil { return sdkerrors.ErrInvalidRequest.Wrap("metadata is malformed, proper base64 string is required") } @@ -80,7 +91,7 @@ Parameters: return err } msg := ecocredit.MsgCreateClass{ - Admin: args[0], + Admin: admin, Issuers: issuers, Metadata: b, CreditType: creditType, diff --git a/x/ecocredit/params.go b/x/ecocredit/params.go index b50c938f00..2582abb744 100644 --- a/x/ecocredit/params.go +++ b/x/ecocredit/params.go @@ -31,7 +31,7 @@ func ParamKeyTable() paramtypes.KeyTable { func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { return paramtypes.ParamSetPairs{ paramtypes.NewParamSetPair(KeyCreditClassFee, &p.CreditClassFee, validateCreditClassFee), - paramtypes.NewParamSetPair(KeyAllowedClassCreators, &p.AllowedClassCreators, validateAllowlistCreditCreators), + paramtypes.NewParamSetPair(KeyAllowedClassCreators, &p.AllowedClassCreators, validateAllowedClassCreators), paramtypes.NewParamSetPair(KeyAllowlistEnabled, &p.AllowlistEnabled, validateAllowlistEnabled), paramtypes.NewParamSetPair(KeyCreditTypes, &p.CreditTypes, validateCreditTypes), } @@ -50,7 +50,7 @@ func validateCreditClassFee(i interface{}) error { return nil } -func validateAllowlistCreditCreators(i interface{}) error { +func validateAllowedClassCreators(i interface{}) error { v, ok := i.([]string) if !ok { return fmt.Errorf("invalid parameter type: %T", i) diff --git a/x/ecocredit/params_test.go b/x/ecocredit/params_test.go index 0b670887d0..1a7fcd4efa 100644 --- a/x/ecocredit/params_test.go +++ b/x/ecocredit/params_test.go @@ -27,7 +27,7 @@ func TestDefaultParams(t *testing.T) { require.Equal(t, df.String(), expected.String()) } -func Test_validateAllowlistCreditCreators(t *testing.T) { +func Test_validateAllowedClassCreators(t *testing.T) { genAddrs := make([]string, 0, 3) for i := 0; i < 3; i++ { @@ -67,8 +67,8 @@ func Test_validateAllowlistCreditCreators(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := validateAllowlistCreditCreators(tt.args); (err != nil) != tt.wantErr { - t.Errorf("validateAllowlistCreditCreators() error = %v, wantErr %v", err, tt.wantErr) + if err := validateAllowedClassCreators(tt.args); (err != nil) != tt.wantErr { + t.Errorf("validateAllowedClassCreators() error = %v, wantErr %v", err, tt.wantErr) } }) }