github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/docs/assets/how-to-guides/write-simple-dapp/poll-1.gno (about) 1 package poll 2 3 import ( 4 "std" 5 6 "gno.land/p/demo/avl" 7 ) 8 9 // Main struct 10 type Poll struct { 11 title string 12 description string 13 deadline int64 // block height 14 voters *avl.Tree // addr -> yes / no (bool) 15 } 16 17 // Getters 18 func (p Poll) Title() string { 19 return p.title 20 } 21 22 func (p Poll) Description() string { 23 return p.description 24 } 25 26 func (p Poll) Deadline() int64 { 27 return p.deadline 28 } 29 30 func (p Poll) Voters() *avl.Tree { 31 return p.voters 32 } 33 34 // Poll instance constructor 35 func NewPoll(title, description string, deadline int64) *Poll { 36 return &Poll{ 37 title: title, 38 description: description, 39 deadline: deadline, 40 voters: avl.NewTree(), 41 } 42 } 43 44 // Vote Votes for a user 45 func (p *Poll) Vote(voter std.Address, vote bool) { 46 p.Voters().Set(voter.String(), vote) 47 } 48 49 // HasVoted vote: yes - true, no - false 50 func (p *Poll) HasVoted(address std.Address) (bool, bool) { 51 vote, exists := p.Voters().Get(address.String()) 52 if exists { 53 return true, vote.(bool) 54 } 55 return false, false 56 } 57 58 // VoteCount Returns the number of yay & nay votes 59 func (p Poll) VoteCount() (int, int) { 60 var yay int 61 62 p.Voters().Iterate("", "", func(key string, value interface{}) bool { 63 vote := value.(bool) 64 if vote == true { 65 yay = yay + 1 66 } 67 }) 68 return yay, p.Voters().Size() - yay 69 }