github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/groups/vote_set.gno (about) 1 package groups 2 3 import ( 4 "std" 5 "time" 6 7 "gno.land/p/demo/rat" 8 ) 9 10 //---------------------------------------- 11 // VoteSet 12 13 type VoteSet interface { 14 // number of present votes in set. 15 Size() int 16 // add or update vote for voter. 17 SetVote(voter std.Address, value string) error 18 // count the number of votes for value. 19 CountVotes(value string) int 20 } 21 22 //---------------------------------------- 23 // VoteList 24 25 type Vote struct { 26 Voter std.Address 27 Value string 28 } 29 30 type VoteList []Vote 31 32 func NewVoteList() *VoteList { 33 return &VoteList{} 34 } 35 36 func (vlist *VoteList) Size() int { 37 return len(*vlist) 38 } 39 40 func (vlist *VoteList) SetVote(voter std.Address, value string) error { 41 // TODO optimize with binary algorithm 42 for i, vote := range *vlist { 43 if vote.Voter == voter { 44 // update vote 45 (*vlist)[i] = Vote{ 46 Voter: voter, 47 Value: value, 48 } 49 return nil 50 } 51 } 52 *vlist = append(*vlist, Vote{ 53 Voter: voter, 54 Value: value, 55 }) 56 return nil 57 } 58 59 func (vlist *VoteList) CountVotes(target string) int { 60 // TODO optimize with binary algorithm 61 var count int 62 for _, vote := range *vlist { 63 if vote.Value == target { 64 count++ 65 } 66 } 67 return count 68 } 69 70 //---------------------------------------- 71 // Committee 72 73 type Committee struct { 74 Quorum rat.Rat 75 Threshold rat.Rat 76 Addresses std.AddressSet 77 } 78 79 //---------------------------------------- 80 // VoteSession 81 // NOTE: this seems a bit too formal and 82 // complicated vs what might be possible; 83 // something simpler, more informal. 84 85 type SessionStatus int 86 87 const ( 88 SessionNew SessionStatus = iota 89 SessionStarted 90 SessionCompleted 91 SessionCanceled 92 ) 93 94 type VoteSession struct { 95 Name string 96 Creator std.Address 97 Body string 98 Start time.Time 99 Deadline time.Time 100 Status SessionStatus 101 Committee *Committee 102 Votes VoteSet 103 Choices []string 104 Result string 105 }