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/bank) add spendable balances cmd #14045

Merged
merged 24 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c987390
feat: (x/bank) add spendable balances cmd
facundomedica Nov 28, 2022
c1c3dd3
cl++
facundomedica Nov 28, 2022
8130a3d
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Nov 28, 2022
7f37972
lint
facundomedica Nov 28, 2022
c0e5de7
Merge branch 'facu/spendable-balances-cmd' of https://github.com/cosm…
facundomedica Nov 28, 2022
6e54642
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Nov 29, 2022
5234194
fix from Likhita
facundomedica Nov 29, 2022
818721f
Merge branch 'facu/spendable-balances-cmd' of https://github.com/cosm…
facundomedica Nov 29, 2022
52addae
merge
facundomedica Dec 20, 2022
f204a13
finish spendable-balances command
facundomedica Dec 20, 2022
6a87cae
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Dec 20, 2022
3b17966
remove since annotations
facundomedica Dec 20, 2022
7221114
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Dec 21, 2022
91428ba
Revert "remove since annotations"
facundomedica Dec 22, 2022
1bb3377
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Dec 22, 2022
24c31cb
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Dec 22, 2022
a2ef3e1
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Dec 23, 2022
2a21cf8
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Jan 2, 2023
fb5039f
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Jan 3, 2023
b2b1843
Merge branch 'main' into facu/spendable-balances-cmd
alexanderbez Jan 5, 2023
7556c02
Merge branch 'main' into facu/spendable-balances-cmd
alexanderbez Jan 5, 2023
f908c6c
fix a bad merge
facundomedica Jan 5, 2023
a12ddb2
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Jan 5, 2023
c8e606c
Merge branch 'main' into facu/spendable-balances-cmd
facundomedica Jan 5, 2023
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 @@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (x/bank) [#14045](https://github.com/cosmos/cosmos-sdk/pull/14045) Add CLI command `spendable-balances`.
* (x/evidence) [#13740](https://github.com/cosmos/cosmos-sdk/pull/13740) Add new proto field `hash` of type `string` to `QueryEvidenceRequest` which helps to decode the hash properly while using query API.
* (core) [#13306](https://github.com/cosmos/cosmos-sdk/pull/13306) Add a `FormatCoins` function to in `core/coins` to format sdk Coins following the Value Renderers spec.
* (math) [#13306](https://github.com/cosmos/cosmos-sdk/pull/13306) Add `FormatInt` and `FormatDec` functiosn in `math` to format integers and decimals following the Value Renderers spec.
Expand Down
50 changes: 50 additions & 0 deletions x/bank/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,56 @@ Example:
return cmd
}

func GetSpendableBalancesCmd() *cobra.Command {
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
cmd := &cobra.Command{
Use: "spendable-balances [address]",
Short: "Query for account spendable balances by address",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the spendable balances of an account.

Example:
$ %s query %s spendable-balances [address]
`,
version.AppName, types.ModuleName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

addr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

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

ctx := cmd.Context()
params := types.NewQuerySpendableBalancesRequest(addr, pageReq)

res, err := queryClient.SpendableBalances(ctx, params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "spendable balances")

return cmd
}

// GetCmdDenomsMetadata defines the cobra command to query client denomination metadata.
func GetCmdDenomsMetadata() *cobra.Command {
cmd := &cobra.Command{
Expand Down
67 changes: 67 additions & 0 deletions x/bank/client/cli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,73 @@ func (s *CLITestSuite) TestGetBalancesCmd() {
}
}

func (s *CLITestSuite) TestGetSpendableBalancesCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)

cmd := cli.GetSpendableBalancesCmd()
cmd.SetOutput(io.Discard)

testCases := []struct {
name string
ctxGen func() client.Context
args []string
expectResult proto.Message
expectErr bool
}{
{
"valid query",
func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&types.QuerySpendableBalancesResponse{})
c := clitestutil.NewMockTendermintRPC(abci.ResponseQuery{
Value: bz,
})
return s.baseCtx.WithClient(c)
},
[]string{
accounts[0].Address.String(),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
&types.QuerySpendableBalancesResponse{},
false,
},
{
"invalid Address",
func() client.Context {
return s.baseCtx
},
[]string{
"foo",
},
nil,
true,
},
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
var outBuf bytes.Buffer

clientCtx := tc.ctxGen().WithOutput(&outBuf)
ctx := svrcmd.CreateExecuteContext(context.Background())

cmd.SetContext(ctx)
cmd.SetArgs(tc.args)

s.Require().NoError(client.SetCmdClientContextHandler(clientCtx, cmd))

err := cmd.Execute()
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(s.encCfg.Codec.UnmarshalJSON(outBuf.Bytes(), tc.expectResult))
s.Require().NoError(err)
}
})
}
}

func (s *CLITestSuite) TestGetCmdDenomsMetadata() {
cmd := cli.GetCmdDenomsMetadata()
cmd.SetOutput(io.Discard)
Expand Down