Skip to content

Commit

Permalink
fix(gno/dao): remove json parsing for roles & members
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelVallenet committed Jan 13, 2025
1 parent e622fd4 commit c7313d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 53 deletions.
62 changes: 13 additions & 49 deletions gno/p/dao_roles_based/dao_roles_based.gno
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type DaoRolesBased struct {
realmName string
}

func NewDaoRolesBasedJSON(name, description, rolesJSON, membersJSON, resourcesJSON string, handlers []MessageHandler) *DaoRolesBased {
func NewDaoRolesBasedJSON(name, description, resourcesJSON string, roles []string, members [][]string, handlers []MessageHandler) *DaoRolesBased {
if len(name) < 5 {
panic("dao name should be at least 5 characters long")
}
Expand All @@ -44,10 +44,7 @@ func NewDaoRolesBasedJSON(name, description, rolesJSON, membersJSON, resourcesJS
}
dao.initRenderingRouter()

roles := dao.parseRolesJSON(rolesJSON)
dao.MemberModule.setRoles(roles)

members := dao.parseMembersJSON(membersJSON)
dao.MemberModule.setMembers(members)

resources := dao.parseResourcesJSON(resourcesJSON)
Expand Down Expand Up @@ -124,6 +121,18 @@ func (d *DaoRolesBased) ProposeJSON(proposalJSON string) {
d.propose(req.title, req.description, msg.Type(), msg)
}

func (d *DaoRolesBased) HasRole(memberId string, role string) bool {
return d.MemberModule.HasRole(memberId, role)
}

func (d *DaoRolesBased) IsMember(memberId string) bool {
return d.MemberModule.IsMember(memberId)
}

func (d *DaoRolesBased) MembersCount() uint64 {
return d.MemberModule.MembersCount()
}

func (d *DaoRolesBased) propose(title, description, resource string, message ExecutableMessage) {
proposer := std.PrevRealm().Addr()
if !d.MemberModule.IsMember(proposer.String()) {
Expand All @@ -146,51 +155,6 @@ func (d *DaoRolesBased) propose(title, description, resource string, message Exe
d.ProposalModule.newProposal(title, description, proposer, message, condition.NewState())
}

func (d *DaoRolesBased) HasRole(memberId string, role string) bool {
return d.MemberModule.HasRole(memberId, role)
}

func (d *DaoRolesBased) IsMember(memberId string) bool {
return d.MemberModule.IsMember(memberId)
}

func (d *DaoRolesBased) MembersCount() uint64 {
return d.MemberModule.MembersCount()
}

func (d *DaoRolesBased) parseRolesJSON(rolesJSON string) []string {
nodes, err := json.Unmarshal([]byte(rolesJSON))
if err != nil {
panic("invalid roles json format")
}
roles := nodes.MustArray()
rolesStr := make([]string, len(roles))
for i, role := range roles {
rolesStr[i] = role.MustString()
}
return rolesStr
}

func (d *DaoRolesBased) parseMembersJSON(membersJSON string) [][]string {
nodes, err := json.Unmarshal([]byte(membersJSON))
if err != nil {
panic("invalid members json format")
}
membersNodesArray := nodes.MustArray()
members := make([][]string, len(membersNodesArray))
for i, memberNode := range membersNodesArray {
memberObj := memberNode.MustObject()
memberRolesObj := memberObj["roles"].MustArray()
member := make([]string, len(memberRolesObj)+1)
member[0] = memberObj["address"].MustString()
for j, role := range memberRolesObj {
member[j+1] = role.MustString()
}
members[i] = member
}
return members
}

func (d *DaoRolesBased) parseResourcesJSON(resourcesJSON string) *avl.Tree {
nodes, err := json.Unmarshal([]byte(resourcesJSON))
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions gno/r/dao_realm/dao_realm.gno
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ func init() {
name := "Demo DAO"
description := "This is a demo DAO"

var rolesJSON = `["admin","public-relationships","finance-officer"]`
var membersJSON = `[{"address":"g126gx6p6d3da4ymef35ury6874j6kys044r7zlg","roles":["admin","public-relationships"]},{"address":"g1ld6uaykyugld4rnm63rcy7vju4zx23lufml3jv","roles":["public-relationships"]},{"address":"g1r69l0vhp7tqle3a0rk8m8fulr8sjvj4h7n0tth","roles":["finance-officer"]},{"address":"g16jv3rpz7mkt0gqulxas56se2js7v5vmc6n6e0r","roles":[]}]`
roles := []string{"admin", "public-relationships", "finance-officer"}
members := [][]string{
{"g126gx6p6d3da4ymef35ury6874j6kys044r7zlg", "admin", "public-relationships"},
{"g1ld6uaykyugld4rnm63rcy7vju4zx23lufml3jv", "public-relationships"},
{"g1r69l0vhp7tqle3a0rk8m8fulr8sjvj4h7n0tth", "finance-officer"},
{"g16jv3rpz7mkt0gqulxas56se2js7v5vmc6n6e0r", ""},
}

messagesHandlers := []dao_roles_based.MessageHandler{
social_feeds.NewCreatePostHandler(),
Expand All @@ -31,9 +36,8 @@ func init() {
}
resourcesJSON += `]`

dao = dao_roles_based.NewDaoRolesBasedJSON(name, description, rolesJSON, membersJSON, resourcesJSON, messagesHandlers)
dao = dao_roles_based.NewDaoRolesBasedJSON(name, description, resourcesJSON, roles, members, messagesHandlers)

// Register the DAO profile
profile.SetStringField(profile.DisplayName, name)
profile.SetStringField(profile.Bio, description)
profile.SetStringField(profile.Avatar, "")
Expand Down

0 comments on commit c7313d4

Please sign in to comment.