Skip to content

Commit

Permalink
Take credit class admin from --from flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ruhatch committed Aug 27, 2021
1 parent dd2d332 commit 654d35f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 52 deletions.
44 changes: 8 additions & 36 deletions x/ecocredit/client/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -262,7 +239,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() {
name: "invalid metadata",
args: append(
[]string{
val0.Address.String(),
val0.Address.String(),
validCreditType,
"=",
Expand All @@ -277,7 +253,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() {
name: "missing from flag",
args: append(
[]string{
val0.Address.String(),
val0.Address.String(),
validCreditType,
validMetadata,
Expand All @@ -291,7 +266,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() {
name: "single issuer",
args: append(
[]string{
val0.Address.String(),
val0.Address.String(),
validCreditType,
validMetadata,
Expand All @@ -310,7 +284,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() {
name: "multiple issuers",
args: append(
[]string{
val0.Address.String(),
strings.Join(
[]string{
val0.Address.String(),
Expand All @@ -335,7 +308,6 @@ func (s *IntegrationTestSuite) TestTxCreateClass() {
name: "with amino-json",
args: append(
[]string{
val0.Address.String(),
val0.Address.String(),
validCreditType,
validMetadata,
Expand Down
33 changes: 22 additions & 11 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -80,7 +91,7 @@ Parameters:
return err
}
msg := ecocredit.MsgCreateClass{
Admin: args[0],
Admin: admin,
Issuers: issuers,
Metadata: b,
CreditType: creditType,
Expand Down
4 changes: 2 additions & 2 deletions x/ecocredit/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions x/ecocredit/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down Expand Up @@ -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)
}
})
}
Expand Down

0 comments on commit 654d35f

Please sign in to comment.