voting_info.gno
2.15 Kb ยท 70 lines
1package v1
2
3import (
4 "gno.land/r/gnoswap/gov/governance"
5)
6
7type VotingInfoResolver struct {
8 *governance.VotingInfo
9}
10
11func NewVotingInfoResolver(votingInfo *governance.VotingInfo) *VotingInfoResolver {
12 return &VotingInfoResolver{
13 VotingInfo: votingInfo,
14 }
15}
16
17// voteYes records a "yes" vote with the specified weight and timing information.
18// This is an internal helper method that delegates to the main vote function.
19//
20// Parameters:
21// - weight: voting weight to use for this vote
22// - votedHeight: block height when vote is cast
23// - votedAt: timestamp when vote is cast
24//
25// Returns:
26// - error: voting error if vote cannot be recorded
27func (v *VotingInfoResolver) voteYes(weight int64, votedHeight int64, votedAt int64) error {
28 return v.vote(true, weight, votedHeight, votedAt)
29}
30
31// voteNo records a "no" vote with the specified weight and timing information.
32// This is an internal helper method that delegates to the main vote function.
33//
34// Parameters:
35// - weight: voting weight to use for this vote
36// - votedHeight: block height when vote is cast
37// - votedAt: timestamp when vote is cast
38//
39// Returns:
40// - error: voting error if vote cannot be recorded
41func (v *VotingInfoResolver) voteNo(weight int64, votedHeight int64, votedAt int64) error {
42 return v.vote(false, weight, votedHeight, votedAt)
43}
44
45// vote records a vote with the specified choice, weight, and timing information.
46// This is the core voting method that prevents double voting and records all vote details.
47//
48// Parameters:
49// - votedYes: true for "yes" vote, false for "no" vote
50// - weight: voting weight to use for this vote
51// - votedHeight: block height when vote is cast
52// - votedAt: timestamp when vote is cast
53//
54// Returns:
55// - error: voting error if user has already voted
56func (v *VotingInfoResolver) vote(votedYes bool, weight int64, votedHeight int64, votedAt int64) error {
57 // Prevent double voting - each user can only vote once per proposal
58 if v.IsVoted() {
59 return errAlreadyVoted
60 }
61
62 // Record all voting details
63 v.SetVotedWeight(weight)
64 v.SetVotedHeight(votedHeight)
65 v.SetVotedAt(votedAt)
66 v.SetVoted(true)
67 v.SetVotedYes(votedYes)
68
69 return nil
70}