Skip to content

Commit

Permalink
feat(gno/dao): naive way DAO
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelVallenet committed Jan 8, 2025
1 parent d38ef14 commit 9933aed
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
39 changes: 38 additions & 1 deletion gno/p/dao2/dao2.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package dao2

import (
"std"
"strings"

"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
"gno.land/p/teritori/daocond"
"gno.land/p/teritori/role_manager"
)

Expand All @@ -16,7 +19,7 @@ type Dao struct {

func NewDao() *Dao {
return &Dao{
roleManager: role_manager.NewWithAddress(std.CurrentRealm().Addr()),
roleManager: role_manager.NewWithAddress(std.PrevRealm().Addr()),
members: avl.NewTree(),
resources: avl.NewTree(),
}
Expand Down Expand Up @@ -52,3 +55,37 @@ func (d *Dao) IsMemberFn(memberId string) bool {
func (d *Dao) MembersCountFn() uint64 {
return uint64(d.members.Size())
}

func (d *Dao) Render() string {
// DISPLAY ORG. WITH LIST OF MEMBERS AND ROLES ASSOCIATED
// DISPLAY CONDITIONS
w := strings.Builder{}

w.WriteString(ufmt.Sprintf("# Decentralized Autonomous Organization\n"))

w.WriteString(ufmt.Sprintf("## Members\n\n"))

i := 1
d.members.Iterate("", "", func(key string, value interface{}) bool {
w.WriteString(ufmt.Sprintf("**Member %d: %s**\n", i, key))
w.WriteString(ufmt.Sprintf("Roles:\n"))
roles := d.roleManager.GetUserRoles(std.Address(key))
for _, role := range roles {
w.WriteString(ufmt.Sprintf("\t%s\n", role))
}
w.WriteString(ufmt.Sprintf("\n--------------------------------\n"))
return false
})

w.WriteString(ufmt.Sprintf("## Resources\n\n"))

d.resources.Iterate("", "", func(key string, value interface{}) bool {
condition := value.(daocond.Condition)
w.WriteString(ufmt.Sprintf("**Resource: %s**\n", key))
w.WriteString(ufmt.Sprintf("Condition: %s\n", condition.Render()))
w.WriteString(ufmt.Sprintf("\n--------------------------------\n"))
return false
})

return w.String()
}
12 changes: 8 additions & 4 deletions gno/p/daocond/cond_utils.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@ func ConditionFromJSON(conditionJSON string, hasRoleFn func(memberId string, rol
if err != nil {
panic("invalid condition json format")
}
conditionObj := nodes.MustObject()
return conditionFromJSON(nodes, hasRoleFn, isMemberFn, membersCountFn)
}

func conditionFromJSON(condition *json.Node, hasRoleFn func(memberId string, role string) bool, isMemberFn func(memberId string) bool, membersCountFn func() uint64) Condition {
conditionObj := condition.MustObject()
conditionType := conditionObj["type"].MustString()
switch conditionType {
case "AND":
conditions := conditionObj["conditions"].MustArray()
if len(conditions) == 0 {
panic("AND condition must have 2 conditions")
}
return And(ConditionFromJSON(conditions[0].MustString(), hasRoleFn, isMemberFn, membersCountFn), ConditionFromJSON(conditions[1].MustString(), hasRoleFn, isMemberFn, membersCountFn))
return And(conditionFromJSON(conditions[0], hasRoleFn, isMemberFn, membersCountFn), conditionFromJSON(conditions[1], hasRoleFn, isMemberFn, membersCountFn))
case "OR":
conditions := conditionObj["conditions"].MustArray()
if len(conditions) == 0 {
panic("OR condition must have 2 conditions")
}
return Or(ConditionFromJSON(conditions[0].MustString(), hasRoleFn, isMemberFn, membersCountFn), ConditionFromJSON(conditions[1].MustString(), hasRoleFn, isMemberFn, membersCountFn))
return Or(conditionFromJSON(conditions[0], hasRoleFn, isMemberFn, membersCountFn), conditionFromJSON(conditions[1], hasRoleFn, isMemberFn, membersCountFn))
case "role-count":
return RoleCount(uint64(jsonutil.MustInt64(conditionObj["count"])), conditionObj["role"].MustString(), hasRoleFn)
case "members-threshold":
return MembersThreshold(float64(jsonutil.MustInt64(conditionObj["threshold"])), isMemberFn, membersCountFn)
return MembersThreshold(float64(jsonutil.MustInt64(conditionObj["threshold"]))/100, isMemberFn, membersCountFn)
default:
panic("unknown condition type")
}
Expand Down
17 changes: 16 additions & 1 deletion gno/r/dao_realm2/dao_realm2.gno
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package dao_realm2

import (
"gno.land/p/demo/avl"
"gno.land/p/teritori/dao2"
"gno.land/p/teritori/daocond"
)

var dao *dao2.Dao

func init() {
dao := dao2.NewDao()
dao = dao2.NewDao()

dao.SetRoles([]string{"admin", "community", "financial", "member"})
dao.SetMembers([][]string{
Expand All @@ -15,4 +19,15 @@ func init() {
{"g16jv3rpz7mkt0gqulxas56se2js7v5vmc6n6e0r", "member"},
})

var resourceJSON = `{"type":"AND","conditions":[{"type":"role-count","role":"public-relationships","count":"1"},{"type":"OR","conditions":[{"type":"members-threshold","threshold":"6"},{"type":"role-count","role":"finance-officer","count":"1"}]}]}`

condition := daocond.ConditionFromJSON(resourceJSON, dao.HasRoleFn, dao.IsMemberFn, dao.MembersCountFn)
resources := avl.NewTree()
resources.Set("social.publish_post", condition)
dao.SetResources(resources)

}

func Render(path string) string {
return dao.Render()
}

0 comments on commit 9933aed

Please sign in to comment.