Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

proposal_vote_status.gno

2.08 Kb · 71 lines
 1package governance
 2
 3import (
 4	gnsmath "gno.land/p/gnoswap/gnsmath/v1"
 5
 6	"gno.land/r/gnoswap/gov/governance"
 7)
 8
 9type ProposalVoteStatusResolver struct {
10	*governance.ProposalVoteStatus
11}
12
13// NewProposalVoteStatusResolver wraps persisted vote status for vote tally
14// calculations and mutation.
15//
16// Parameters:
17//   - voteStatus: persisted proposal vote status to resolve
18//
19// Returns:
20//   - *ProposalVoteStatusResolver: resolver backed by voteStatus
21func NewProposalVoteStatusResolver(voteStatus *governance.ProposalVoteStatus) *ProposalVoteStatusResolver {
22	return &ProposalVoteStatusResolver{voteStatus}
23}
24
25// TotalVoteWeight returns the total weight of all votes cast (yes + no).
26//
27// Returns:
28//   - int64: combined "yes" and "no" vote weight
29func (p *ProposalVoteStatusResolver) TotalVoteWeight() int64 {
30	return gnsmath.SafeAddInt64(p.YesWeight(), p.NoWeight())
31}
32
33// IsPassed determines if the proposal has passed the voting requirements.
34// A proposal passes when total vote weight reaches quorum and "yes" votes
35// strictly exceed "no" votes.
36//
37// Returns:
38//   - bool: true when quorum is met and yes weight is greater than no weight
39func (p *ProposalVoteStatusResolver) IsPassed() bool {
40	if p.TotalVoteWeight() < p.QuorumAmount() {
41		return false
42	}
43
44	return p.YesWeight() > p.NoWeight()
45}
46
47// addYesVoteWeight adds the specified weight to the "yes" vote tally.
48// This is called when a user votes "yes" on the proposal.
49//
50// Parameters:
51//   - yea: vote weight to add to "yes" votes
52//
53// Returns:
54//   - error: always nil (reserved for future validation)
55func (p *ProposalVoteStatusResolver) AddYesVoteWeight(yea int64) error {
56	p.SetYesWeight(gnsmath.SafeAddInt64(p.YesWeight(), yea))
57	return nil
58}
59
60// addNoVoteWeight adds the specified weight to the "no" vote tally.
61// This is called when a user votes "no" on the proposal.
62//
63// Parameters:
64//   - nay: vote weight to add to "no" votes
65//
66// Returns:
67//   - error: always nil (reserved for future validation)
68func (p *ProposalVoteStatusResolver) AddNoVoteWeight(nay int64) error {
69	p.SetNoWeight(gnsmath.SafeAddInt64(p.NoWeight(), nay))
70	return nil
71}