-
Notifications
You must be signed in to change notification settings - Fork 105
/
vote-proposal.go
168 lines (141 loc) · 4.06 KB
/
vote-proposal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package odao
import (
"bytes"
"fmt"
"strconv"
"github.com/rocket-pool/rocketpool-go/dao"
"github.com/rocket-pool/rocketpool-go/types"
"github.com/urfave/cli"
"github.com/rocket-pool/smartnode/shared/services/gas"
"github.com/rocket-pool/smartnode/shared/services/rocketpool"
cliutils "github.com/rocket-pool/smartnode/shared/utils/cli"
)
func voteOnProposal(c *cli.Context) error {
// Get RP client
rp, err := rocketpool.NewClientFromCtx(c).WithReady()
if err != nil {
return err
}
defer rp.Close()
// Get oracle DAO proposals
proposals, err := rp.TNDAOProposals()
if err != nil {
return err
}
// Get oracle DAO members
allMembers, err := rp.TNDAOMembers()
if err != nil {
return err
}
// Get votable proposals
votableProposals := []dao.ProposalDetails{}
for _, proposal := range proposals.Proposals {
if proposal.State == types.Active && !proposal.MemberVoted {
votableProposals = append(votableProposals, proposal)
}
}
// Check for votable proposals
if len(votableProposals) == 0 {
fmt.Println("No proposals can be voted on.")
return nil
}
// Get selected proposal
var selectedProposal dao.ProposalDetails
if c.String("proposal") != "" {
// Get selected proposal ID
selectedId, err := strconv.ParseUint(c.String("proposal"), 10, 64)
if err != nil {
return fmt.Errorf("Invalid proposal ID '%s': %w", c.String("proposal"), err)
}
// Get matching proposal
found := false
for _, proposal := range votableProposals {
if proposal.ID == selectedId {
selectedProposal = proposal
found = true
break
}
}
if !found {
return fmt.Errorf("Proposal %d can not be voted on.", selectedId)
}
} else {
// Prompt for proposal selection
var memberID string
options := make([]string, len(votableProposals))
for pi, proposal := range votableProposals {
for _, member := range allMembers.Members {
if bytes.Equal(proposal.ProposerAddress.Bytes(), member.Address.Bytes()) {
memberID = member.ID
}
}
options[pi] = fmt.Sprintf(
"proposal %d (message: '%s', payload: %s, end time: %s, votes required: %.2f, votes for: %.2f, votes against: %.2f, proposed by: %s (%s))",
proposal.ID,
proposal.Message,
proposal.PayloadStr,
cliutils.GetDateTimeString(proposal.EndTime),
proposal.VotesRequired,
proposal.VotesFor,
proposal.VotesAgainst,
memberID,
proposal.ProposerAddress)
}
selected, _ := cliutils.Select("Please select a proposal to vote on:", options)
selectedProposal = votableProposals[selected]
}
// Get support status
var support bool
var supportLabel string
if c.String("support") != "" {
// Parse support status
var err error
support, err = cliutils.ValidateBool("support", c.String("support"))
if err != nil {
return err
}
} else {
// Prompt for support status
support = cliutils.Confirm("Would you like to vote in support of the proposal?")
}
if support {
supportLabel = "in support of"
} else {
supportLabel = "against"
}
// Check if proposal can be voted on
canVote, err := rp.CanVoteOnTNDAOProposal(selectedProposal.ID)
if err != nil {
return err
}
if !canVote.CanVote {
fmt.Println("Cannot vote on proposal:")
if canVote.JoinedAfterCreated {
fmt.Println("You cannot vote on proposals created before you joined the oracle DAO.")
}
return nil
}
// Assign max fees
err = gas.AssignMaxFeeAndLimit(canVote.GasInfo, rp, c.Bool("yes"))
if err != nil {
return err
}
// Prompt for confirmation
if !(c.Bool("yes") || cliutils.Confirm(fmt.Sprintf("Are you sure you want to vote %s proposal %d? Your vote cannot be changed later.", supportLabel, selectedProposal.ID))) {
fmt.Println("Cancelled.")
return nil
}
// Vote on proposal
response, err := rp.VoteOnTNDAOProposal(selectedProposal.ID, support)
if err != nil {
return err
}
fmt.Printf("Submitting vote...\n")
cliutils.PrintTransactionHash(rp, response.TxHash)
if _, err = rp.WaitForTransaction(response.TxHash); err != nil {
return err
}
// Log & return
fmt.Printf("Successfully voted %s proposal %d.\n", supportLabel, selectedProposal.ID)
return nil
}