feat(bank): autocli query support (#16899)

This commit is contained in:
Julien Robert
2023-07-10 12:20:45 +00:00
committed by GitHub
parent 81b70496c5
commit febeced2e1
13 changed files with 242 additions and 1160 deletions
+21 -22
View File
@@ -612,11 +612,11 @@ func (s *E2ETestSuite) TestCLISendGenerateSignAndBroadcast() {
s.Require().NoError(err)
s.Require().Equal(0, len(sigs))
resp, err := clitestutil.QueryBalancesExec(val1.ClientCtx, val1.Address, addresscodec.NewBech32Codec("cosmos"))
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val1.APIAddress, val1.Address))
s.Require().NoError(err)
var balRes banktypes.QueryAllBalancesResponse
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
startTokens := balRes.Balances.AmountOf(s.cfg.BondDenom)
@@ -679,10 +679,9 @@ func (s *E2ETestSuite) TestCLISendGenerateSignAndBroadcast() {
s.Require().NoError(s.network.WaitForNextBlock())
// Ensure foo has right amount of funds
resp, err = clitestutil.QueryBalancesExec(val1.ClientCtx, val1.Address, addresscodec.NewBech32Codec("cosmos"))
resp, err = testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val1.APIAddress, val1.Address))
s.Require().NoError(err)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
s.Require().Equal(startTokens, balRes.Balances.AmountOf(s.cfg.BondDenom))
@@ -701,20 +700,20 @@ func (s *E2ETestSuite) TestCLISendGenerateSignAndBroadcast() {
// Ensure destiny account state
err = s.network.RetryForBlocks(func() error {
resp, err = clitestutil.QueryBalancesExec(val1.ClientCtx, addr, addresscodec.NewBech32Codec("cosmos"))
resp, err = testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val1.APIAddress, addr))
s.Require().NoError(err)
return err
}, 3)
s.Require().NoError(err)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
s.Require().Equal(sendTokens.Amount, balRes.Balances.AmountOf(s.cfg.BondDenom))
// Ensure origin account state
resp, err = clitestutil.QueryBalancesExec(val1.ClientCtx, val1.Address, addresscodec.NewBech32Codec("cosmos"))
resp, err = testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val1.APIAddress, val1.Address))
s.Require().NoError(err)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
}
@@ -833,11 +832,11 @@ func (s *E2ETestSuite) TestCLIMultisignSortSignatures() {
addr, err := multisigRecord.GetAddress()
s.Require().NoError(err)
resp, err := clitestutil.QueryBalancesExec(val1.ClientCtx, addr, addresscodec.NewBech32Codec("cosmos"))
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val1.APIAddress, addr))
s.Require().NoError(err)
var balRes banktypes.QueryAllBalancesResponse
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
intialCoins := balRes.Balances
@@ -851,10 +850,9 @@ func (s *E2ETestSuite) TestCLIMultisignSortSignatures() {
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
resp, err = clitestutil.QueryBalancesExec(val1.ClientCtx, addr, addresscodec.NewBech32Codec("cosmos"))
resp, err = testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val1.APIAddress, addr))
s.Require().NoError(err)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = val1.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
diff, _ := balRes.Balances.SafeSub(intialCoins...)
s.Require().Equal(sendTokens.Amount, diff.AmountOf(s.cfg.BondDenom))
@@ -993,11 +991,12 @@ func (s *E2ETestSuite) TestCLIMultisign() {
var balRes banktypes.QueryAllBalancesResponse
err = s.network.RetryForBlocks(func() error {
resp, err := clitestutil.QueryBalancesExec(val1.ClientCtx, addr, addresscodec.NewBech32Codec("cosmos"))
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val1.APIAddress, addr))
s.Require().NoError(err)
if err != nil {
return err
}
return val1.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
return val1.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
}, 3)
s.Require().NoError(err)
s.Require().True(sendTokens.Amount.Equal(balRes.Balances.AmountOf(s.cfg.BondDenom)))
@@ -1393,10 +1392,10 @@ func (s *E2ETestSuite) TestSignWithMultiSignersAminoJSON() {
require.Equal(uint32(0), txRes.Code, txRes.RawLog)
// Make sure the addr1's balance got funded.
queryResJSON, err := clitestutil.QueryBalancesExec(val0.ClientCtx, addr1, addresscodec.NewBech32Codec("cosmos"))
require.NoError(err)
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val0.APIAddress, addr1))
s.Require().NoError(err)
var queryRes banktypes.QueryAllBalancesResponse
err = val0.ClientCtx.Codec.UnmarshalJSON(queryResJSON.Bytes(), &queryRes)
err = val0.ClientCtx.Codec.UnmarshalJSON(resp, &queryRes)
require.NoError(err)
require.Equal(sdk.NewCoins(val0Coin, val1Coin), queryRes.Balances)
}
@@ -1729,11 +1728,11 @@ func (s *E2ETestSuite) createBankMsg(val *network.Validator, toAddr sdk.AccAddre
}
func (s *E2ETestSuite) getBalances(clientCtx client.Context, addr sdk.AccAddress, denom string) math.Int {
resp, err := clitestutil.QueryBalancesExec(clientCtx, addr, addresscodec.NewBech32Codec("cosmos"))
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s/by_denom?denom=%s", s.cfg.APIAddress, addr.String(), denom))
s.Require().NoError(err)
var balRes banktypes.QueryAllBalancesResponse
err = clientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = clientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
startTokens := balRes.Balances.AmountOf(denom)
return startTokens
-271
View File
@@ -18,7 +18,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
@@ -96,276 +95,6 @@ func (s *E2ETestSuite) TearDownSuite() {
s.network.Cleanup()
}
func (s *E2ETestSuite) TestGetBalancesCmd() {
val := s.network.Validators[0]
testCases := []struct {
name string
args []string
expectErr bool
respType proto.Message
expected proto.Message
}{
{"no address provided", []string{}, true, nil, nil},
{
"total account balance",
[]string{
val.Address.String(),
fmt.Sprintf("--%s=json", flags.FlagOutput),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
false,
&types.QueryAllBalancesResponse{},
&types.QueryAllBalancesResponse{
Balances: sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Sub(s.cfg.BondedTokens)),
),
Pagination: &query.PageResponse{},
},
},
{
"total account balance of a specific denom",
[]string{
val.Address.String(),
fmt.Sprintf("--%s=json", flags.FlagOutput),
fmt.Sprintf("--%s=%s", cli.FlagDenom, s.cfg.BondDenom),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
false,
&sdk.Coin{},
NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Sub(s.cfg.BondedTokens)),
},
{
"total account balance of a bogus denom",
[]string{
val.Address.String(),
fmt.Sprintf("--%s=foobar", cli.FlagDenom),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
false,
&sdk.Coin{},
NewCoin("foobar", math.ZeroInt()),
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetBalancesCmd(addresscodec.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType))
s.Require().Equal(tc.expected.String(), tc.respType.String())
}
})
}
}
func (s *E2ETestSuite) TestGetCmdQueryTotalSupply() {
val := s.network.Validators[0]
testCases := []struct {
name string
args []string
expectErr bool
respType proto.Message
expected proto.Message
}{
{
name: "total supply",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
respType: &types.QueryTotalSupplyResponse{},
expected: &types.QueryTotalSupplyResponse{
Supply: sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(math.NewInt(10))),
),
Pagination: &query.PageResponse{Total: 0},
},
},
{
name: "total supply of a specific denomination",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=%s", cli.FlagDenom, s.cfg.BondDenom),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
respType: &sdk.Coin{},
expected: &sdk.Coin{
Denom: s.cfg.BondDenom,
Amount: s.cfg.StakingTokens.Add(math.NewInt(10)),
},
},
{
name: "total supply of a bogus denom",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=foobar", cli.FlagDenom),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
respType: &sdk.Coin{},
expected: &sdk.Coin{
Denom: "foobar",
Amount: math.ZeroInt(),
},
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdQueryTotalSupply()
clientCtx := val.ClientCtx
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType))
s.Require().Equal(tc.expected, tc.respType)
}
})
}
}
func (s *E2ETestSuite) TestGetCmdQueryDenomsMetadata() {
val := s.network.Validators[0]
testCases := []struct {
name string
args []string
expectErr bool
respType proto.Message
expected proto.Message
}{
{
name: "all denoms client metadata",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
respType: &types.QueryDenomsMetadataResponse{},
expected: &types.QueryDenomsMetadataResponse{
Metadatas: []types.Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Denom: "uatom",
Exponent: 0,
Aliases: []string{"microatom"},
},
{
Denom: "atom",
Exponent: 6,
Aliases: []string{"ATOM"},
},
},
Base: "uatom",
Display: "atom",
},
{
Name: "Ethereum",
Symbol: "ETH",
Description: "Ethereum mainnet token",
DenomUnits: []*types.DenomUnit{
{
Denom: "wei",
Exponent: 0,
Aliases: []string{},
},
{
Denom: "eth",
Exponent: 6,
Aliases: []string{"ETH"},
},
},
Base: "wei",
Display: "eth",
},
},
Pagination: &query.PageResponse{Total: 2},
},
},
{
name: "client metadata of a specific denomination",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=%s", cli.FlagDenom, "uatom"),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
respType: &types.QueryDenomMetadataResponse{},
expected: &types.QueryDenomMetadataResponse{
Metadata: types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Denom: "uatom",
Exponent: 0,
Aliases: []string{"microatom"},
},
{
Denom: "atom",
Exponent: 6,
Aliases: []string{"ATOM"},
},
},
Base: "uatom",
Display: "atom",
},
},
},
{
name: "client metadata of a bogus denom",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=foobar", cli.FlagDenom),
fmt.Sprintf("--%s=json", flags.FlagOutput),
},
expectErr: true,
respType: &types.QueryDenomMetadataResponse{},
expected: &types.QueryDenomMetadataResponse{
Metadata: types.Metadata{
DenomUnits: []*types.DenomUnit{},
},
},
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.GetCmdDenomsMetadata()
clientCtx := val.ClientCtx
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType))
s.Require().Equal(tc.expected, tc.respType)
}
})
}
}
func (s *E2ETestSuite) TestNewSendTxCmdGenOnly() {
val := s.network.Validators[0]
+4 -6
View File
@@ -10,7 +10,6 @@ import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
@@ -353,9 +352,9 @@ func (s *E2ETestSuite) TestNewCmdCancelProposal() {
var balRes banktypes.QueryAllBalancesResponse
var newBalance banktypes.QueryAllBalancesResponse
if !tc.expectErr && tc.expectedCode == 0 {
resp, err := clitestutil.QueryBalancesExec(clientCtx, val.Address, addresscodec.NewBech32Codec("cosmos"))
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val.APIAddress, val.Address.String()))
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
}
@@ -364,14 +363,13 @@ func (s *E2ETestSuite) TestNewCmdCancelProposal() {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp), out.String())
s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, resp.TxHash, tc.expectedCode))
if !tc.expectErr && tc.expectedCode == 0 {
resp, err := clitestutil.QueryBalancesExec(clientCtx, val.Address, addresscodec.NewBech32Codec("cosmos"))
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val.APIAddress, val.Address.String()))
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(resp.Bytes(), &newBalance)
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &newBalance)
s.Require().NoError(err)
remainingAmount := v1.DefaultMinDepositTokens.Mul(
v1.DefaultProposalCancelRatio.Mul(math.LegacyMustNewDecFromStr("100")).TruncateInt(),
@@ -1242,11 +1242,11 @@ func (s *CLITestSuite) TestAuxToFeeWithTips() {
}
func (s *CLITestSuite) getBalances(clientCtx client.Context, addr sdk.AccAddress, denom string) math.Int {
resp, err := clitestutil.QueryBalancesExec(clientCtx, addr, s.ac)
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s/by_denom?denom=%s", s.baseCtx.NodeURI, addr.String(), denom))
s.Require().NoError(err)
var balRes banktypes.QueryAllBalancesResponse
err = clientCtx.Codec.UnmarshalJSON(resp.Bytes(), &balRes)
err = clientCtx.Codec.UnmarshalJSON(resp, &balRes)
s.Require().NoError(err)
startTokens := balRes.Balances.AmountOf(denom)
return startTokens