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

feat(x/ecocredit): add query balances by batch cmd #1634

Merged
merged 3 commits into from
Nov 24, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1484](https://github.com/regen-network/regen-ledger/pull/1484) Add `Msg/UpdateCurator`
- [#1623](https://github.com/regen-network/regen-ledger/pull/1623) Add `Msg/UpdateBatchMetadata`
- [#1625](https://github.com/regen-network/regen-ledger/pull/1625) Add `reason`/`retirement_reason`
- [#1634](https://github.com/regen-network/regen-ledger/pull/1634) Add `balances-by-batch` query command

#### Changed

Expand Down
61 changes: 44 additions & 17 deletions x/ecocredit/base/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ regen q ecocredit class-issuers C01 --limit 10 --offset 10`,
ClassId: args[0],
Pagination: pagination,
})
if err != nil {
return err
}

return printQueryResponse(ctx, res, err)
},
Expand Down Expand Up @@ -260,7 +257,7 @@ func QueryBatchesByClassCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "batches-by-class [class-id]",
Short: "List all credit batches by credit class",
Long: "List all credit batches by credit class with pagination flags.",
Long: "List all credit batches by credit class with optional pagination flags.",
Example: `regen q ecocredit batches-by-class C01
regen q ecocredit batches-by-class C01 --limit 10 --offset 10`,
Args: cobra.ExactArgs(1),
Expand Down Expand Up @@ -560,10 +557,9 @@ func QueryAllowedClassCreatorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "allowed-class-creators",
Short: "Retrieve the allowed credit class creators",
Long: "Retrieve the list of allowed credit class creators with pagination",
Example: `
regen q ecocredit allowed-class-creators
regen q ecocredit allowed-class-creators --limit 10`,
Long: "Retrieve the list of allowed credit class creators with optional pagination flags.",
Example: `regen q ecocredit allowed-class-creators
regen q ecocredit allowed-class-creators --limit 10`,
RunE: func(cmd *cobra.Command, args []string) error {
c, ctx, err := mkQueryClient(cmd)
if err != nil {
Expand All @@ -587,17 +583,15 @@ func QueryAllowedClassCreatorsCmd() *cobra.Command {
return qflags(cmd)
}

// QueryAllBalances returns a query command that retrieves a list of all ecocredit balances
// QueryAllBalancesCmd returns a query command that retrieves a list of all ecocredit balances
// with pagination.
func QueryAllBalances() *cobra.Command {
func QueryAllBalancesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "all-balances",
Short: "Retrieve all ecocredit balances",
Long: "Retrieve all ecocredit balances across all addresses and batch denoms with pagination",
Example: `
regen q ecocredit all-balances
regen q ecocredit all-balances --limit 10
`,
Long: "Retrieve all ecocredit balances across all addresses and credit batches with optional pagination flags.",
Example: `regen q ecocredit all-balances
regen q ecocredit all-balances --limit 10`,
RunE: func(cmd *cobra.Command, args []string) error {
c, ctx, err := mkQueryClient(cmd)
if err != nil {
Expand All @@ -620,9 +614,42 @@ func QueryAllBalances() *cobra.Command {
return qflags(cmd)
}

// QueryAllowedBridgeChains returns a query command that retrieves a list of chain that are allowed to be used
// QueryBalancesByBatchCmd returns a query command that retrieves a list of all ecocredit balances
// with pagination.
func QueryBalancesByBatchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "balances-by-batch [batch-denom]",
Short: "Retrieve all ecocredit balances for a given credit batch",
Long: "Retrieve all ecocredit balances for a given credit batch with optional pagination flags.",
Example: `regen q ecocredit balances-by-batch C01-001-20200101-20210101-001
regen q ecocredit balances-by-batch C01-001-20200101-20210101-001 --limit 10`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, ctx, err := mkQueryClient(cmd)
if err != nil {
return err
}
pagination, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

res, err := c.BalancesByBatch(cmd.Context(), &types.QueryBalancesByBatchRequest{
BatchDenom: args[0],
Pagination: pagination,
})
return printQueryResponse(ctx, res, err)
},
}

flags.AddPaginationFlagsToCmd(cmd, "all-balances")

return qflags(cmd)
}

// QueryAllowedBridgeChainsCmd returns a query command that retrieves a list of chain that are allowed to be used
// in bridge operations.
func QueryAllowedBridgeChains() *cobra.Command {
func QueryAllowedBridgeChainsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "allowed-bridge-chains",
Short: "Retrieve the list of allowed bridge chains",
Expand Down
5 changes: 3 additions & 2 deletions x/ecocredit/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ func QueryCmd(name string) *cobra.Command {
baseclient.QueryClassCreatorAllowlistCmd(),
baseclient.QueryAllowedClassCreatorsCmd(),
baseclient.QueryClassFeeCmd(),
baseclient.QueryAllBalances(),
baseclient.QueryAllowedBridgeChains(),
baseclient.QueryAllBalancesCmd(),
baseclient.QueryBalancesByBatchCmd(),
baseclient.QueryAllowedBridgeChainsCmd(),
basketclient.QueryBasketCmd(),
basketclient.QueryBasketsCmd(),
basketclient.QueryBasketBalanceCmd(),
Expand Down
76 changes: 74 additions & 2 deletions x/ecocredit/client/testsuite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ func (s *IntegrationTestSuite) TestQueryCreditTypeCmd() {
}
}

func (s *IntegrationTestSuite) TestQueryAllowedClassCreators() {
func (s *IntegrationTestSuite) TestQueryAllowedClassCreatorsCmd() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
clientCtx.OutputFormat = outputFormat
Expand Down Expand Up @@ -929,7 +929,7 @@ func (s *IntegrationTestSuite) TestQueryAllBalancesCmd() {
clientCtx := val.ClientCtx
clientCtx.OutputFormat = outputFormat

cmd := client.QueryAllBalances()
cmd := client.QueryAllBalancesCmd()
out, err := cli.ExecTestCLICmd(clientCtx, cmd, []string{fmt.Sprintf("--%s", flags.FlagCountTotal)})

s.Require().NoError(err, out.String())
Expand All @@ -939,3 +939,75 @@ func (s *IntegrationTestSuite) TestQueryAllBalancesCmd() {
s.Require().Greater(len(res.Balances), 0)
s.Require().Greater(res.Pagination.Total, uint64(0))
}

func (s *IntegrationTestSuite) TestQueryBalancesByBatchCmd() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
clientCtx.OutputFormat = outputFormat

testCases := []struct {
name string
args []string
expectErr bool
expectedErrMsg string
}{
{
name: "missing args",
args: []string{},
expectErr: true,
expectedErrMsg: "Error: accepts 1 arg(s), received 0",
},
{
name: "too many args",
args: []string{"abcde", "abcde"},
expectErr: true,
expectedErrMsg: "Error: accepts 1 arg(s), received 2",
},
{
name: "valid",
args: []string{s.batchDenom},
expectErr: false,
},
{
name: "valid with pagination",
args: []string{
s.batchDenom,
fmt.Sprintf("--%s", flags.FlagCountTotal),
fmt.Sprintf("--%s=%d", flags.FlagLimit, 1),
},
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := client.QueryBalancesByBatchCmd()
out, err := cli.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
s.Require().Contains(out.String(), tc.expectedErrMsg)
} else {
s.Require().NoError(err, out.String())

var res types.QueryBalancesByBatchResponse
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
s.Require().NotEmpty(res.Balances)
}
})
}
}

func (s *IntegrationTestSuite) TestQueryAllowedBridgeChainsCmd() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
clientCtx.OutputFormat = outputFormat

cmd := client.QueryAllowedBridgeChainsCmd()
out, err := cli.ExecTestCLICmd(clientCtx, cmd, []string{})

s.Require().NoError(err, out.String())

var res types.QueryAllowedBridgeChainsResponse
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
s.Require().Len(res.AllowedBridgeChains, 1)
s.Require().Equal(res.AllowedBridgeChains[0], s.bridgeChain)
}
9 changes: 9 additions & 0 deletions x/ecocredit/client/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type IntegrationTestSuite struct {
basketFee sdk.Coins
creditTypeAbbrev string
allowedDenoms []string
bridgeChain string
classID string
projectID string
projectReferenceID string
Expand Down Expand Up @@ -256,6 +257,14 @@ func (s *IntegrationTestSuite) setupGenesis() {
})
require.NoError(err)

s.bridgeChain = "polygon"

// set allowed bridge chain
err = baseStore.AllowedBridgeChainTable().Insert(ctx, &baseapi.AllowedBridgeChain{
ChainName: s.bridgeChain,
})
require.NoError(err)

// export genesis into target
target := ormjson.NewRawMessageTarget()
err = mdb.ExportJSON(ctx, target)
Expand Down