Search Apps Documentation Source Content File Folder Download Copy Actions Download

voting_info.gno

3.99 Kb ยท 155 lines
  1package governance
  2
  3// VotingInfo tracks voting-related information for a specific user on a specific proposal.
  4// This structure maintains the user's voting eligibility, voting history, and voting power.
  5type VotingInfo struct {
  6	availableVoteWeight int64 // Total voting weight available to this user for this proposal
  7	votedWeight         int64 // Actual weight used when voting (0 if not voted)
  8	votedHeight         int64 // Block height when vote was cast
  9	votedAt             int64 // Timestamp when vote was cast
 10	votedYes            bool  // True if voted "yes", false if voted "no"
 11	voted               bool  // True if user has already voted
 12}
 13
 14// NewVotingInfo creates a new voting information structure for a user.
 15// This constructor initializes the voting eligibility based on delegation snapshots.
 16//
 17// Parameters:
 18//   - availableVoteWeight: total voting weight available to this user
 19//
 20// Returns:
 21//   - *VotingInfo: newly created voting information structure
 22func NewVotingInfo(availableVoteWeight int64) *VotingInfo {
 23	return &VotingInfo{
 24		availableVoteWeight: availableVoteWeight,
 25		// Other fields are initialized to zero values (false, 0)
 26		// voted starts as false, indicating no vote has been cast
 27	}
 28}
 29
 30// VotingType returns a human-readable string representation of the vote choice.
 31//
 32// Returns:
 33//   - string: "yes" or "no" based on voting choice
 34func (v *VotingInfo) VotingType() string {
 35	if v.votedYes {
 36		return "yes"
 37	}
 38
 39	return "no"
 40}
 41
 42// IsVoted checks if the user has already cast their vote.
 43//
 44// Returns:
 45//   - bool: true if user has voted on this proposal
 46func (v *VotingInfo) IsVoted() bool {
 47	return v.voted
 48}
 49
 50// VotedYes checks if the user voted "yes" on the proposal.
 51// Only meaningful if IsVoted() returns true.
 52//
 53// Returns:
 54//   - bool: true if user voted "yes"
 55func (v *VotingInfo) VotedYes() bool {
 56	return v.votedYes
 57}
 58
 59// VotedNo checks if the user voted "no" on the proposal.
 60// Only meaningful if IsVoted() returns true.
 61//
 62// Returns:
 63//   - bool: true if user voted "no"
 64func (v *VotingInfo) VotedNo() bool {
 65	return !v.votedYes
 66}
 67
 68// AvailableVoteWeight returns the total voting weight available to this user.
 69// This weight is determined at proposal creation time based on delegation snapshots.
 70//
 71// Returns:
 72//   - int64: available voting weight
 73func (v *VotingInfo) AvailableVoteWeight() int64 {
 74	return v.availableVoteWeight
 75}
 76
 77// VotedWeight returns the weight actually used when voting.
 78// Returns 0 if the user hasn't voted yet.
 79//
 80// Returns:
 81//   - int64: weight used for voting, or 0 if not voted
 82func (v *VotingInfo) VotedWeight() int64 {
 83	if !v.voted {
 84		return 0
 85	}
 86
 87	return v.votedWeight
 88}
 89
 90// VotedHeight returns the block height when the vote was cast.
 91// Returns 0 if the user hasn't voted yet.
 92//
 93// Returns:
 94//   - int64: block height when vote was cast
 95func (v *VotingInfo) VotedHeight() int64 {
 96	if !v.voted {
 97		return 0
 98	}
 99
100	return v.votedHeight
101}
102
103// VotedAt returns the timestamp when the vote was cast.
104// Returns 0 if the user hasn't voted yet.
105//
106// Returns:
107//   - int64: timestamp when vote was cast
108func (v *VotingInfo) VotedAt() int64 {
109	if !v.voted {
110		return 0
111	}
112
113	return v.votedAt
114}
115
116/* Setter methods */
117func (v *VotingInfo) SetAvailableVoteWeight(availableVoteWeight int64) {
118	v.availableVoteWeight = availableVoteWeight
119}
120
121func (v *VotingInfo) SetVotedWeight(votedWeight int64) {
122	v.votedWeight = votedWeight
123}
124
125func (v *VotingInfo) SetVotedHeight(votedHeight int64) {
126	v.votedHeight = votedHeight
127}
128
129func (v *VotingInfo) SetVotedAt(votedAt int64) {
130	v.votedAt = votedAt
131}
132
133func (v *VotingInfo) SetVotedYes(votedYes bool) {
134	v.votedYes = votedYes
135}
136
137func (v *VotingInfo) SetVoted(voted bool) {
138	v.voted = voted
139}
140
141// Clone creates a deep copy of the VotingInfo.
142func (v *VotingInfo) Clone() *VotingInfo {
143	if v == nil {
144		return nil
145	}
146
147	return &VotingInfo{
148		availableVoteWeight: v.availableVoteWeight,
149		votedWeight:         v.votedWeight,
150		votedHeight:         v.votedHeight,
151		votedAt:             v.votedAt,
152		votedYes:            v.votedYes,
153		voted:               v.voted,
154	}
155}