cosmos-sdk/x/gov/client/testutil/helpers.go

174 lines
6.7 KiB
Go
Raw Normal View History

2020-05-10 07:25:27 -07:00
package testutil
import (
"fmt"
"strings"
"github.com/stretchr/testify/require"
2020-05-10 07:25:27 -07:00
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
2020-06-13 02:07:51 -07:00
"github.com/cosmos/cosmos-sdk/x/gov/types"
2020-05-10 07:25:27 -07:00
)
//___________________________________________________________________________________
// simcli query gov
// QueryGovParamDeposit is simcli query gov param deposit
2020-06-13 02:07:51 -07:00
func QueryGovParamDeposit(f *cli.Fixtures) types.DepositParams {
cmd := fmt.Sprintf("%s query gov param deposit %s", f.SimdBinary, f.Flags())
2020-05-10 07:25:27 -07:00
out, _ := tests.ExecuteT(f.T, cmd, "")
2020-06-13 02:07:51 -07:00
var depositParam types.DepositParams
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(out), &depositParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return depositParam
}
// QueryGovParamVoting is simcli query gov param voting
2020-06-13 02:07:51 -07:00
func QueryGovParamVoting(f *cli.Fixtures) types.VotingParams {
cmd := fmt.Sprintf("%s query gov param voting %s", f.SimdBinary, f.Flags())
2020-05-10 07:25:27 -07:00
out, _ := tests.ExecuteT(f.T, cmd, "")
2020-06-13 02:07:51 -07:00
var votingParam types.VotingParams
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(out), &votingParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return votingParam
}
// QueryGovParamTallying is simcli query gov param tallying
2020-06-13 02:07:51 -07:00
func QueryGovParamTallying(f *cli.Fixtures) types.TallyParams {
cmd := fmt.Sprintf("%s query gov param tallying %s", f.SimdBinary, f.Flags())
2020-05-10 07:25:27 -07:00
out, _ := tests.ExecuteT(f.T, cmd, "")
2020-06-13 02:07:51 -07:00
var tallyingParam types.TallyParams
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(out), &tallyingParam)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return tallyingParam
}
// QueryGovProposal is simcli query gov proposal
2020-06-13 02:07:51 -07:00
func QueryGovProposal(f *cli.Fixtures, proposalID int, flags ...string) types.Proposal {
cmd := fmt.Sprintf("%s query gov proposal %d %v", f.SimdBinary, proposalID, f.Flags())
2020-05-10 07:25:27 -07:00
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
2020-06-13 02:07:51 -07:00
var proposal types.Proposal
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(out), &proposal)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return proposal
}
// QueryGovProposals is simcli query gov proposals
2020-06-13 02:07:51 -07:00
func QueryGovProposals(f *cli.Fixtures, flags ...string) types.Proposals {
cmd := fmt.Sprintf("%s query gov proposals %v", f.SimdBinary, f.Flags())
2020-05-10 07:25:27 -07:00
stdout, stderr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
if strings.Contains(stderr, "no matching proposals found") {
2020-06-13 02:07:51 -07:00
return types.Proposals{}
2020-05-10 07:25:27 -07:00
}
require.Empty(f.T, stderr)
2020-06-13 02:07:51 -07:00
var out types.Proposals
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(stdout), &out)
require.NoError(f.T, err)
return out
}
// QueryGovVote is simcli query gov vote
2020-06-13 02:07:51 -07:00
func QueryGovVote(f *cli.Fixtures, proposalID int, voter sdk.AccAddress, flags ...string) types.Vote {
cmd := fmt.Sprintf("%s query gov vote %d %s %v", f.SimdBinary, proposalID, voter, f.Flags())
2020-05-10 07:25:27 -07:00
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
2020-06-13 02:07:51 -07:00
var vote types.Vote
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(out), &vote)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return vote
}
// QueryGovVotes is simcli query gov votes
2020-06-13 02:07:51 -07:00
func QueryGovVotes(f *cli.Fixtures, proposalID int, flags ...string) []types.Vote {
cmd := fmt.Sprintf("%s query gov votes %d %v", f.SimdBinary, proposalID, f.Flags())
2020-05-10 07:25:27 -07:00
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
2020-06-13 02:07:51 -07:00
var votes []types.Vote
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(out), &votes)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return votes
}
// QueryGovDeposit is simcli query gov deposit
2020-06-13 02:07:51 -07:00
func QueryGovDeposit(f *cli.Fixtures, proposalID int, depositor sdk.AccAddress, flags ...string) types.Deposit {
cmd := fmt.Sprintf("%s query gov deposit %d %s %v", f.SimdBinary, proposalID, depositor, f.Flags())
2020-05-10 07:25:27 -07:00
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
2020-06-13 02:07:51 -07:00
var deposit types.Deposit
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(out), &deposit)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return deposit
}
// QueryGovDeposits is simcli query gov deposits
2020-06-13 02:07:51 -07:00
func QueryGovDeposits(f *cli.Fixtures, propsalID int, flags ...string) []types.Deposit {
cmd := fmt.Sprintf("%s query gov deposits %d %v", f.SimdBinary, propsalID, f.Flags())
2020-05-10 07:25:27 -07:00
out, _ := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
2020-06-13 02:07:51 -07:00
var deposits []types.Deposit
2020-05-10 07:25:27 -07:00
err := f.Cdc.UnmarshalJSON([]byte(out), &deposits)
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return deposits
}
//___________________________________________________________________________________
// simcli tx gov
// TxGovSubmitProposal is simcli tx gov submit-proposal
func TxGovSubmitProposal(f *cli.Fixtures, from, typ, title, description string, deposit sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx gov submit-proposal %v --keyring-backend=test --from=%s --type=%s",
f.SimdBinary, f.Flags(), from, typ)
2020-05-10 07:25:27 -07:00
cmd += fmt.Sprintf(" --title=%s --description=%s --deposit=%s", title, description, deposit)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovDeposit is simcli tx gov deposit
func TxGovDeposit(f *cli.Fixtures, proposalID int, from string, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx gov deposit %d %s --keyring-backend=test --from=%s %v",
f.SimdBinary, proposalID, amount, from, f.Flags())
2020-05-10 07:25:27 -07:00
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovVote is simcli tx gov vote
2020-06-13 02:07:51 -07:00
func TxGovVote(f *cli.Fixtures, proposalID int, option types.VoteOption, from string, flags ...string) (bool, string, string) {
2020-05-10 07:25:27 -07:00
cmd := fmt.Sprintf("%s tx gov vote %d %s --keyring-backend=test --from=%s %v",
f.SimdBinary, proposalID, option, from, f.Flags())
2020-05-10 07:25:27 -07:00
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovSubmitParamChangeProposal executes a CLI parameter change proposal
// submission.
func TxGovSubmitParamChangeProposal(f *cli.Fixtures,
from, proposalPath string, deposit sdk.Coin, flags ...string,
) (bool, string, string) {
cmd := fmt.Sprintf(
"%s tx gov submit-proposal param-change %s --keyring-backend=test --from=%s %v",
f.SimdBinary, proposalPath, from, f.Flags(),
2020-05-10 07:25:27 -07:00
)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxGovSubmitCommunityPoolSpendProposal executes a CLI community pool spend proposal
// submission.
func TxGovSubmitCommunityPoolSpendProposal(f *cli.Fixtures,
from, proposalPath string, deposit sdk.Coin, flags ...string,
) (bool, string, string) {
cmd := fmt.Sprintf(
"%s tx gov submit-proposal community-pool-spend %s --keyring-backend=test --from=%s %v",
f.SimdBinary, proposalPath, from, f.Flags(),
2020-05-10 07:25:27 -07:00
)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}