proposal_vote_status.gno
3.17 Kb ยท 104 lines
1package v1
2
3import (
4 "gno.land/r/gnoswap/gov/governance"
5)
6
7type ProposalVoteStatusResolver struct {
8 *governance.ProposalVoteStatus
9}
10
11func NewProposalVoteStatusResolver(voteStatus *governance.ProposalVoteStatus) *ProposalVoteStatusResolver {
12 return &ProposalVoteStatusResolver{voteStatus}
13}
14
15// TotalVoteWeight returns the total weight of all votes cast (yes + no).
16//
17// Returns:
18// - int64: combined weight of all votes
19func (p *ProposalVoteStatusResolver) TotalVoteWeight() int64 {
20 return safeAddInt64(p.YesWeight(), p.NoWeight())
21}
22
23// DiffVoteWeight returns the absolute difference between yes and no votes.
24// This can be used to determine the margin of victory or defeat.
25//
26// Returns:
27// - int64: absolute difference between yes and no vote weights
28func (p *ProposalVoteStatusResolver) DiffVoteWeight() int64 {
29 yes := p.YesWeight()
30 no := p.NoWeight()
31 if yes > no {
32 return safeSubInt64(yes, no)
33 }
34
35 return safeSubInt64(no, yes)
36}
37
38// IsVotingFinished determines if voting has effectively ended due to mathematical impossibility
39// of changing the outcome. This happens when the remaining uncast votes cannot change the result.
40//
41// Returns:
42// - bool: true if voting outcome is mathematically determined
43func (p *ProposalVoteStatusResolver) IsVotingFinished() bool {
44 totalVotes := p.TotalVoteWeight()
45
46 // If we haven't reached quorum yet, voting is not finished
47 if totalVotes < p.QuorumAmount() {
48 return false
49 }
50
51 // Calculate remaining votes that could still be cast
52 remainingVotes := safeSubInt64(p.MaxVotingWeight(), totalVotes)
53
54 // If the difference between yes/no is greater than remaining votes,
55 // the outcome cannot change, so voting is effectively finished
56 return safeSubInt64(remainingVotes, p.DiffVoteWeight()) <= 0
57}
58
59// IsRejected determines if the proposal has been rejected by voting.
60// A proposal is rejected if voting is finished and it did not pass.
61//
62// Returns:
63// - bool: true if proposal has been rejected
64func (p *ProposalVoteStatusResolver) IsRejected() bool {
65 // Only consider rejection if voting is finished
66 if !p.IsVotingFinished() {
67 return false
68 }
69
70 // Proposal is rejected if it didn't pass
71 return !p.IsPassed()
72}
73
74// IsPassed determines if the proposal has passed the voting requirements.
75// A proposal passes if it receives at least the quorum amount of "yes" votes.
76func (p *ProposalVoteStatusResolver) IsPassed() bool {
77 return p.YesWeight() >= p.QuorumAmount()
78}
79
80// addYesVoteWeight adds the specified weight to the "yes" vote tally.
81// This is called when a user votes "yes" on the proposal.
82//
83// Parameters:
84// - yea: vote weight to add to "yes" votes
85//
86// Returns:
87// - error: always nil (reserved for future validation)
88func (p *ProposalVoteStatusResolver) AddYesVoteWeight(yea int64) error {
89 p.SetYesWeight(safeAddInt64(p.YesWeight(), yea))
90 return nil
91}
92
93// addNoVoteWeight adds the specified weight to the "no" vote tally.
94// This is called when a user votes "no" on the proposal.
95//
96// Parameters:
97// - nay: vote weight to add to "no" votes
98//
99// Returns:
100// - error: always nil (reserved for future validation)
101func (p *ProposalVoteStatusResolver) AddNoVoteWeight(nay int64) error {
102 p.SetNoWeight(safeAddInt64(p.NoWeight(), nay))
103 return nil
104}