Search Apps Documentation Source Content File Folder Download Copy Actions Download

z_commondao_execute_3_filetest.gno

1.82 Kb ยท 71 lines
 1// PKGPATH: gno.land/r/test
 2package test
 3
 4import (
 5	"errors"
 6	"time"
 7
 8	"gno.land/p/nt/commondao"
 9)
10
11const (
12	member    address = "g16jpf0puufcpcjkph5nxueec8etpcldz7zwgydq" // @devxZ
13	groupName         = "admin"
14)
15
16var (
17	dao      *commondao.CommonDAO
18	proposal *commondao.Proposal
19)
20
21type testPropDef struct{}
22
23func (testPropDef) Title() string               { return "" }
24func (testPropDef) Body() string                { return "" }
25func (testPropDef) VotingPeriod() time.Duration { return 0 }
26func (testPropDef) Execute(cur realm) error     { return nil }
27
28// Tally only succeeds if one YES vote from an admin is found
29func (testPropDef) Tally(ctx commondao.VotingContext) (bool, error) {
30	if ctx.VotingRecord.Size() < 1 {
31		return false, errors.New("proposal requires at least 1 YES vote from an admin to pass")
32	}
33
34	admins, _ := ctx.Members.Grouping().Get(groupName)
35	return admins.Members().IterateByOffset(0, admins.Members().Size(), func(member address) bool {
36		v, found := ctx.VotingRecord.GetVote(member)
37		// Proposal passes when a YES vote is found
38		return (found && v.Choice == commondao.ChoiceYes)
39	}), nil
40}
41
42func init() {
43	// Create a DAO with member grouping support and a proposal
44	storage := commondao.NewMemberStorageWithGrouping()
45	dao = commondao.New(commondao.WithMemberStorage(storage))
46	proposal = dao.MustPropose(member, testPropDef{})
47
48	// Add a single group with members
49	admins, _ := storage.Grouping().Add(groupName)
50	admins.Members().Add(member)
51	admins.Members().Add("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
52
53	// Add a vote to the voting record
54	proposal.VotingRecord().AddVote(commondao.Vote{
55		Address: member,
56		Choice:  commondao.ChoiceYes,
57	})
58}
59
60func main() {
61	err := dao.Execute(proposal.ID())
62	if err != nil {
63		panic(err)
64	}
65
66	println(string(proposal.Status()))
67	println(proposal.StatusReason())
68}
69
70// Output:
71// executed