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: add basket balance queries #760

Merged
merged 1 commit into from
Feb 11, 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
88 changes: 88 additions & 0 deletions x/ecocredit/client/basket/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package basketclient

import (
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"

"github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

// QueryBasketBalanceCmd returns a query command that retrieves the balance of a credit batch in the basket.
func QueryBasketBalanceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "basket-balance [basket-denom] [batch-denom]",
Short: "Retrieves the balance of a credit batch in the basket",
Long: strings.TrimSpace(`Retrieves the balance of a credit batch in the basket
Example:
$regen q ecocredit basket-balance BASKET C01-20210101-20220101-001
`),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
client := basket.NewQueryClient(ctx)

res, err := client.BasketBalance(cmd.Context(), &basket.QueryBasketBalanceRequest{
BasketDenom: args[0],
BatchDenom: args[1],
})
if err != nil {
return err
}

return ctx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// QueryBasketBalancesCmd returns a query command that retrieves the the balance of each credit batch for the given basket denom.
func QueryBasketBalancesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "basket-balances [basket-denom]",
Short: "Retrieves the the balance of each credit batch for the given basket denom",
Long: strings.TrimSpace(`Retrieves the the balance of each credit batch for the given basket denom

Examples:
$regen q ecocredit basket-balances BASKET1
$regen q ecocredit basket-balances BASKET1 --pagination.offset 1 --pagination.limit 10
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pagination, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

client := basket.NewQueryClient(ctx)
res, err := client.BasketBalances(cmd.Context(), &basket.QueryBasketBalancesRequest{
BasketDenom: args[0],
Pagination: pagination,
})
if err != nil {
return err
}

return ctx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "basket-balances")

return cmd
}
3 changes: 3 additions & 0 deletions x/ecocredit/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"

"github.com/regen-network/regen-ledger/x/ecocredit"
basketcli "github.com/regen-network/regen-ledger/x/ecocredit/client/basket"
)

// QueryCmd returns the parent command for all x/ecocredit query commands.
Expand All @@ -32,6 +33,8 @@ func QueryCmd(name string) *cobra.Command {
QuerySupplyCmd(),
QueryCreditTypesCmd(),
QueryParams(),
basketcli.QueryBasketBalanceCmd(),
basketcli.QueryBasketBalancesCmd(),
)
return cmd
}
Expand Down