1package govdao
2
3import (
4 "errors"
5 "std"
6
7 "gno.land/p/demo/combinederr"
8 "gno.land/p/demo/dao"
9 "gno.land/p/demo/membstore"
10 "gno.land/p/gov/executor"
11)
12
13var errNoChangesProposed = errors.New("no set changes proposed")
14
15// NewGovDAOExecutor creates the govdao wrapped callback executor
16func NewGovDAOExecutor(cb func() error) dao.Executor {
17 if cb == nil {
18 panic(errNoChangesProposed)
19 }
20
21 return executor.NewCallbackExecutor(
22 cb,
23 std.CurrentRealm().PkgPath(),
24 )
25}
26
27// NewMemberPropExecutor returns the GOVDAO member change executor
28func NewMemberPropExecutor(changesFn func() []membstore.Member) dao.Executor {
29 if changesFn == nil {
30 panic(errNoChangesProposed)
31 }
32
33 callback := func() error {
34 errs := &combinederr.CombinedError{}
35 cbMembers := changesFn()
36
37 for _, member := range cbMembers {
38 switch {
39 case !members.IsMember(member.Address):
40 // Addition request
41 err := members.AddMember(member)
42
43 errs.Add(err)
44 case member.VotingPower == 0:
45 // Remove request
46 err := members.UpdateMember(member.Address, membstore.Member{
47 Address: member.Address,
48 VotingPower: 0, // 0 indicated removal
49 })
50
51 errs.Add(err)
52 default:
53 // Update request
54 err := members.UpdateMember(member.Address, member)
55
56 errs.Add(err)
57 }
58 }
59
60 // Check if there were any execution errors
61 if errs.Size() == 0 {
62 return nil
63 }
64
65 return errs
66 }
67
68 return NewGovDAOExecutor(callback)
69}
70
71func NewMembStoreImplExecutor(changeFn func() membstore.MemberStore) dao.Executor {
72 if changeFn == nil {
73 panic(errNoChangesProposed)
74 }
75
76 callback := func() error {
77 setMembStoreImpl(changeFn())
78
79 return nil
80 }
81
82 return NewGovDAOExecutor(callback)
83}
84
85// setMembStoreImpl sets a new dao.MembStore implementation
86func setMembStoreImpl(impl membstore.MemberStore) {
87 if impl == nil {
88 panic("invalid member store")
89 }
90
91 members = impl
92}
poc.gno
1.83 Kb ยท 92 lines