Skip to content

Commit

Permalink
refactor(x/ecocredit): convert class fee argument to optional flag (#…
Browse files Browse the repository at this point in the history
…1475)

* refactor: convert class fee to optional flag

* chore: add changelog

* chore: review changes

* Update x/ecocredit/base/client/tx.go

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>

* chore: fix fee error

* Update x/ecocredit/client/testsuite/tx.go

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
aleem1314 and ryanchristo authored Sep 15, 2022
1 parent f4f3106 commit c9a461f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1429](https://github.com/regen-network/regen-ledger/pull/1429) Migrated `ecocredit/simulation` core operations to `ecocredit/base/simulation`
- [#1447](https://github.com/regen-network/regen-ledger/pull/1447) Rename `CoreKeeper` to `BaseKeeper`
- [#1452](https://github.com/regen-network/regen-ledger/pull/1452) Migrated `BeginBlocker` to `ecocredit/module`
- [#1475](https://github.com/regen-network/regen-ledger/pull/1475) Convert `CreateClass` command `fee` argument to optional flag

### Fixed

Expand Down
42 changes: 30 additions & 12 deletions x/ecocredit/base/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ const (
FlagRemoveIssuers string = "remove-issuers"
FlagReferenceID string = "reference-id"
FlagRetirementJurisdiction string = "retirement-jurisdiction"
FlagClassFee string = "class-fee"
)

// TxCreateClassCmd returns a transaction command that creates a credit class.
func TxCreateClassCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-class [issuers] [credit-type-abbrev] [metadata] [fee] [flags]",
Use: "create-class [issuers] [credit-type-abbrev] [metadata] [flags]",
Short: "Creates a new credit class with transaction author (--from) as admin",
Long: fmt.Sprintf(`Creates a new credit class with transaction author (--from) as admin.
Expand All @@ -44,13 +45,21 @@ Parameters:
- issuers: comma separated (no spaces) list of issuer account addresses
- credit-type-abbrev: the abbreviation of a credit type
- metadata: arbitrary data attached to the credit class info
- fee: fee to pay for the creation of the credit class`,
Flags:
- class-fee: the fee that the class creator will pay to create the credit class. It must be >= the
required credit_class_fee param. If the credit_class_fee param is empty, no fee is required.
We explicitly include the class creation fee here so that the class creator acknowledges paying
the fee and is not surprised to learn that the they paid a fee without consent.
`,
types.KeyAllowedClassCreators,
types.KeyCreditClassFee,
),
Example: `regen tx ecocredit create-class regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw C regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf 20000000uregen
regen tx ecocredit create-class regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw,regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6 C regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf 20000000uregen`,
Args: cobra.ExactArgs(4),
Example: `regen tx ecocredit create-class regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw C regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf --class-fee 20000000uregen
regen tx ecocredit create-class regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw,regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6 C regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf --class-fee 20000000uregen`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -66,24 +75,33 @@ regen tx ecocredit create-class regen1elq7ys34gpkj3jyvqee0h6yk4h9wsfxmgqelsw,reg
issuers[i] = strings.TrimSpace(issuers[i])
}

// Parse and normalize credit class fee
fee, err := sdk.ParseCoinNormalized(args[3])
if err != nil {
return err
}

msg := types.MsgCreateClass{
Admin: admin.String(),
Issuers: issuers,
Metadata: args[2],
CreditTypeAbbrev: args[1],
Fee: &fee,
}

// Parse and normalize credit class fee
feeString, err := cmd.Flags().GetString(FlagClassFee)
if err != nil {
return err
}
if feeString != "" {
fee, err := sdk.ParseCoinNormalized(feeString)
if err != nil {
return fmt.Errorf("failed to parse class-fee: %w", err)
}

msg.Fee = &fee
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}

cmd.Flags().String(FlagClassFee, "", "the fee that the class creator will pay to create the credit class (e.g. \"20regen\")")

return txFlags(cmd)
}

Expand Down
4 changes: 2 additions & 2 deletions x/ecocredit/basket/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Flags:
false unless the credits were previously put into the basket by the address
picking them from the basket, in which case they will remain tradable.
- credit-type-abbrev: filters against credits from this credit type abbreviation (e.g. "BIO").
- allowed_classes: comma separated (no spaces) list of credit classes allowed to be put in
- allowed-classes: comma separated (no spaces) list of credit classes allowed to be put in
the basket (e.g. "C01,C02").
- min-start-date: the earliest start date for batches of credits allowed into the basket.
- start-date-window: the duration of time (in seconds) measured into the past which sets a
Expand All @@ -56,7 +56,7 @@ Flags:
curator explicitly acknowledges paying this fee and is not surprised to learn that they
paid a big fee and didn't know beforehand.
- description: the description to be used in the basket coin's bank denom metadata.`),
Example: `regen tx ecocredit create-basket NCT --credit-type-abbrev C --allowed_classes C01,C02 basket-fee 100000000uregen description "NCT basket"`,
Example: `regen tx ecocredit create-basket NCT --credit-type-abbrev C --allowed-classes C01,C02 --basket-fee 100000000uregen --description "NCT basket"`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
Expand Down
11 changes: 6 additions & 5 deletions x/ecocredit/client/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
basetypes "github.com/regen-network/regen-ledger/x/ecocredit/base/types/v1"
basketclient "github.com/regen-network/regen-ledger/x/ecocredit/basket/client"
baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket/types/v1"
"github.com/regen-network/regen-ledger/x/ecocredit/genesis"
marketclient "github.com/regen-network/regen-ledger/x/ecocredit/marketplace/client"
markettypes "github.com/regen-network/regen-ledger/x/ecocredit/marketplace/types/v1"
)
Expand All @@ -49,7 +50,7 @@ type IntegrationTestSuite struct {
addr2 sdk.AccAddress

// test values
creditClassFee sdk.Coins
creditClassFee *sdk.Coin
basketFee sdk.Coins
creditTypeAbbrev string
allowedDenoms []string
Expand Down Expand Up @@ -91,7 +92,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
Issuers: []string{s.addr1.String()},
Metadata: "metadata",
CreditTypeAbbrev: s.creditTypeAbbrev,
Fee: &s.creditClassFee[0],
Fee: s.creditClassFee,
})

// set test reference id
Expand Down Expand Up @@ -260,8 +261,8 @@ func (s *IntegrationTestSuite) setupGenesis() {
require.NoError(err)

// set credit class and basket fees
s.creditClassFee = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, basetypes.DefaultClassFee))
s.basketFee = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, basetypes.DefaultBasketFee))
s.creditClassFee = genesis.DefaultClassFee().Fee
s.basketFee = sdk.NewCoins(*genesis.DefaultBasketFee().Fee)

