diff --git a/gov/deposit.go b/gov/deposit.go new file mode 100644 index 0000000..b604234 --- /dev/null +++ b/gov/deposit.go @@ -0,0 +1,27 @@ +package gov + +import ( + "fmt" + "strconv" + + "github.com/MalteHerrmann/upgrade-local-node-go/utils" + "github.com/pkg/errors" +) + +// DepositForProposal deposits the given amount of Evmos for the proposal with the given proposalID +// from the given account. +func DepositForProposal(bin *utils.Binary, proposalID int, sender string, amount int) (string, error) { + out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{ + Subcommand: []string{ + "tx", "gov", "deposit", strconv.Itoa(proposalID), strconv.Itoa(amount) + "aevmos", + }, + From: sender, + UseDefaults: true, + Quiet: true, + }) + if err != nil { + return out, errors.Wrap(err, fmt.Sprintf("failed to deposit for proposal %d", proposalID)) + } + + return out, nil +} diff --git a/gov/proposal.go b/gov/proposal.go index 2fef680..5dfbcf2 100644 --- a/gov/proposal.go +++ b/gov/proposal.go @@ -2,7 +2,6 @@ package gov import ( "fmt" - "log" "strconv" "strings" @@ -78,42 +77,6 @@ func QueryLatestProposalID(bin *utils.Binary) (int, error) { return int(res.Proposals[len(res.Proposals)-1].Id), nil } -// SubmitAllVotesForProposal submits a vote for the given proposal ID using all testing accounts. -func SubmitAllVotesForProposal(bin *utils.Binary, proposalID int) error { - accsWithDelegations, err := utils.FilterAccountsWithDelegations(bin) - if err != nil { - return errors.Wrap(err, "Error filtering accounts") - } - - if len(accsWithDelegations) == 0 { - return errors.New("No accounts with delegations found") - } - - utils.Wait(1) - log.Printf("Voting for proposal %d...\n", proposalID) - - var out string - - for _, acc := range accsWithDelegations { - out, err = VoteForProposal(bin, proposalID, acc.Name) - if err != nil { - if strings.Contains(out, fmt.Sprintf("%d: unknown proposal", proposalID)) { - return fmt.Errorf("no proposal with ID %d found", proposalID) - } - - if strings.Contains(out, fmt.Sprintf("%d: inactive proposal", proposalID)) { - return fmt.Errorf("proposal with ID %d is inactive", proposalID) - } - - log.Printf(" - could NOT vote using key: %s\n", acc.Name) - } else { - log.Printf(" - voted using key: %s\n", acc.Name) - } - } - - return nil -} - // SubmitUpgradeProposal submits a software upgrade proposal with the given target version and upgrade height. func SubmitUpgradeProposal(bin *utils.Binary, targetVersion string, upgradeHeight int) (int, error) { upgradeProposal := buildUpgradeProposalCommand(targetVersion, upgradeHeight) @@ -145,37 +108,3 @@ func SubmitUpgradeProposal(bin *utils.Binary, targetVersion string, upgradeHeigh return GetProposalIDFromSubmitEvents(events) } - -// VoteForProposal votes for the proposal with the given ID using the given account. -func VoteForProposal(bin *utils.Binary, proposalID int, sender string) (string, error) { - out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{ - Subcommand: []string{"tx", "gov", "vote", strconv.Itoa(proposalID), "yes"}, - From: sender, - UseDefaults: true, - Quiet: true, - }) - if err != nil { - return out, errors.Wrap(err, fmt.Sprintf("failed to vote for proposal %d", proposalID)) - } - - return out, nil -} - -// DepositForProposal deposits the given amount of Evmos for the proposal with the given proposalID -// from the given account. -func DepositForProposal(bin *utils.Binary, proposalID int, sender string, amount int) (string, error) { - out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{ - Subcommand: []string{ - "tx", "gov", "deposit", strconv.Itoa(proposalID), strconv.Itoa(amount) + "aevmos", - }, - From: sender, - UseDefaults: true, - Quiet: true, - }) - - if err != nil { - return out, errors.Wrap(err, fmt.Sprintf("failed to deposit for proposal %d", proposalID)) - } - - return out, nil -} diff --git a/gov/vote.go b/gov/vote.go new file mode 100644 index 0000000..b7e5310 --- /dev/null +++ b/gov/vote.go @@ -0,0 +1,62 @@ +package gov + +import ( + "fmt" + "log" + "strconv" + "strings" + + "github.com/MalteHerrmann/upgrade-local-node-go/utils" + "github.com/pkg/errors" +) + +// SubmitAllVotesForProposal submits a vote for the given proposal ID using all testing accounts. +func SubmitAllVotesForProposal(bin *utils.Binary, proposalID int) error { + accsWithDelegations, err := utils.FilterAccountsWithDelegations(bin) + if err != nil { + return errors.Wrap(err, "error filtering accounts") + } + + if len(accsWithDelegations) == 0 { + return errors.New("no accounts with delegations found") + } + + utils.Wait(1) + log.Printf("Voting for proposal %d...\n", proposalID) + + var out string + + for _, acc := range accsWithDelegations { + out, err = VoteForProposal(bin, proposalID, acc.Name) + if err != nil { + if strings.Contains(out, fmt.Sprintf("%d: unknown proposal", proposalID)) { + return fmt.Errorf("no proposal with ID %d found", proposalID) + } + + if strings.Contains(out, fmt.Sprintf("%d: inactive proposal", proposalID)) { + return fmt.Errorf("proposal with ID %d is inactive", proposalID) + } + + log.Printf(" - could NOT vote using key: %s\n", acc.Name) + } else { + log.Printf(" - voted using key: %s\n", acc.Name) + } + } + + return nil +} + +// VoteForProposal votes for the proposal with the given ID using the given account. +func VoteForProposal(bin *utils.Binary, proposalID int, sender string) (string, error) { + out, err := utils.ExecuteBinaryCmd(bin, utils.BinaryCmdArgs{ + Subcommand: []string{"tx", "gov", "vote", strconv.Itoa(proposalID), "yes"}, + From: sender, + UseDefaults: true, + Quiet: true, + }) + if err != nil { + return out, errors.Wrap(err, fmt.Sprintf("failed to vote for proposal %d", proposalID)) + } + + return out, nil +} diff --git a/main.go b/main.go index b59edda..ac45d02 100644 --- a/main.go +++ b/main.go @@ -27,12 +27,11 @@ func main() { bin, err := utils.NewEvmosTestingBinary() if err != nil { - log.Fatalf("Error creating binary: %v", err) + log.Fatalf("error creating binary: %v", err) } - err = bin.GetAccounts() - if err != nil { - log.Fatalf("Error getting accounts: %v", err) + if err = bin.GetAccounts(); err != nil { + log.Fatalf("error getting accounts: %v", err) } // TODO: use with Cobra CLI @@ -40,34 +39,32 @@ func main() { case "vote": proposalID, err := getProposalIDFromInput(bin, os.Args) if err != nil { - log.Fatalf("Error getting proposal ID: %v", err) + log.Fatalf("error getting proposal ID: %v", err) } - err = gov.SubmitAllVotesForProposal(bin, proposalID) - if err != nil { - log.Fatalf("Error submitting votes for proposal %d: %v", proposalID, err) + if err = gov.SubmitAllVotesForProposal(bin, proposalID); err != nil { + log.Fatalf("error submitting votes for proposal %d: %v", proposalID, err) } case "deposit": proposalID, err := getProposalIDFromInput(bin, os.Args) if err != nil { - log.Fatalf("Error getting proposal ID: %v", err) + log.Fatalf("error getting proposal ID: %v", err) } // TODO: replace fixed amount with min deposit from chain params if _, err = gov.DepositForProposal(bin, proposalID, bin.Accounts[0].Name, 1e9); err != nil { - log.Fatalf("Error depositing for proposal %d: %v", proposalID, err) + log.Fatalf("error depositing for proposal %d: %v", proposalID, err) } default: targetVersion := os.Args[1] if matched, _ := regexp.MatchString(`v\d+\.\d+\.\d(-rc\d+)?`, targetVersion); !matched { - log.Fatalf("Invalid target version: %s. Please use the format vX.Y.Z(-rc*).\n", targetVersion) + log.Fatalf("invalid target version: %s; please use the format vX.Y.Z(-rc*).\n", targetVersion) } - err := upgradeLocalNode(bin, targetVersion) - if err != nil { - log.Fatalf("Error upgrading local node: %v", err) + if err := upgradeLocalNode(bin, targetVersion); err != nil { + log.Fatalf("error upgrading local node: %v", err) } } } @@ -83,15 +80,15 @@ func getProposalIDFromInput(bin *utils.Binary, args []string) (int, error) { case 2: proposalID, err = gov.QueryLatestProposalID(bin) if err != nil { - return 0, errors.Wrap(err, "Error querying latest proposal ID") + return 0, errors.Wrap(err, "error querying latest proposal ID") } case 3: proposalID, err = strconv.Atoi(args[2]) if err != nil { - return 0, errors.Wrapf(err, "Error converting proposal ID %s to integer", args[2]) + return 0, errors.Wrapf(err, "error converting proposal ID %s to integer", args[2]) } default: - return 0, errors.New("Invalid number of arguments") + return 0, errors.New("invalid number of arguments") } return proposalID, nil @@ -102,7 +99,7 @@ func getProposalIDFromInput(bin *utils.Binary, args []string) (int, error) { func upgradeLocalNode(bin *utils.Binary, targetVersion string) error { currentHeight, err := utils.GetCurrentHeight(bin) if err != nil { - return errors.Wrap(err, "Error getting current height") + return errors.Wrap(err, "error getting current height") } upgradeHeight := currentHeight + deltaHeight @@ -111,14 +108,13 @@ func upgradeLocalNode(bin *utils.Binary, targetVersion string) error { proposalID, err := gov.SubmitUpgradeProposal(bin, targetVersion, upgradeHeight) if err != nil { - return errors.Wrap(err, "Error executing upgrade proposal") + return errors.Wrap(err, "error executing upgrade proposal") } log.Printf("Scheduled upgrade to %s at height %d.\n", targetVersion, upgradeHeight) - err = gov.SubmitAllVotesForProposal(bin, proposalID) - if err != nil { - return errors.Wrapf(err, "Error submitting votes for proposal %d", proposalID) + if err = gov.SubmitAllVotesForProposal(bin, proposalID); err != nil { + return errors.Wrapf(err, "error submitting votes for proposal %d", proposalID) } return nil