Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

voting_info.gno

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