// get raw json from target
json, err := target.JSON()
Expand Down Expand Up @@ -329,8 +330,8 @@ func (s *IntegrationTestSuite) createClass(clientCtx client.Context, msg *basety
strings.Join(msg.Issuers, ","),
msg.CreditTypeAbbrev,
msg.Metadata,
msg.Fee.String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, msg.Admin),
fmt.Sprintf("--%s=%s", baseclient.FlagClassFee, msg.Fee.String()),
}
args = append(args, s.commonTxFlags()...)
out, err := cli.ExecTestCLICmd(clientCtx, cmd, args)
Expand Down
22 changes: 11 additions & 11 deletions x/ecocredit/client/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ func (s *IntegrationTestSuite) TestTxCreateClassCmd() {
name: "missing args",
args: []string{},
expErr: true,
expErrMsg: "Error: accepts 4 arg(s), received 0",
expErrMsg: "Error: accepts 3 arg(s), received 0",
},
{
name: "too many args",
args: []string{"foo", "bar", "baz", "bar", "foo"},
args: []string{"foo", "bar", "baz", "bar"},
expErr: true,
expErrMsg: "Error: accepts 4 arg(s), received 5",
expErrMsg: "Error: accepts 3 arg(s), received 4",
},
{
name: "missing from flag",
args: []string{
admin,
s.creditTypeAbbrev,
"metadata",
creditClassFee,
fmt.Sprintf("--%s=%s", client.FlagClassFee, creditClassFee),
},
expErr: true,
expErrMsg: "Error: required flag(s) \"from\" not set",
Expand All @@ -55,8 +55,8 @@ func (s *IntegrationTestSuite) TestTxCreateClassCmd() {
admin,
s.creditTypeAbbrev,
"metadata",
"foo",
fmt.Sprintf("--%s=%s", flags.FlagFrom, admin),
fmt.Sprintf("--%s=%s", client.FlagClassFee, "foo"),
},
expErr: true,
expErrMsg: "invalid decimal coin expression",
Expand All @@ -67,8 +67,8 @@ func (s *IntegrationTestSuite) TestTxCreateClassCmd() {
admin,
s.creditTypeAbbrev,
"metadata",
creditClassFee,
fmt.Sprintf("--%s=%s", flags.FlagFrom, admin),
fmt.Sprintf("--%s=%s", client.FlagClassFee, creditClassFee),
},
},
{
Expand All @@ -77,8 +77,8 @@ func (s *IntegrationTestSuite) TestTxCreateClassCmd() {
admin,
s.creditTypeAbbrev,
"metadata",
creditClassFee,
fmt.Sprintf("--%s=%s", flags.FlagFrom, s.val.Moniker),
fmt.Sprintf("--%s=%s", client.FlagClassFee, creditClassFee),
},
},
{
Expand All @@ -87,9 +87,9 @@ func (s *IntegrationTestSuite) TestTxCreateClassCmd() {
admin,
s.creditTypeAbbrev,
"metadata",
creditClassFee,
fmt.Sprintf("--%s=%s", flags.FlagFrom, admin),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
fmt.Sprintf("--%s=%s", client.FlagClassFee, creditClassFee),
},
},
}
Expand Down Expand Up @@ -797,7 +797,7 @@ func (s *IntegrationTestSuite) TestTxUpdateClassAdmin() {
Issuers: []string{admin},
Metadata: "metadata",
CreditTypeAbbrev: s.creditTypeAbbrev,
Fee: &s.creditClassFee[0],
Fee: s.creditClassFee,
})

// create new credit class to not interfere with other tests
Expand All @@ -806,7 +806,7 @@ func (s *IntegrationTestSuite) TestTxUpdateClassAdmin() {
Issuers: []string{admin},
Metadata: "metadata",
CreditTypeAbbrev: s.creditTypeAbbrev,
Fee: &s.creditClassFee[0],
Fee: s.creditClassFee,
})

// create new credit class to not interfere with other tests
Expand All @@ -815,7 +815,7 @@ func (s *IntegrationTestSuite) TestTxUpdateClassAdmin() {
Issuers: []string{admin},
Metadata: "metadata",
CreditTypeAbbrev: s.creditTypeAbbrev,
Fee: &s.creditClassFee[0],
Fee: s.creditClassFee,
})

testCases := []struct {
Expand Down

0 comments on commit c9a461f

Please sign in to comment.