Skip to content

feat: govdao v3 better rendering #4164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion examples/gno.land/r/gnoland/users/v1/admin.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package users
import (
"std"

"gno.land/p/demo/ufmt"
"gno.land/r/gov/dao"
susers "gno.land/r/sys/users"
)
Expand All @@ -18,7 +19,15 @@ func NewSetPausedExecutor(newPausedValue bool) dao.ProposalRequest {

e := dao.NewSimpleExecutor(cb, "")

return dao.NewProposalRequest("Pause users/v1 realm", "", e)
title := ""
if newPausedValue {
title = "Pause `/r/gnoland/users/v1`"
} else {
title = "Unpause `/r/gnoland/users/v1`"
}

desc := ufmt.Sprintf("Proposal to set the users/v1 realm to `paused=%v`.", newPausedValue)
return dao.NewProposalRequest(title, desc, e)
}

// ProposeNewName allows GovDAO to propose a new name for an existing user
Expand Down
52 changes: 4 additions & 48 deletions examples/gno.land/r/gov/dao/v3/impl/govdao_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ func TestCreateProposalAndVote(t *testing.T) {
// })
dao.MustVoteOnProposalSimple(0, "NO")

urequire.Equal(t, true, contains(dao.Render(""), "Proposal open for votes"))
urequire.Equal(t, true, contains(dao.Render(""), "15.789473684210526%"))
urequire.Equal(t, true, contains(dao.Render(""), "52.63157894736842%"))
urequire.Equal(t, true, strings.Contains(dao.Render(""), "Prop #0 - New Member Proposal"))
urequire.Equal(t, true, strings.Contains(dao.Render(""), "Author: "+m1.String()))

urequire.PanicsWithMessage(t, "proposal didn't reach supermajority yet: 66", func() {
dao.ExecuteProposal(dao.ProposalID(0))
Expand All @@ -140,50 +139,7 @@ func TestCreateProposalAndVote(t *testing.T) {
accepted := dao.ExecuteProposal(dao.ProposalID(0))
urequire.Equal(t, false, accepted)

urequire.Equal(t, true, contains(dao.Render(""), "**PROPOSAL HAS BEEN DENIED**"))
urequire.Equal(t, true, contains(dao.Render(""), "NO PERCENT: 68.42105263157895%"))
}

func TestProposalPagination(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to test this, its tested in p/demo/avl/pager already

loadMembers()
portfolio := "### This is my portfolio:\n\n- THINGS"

testing.SetOriginCaller(m1)

nm1 := testutils.TestAddress("nm1")

var pid dao.ProposalID

pid = dao.MustCreateProposal(NewAddMemberRequest(nm1, memberstore.T2, portfolio))
// TODO: tests keep the same vm state: https://github.com/gnolang/gno/issues/1982
urequire.Equal(t, 1, int(pid))

pid = dao.MustCreateProposal(NewAddMemberRequest(nm1, memberstore.T2, portfolio))
urequire.Equal(t, 2, int(pid))

pid = dao.MustCreateProposal(NewAddMemberRequest(nm1, memberstore.T2, portfolio))
urequire.Equal(t, 3, int(pid))

pid = dao.MustCreateProposal(NewAddMemberRequest(nm1, memberstore.T2, portfolio))
urequire.Equal(t, 4, int(pid))

pid = dao.MustCreateProposal(NewAddMemberRequest(nm1, memberstore.T2, portfolio))
urequire.Equal(t, 5, int(pid))

pid = dao.MustCreateProposal(NewAddMemberRequest(nm1, memberstore.T2, portfolio))
urequire.Equal(t, 6, int(pid))

urequire.Equal(t, true, contains(dao.Render(""), "## Proposal with id: 6"))
urequire.Equal(t, true, contains(dao.Render(""), "## Proposal with id: 5"))
urequire.Equal(t, true, contains(dao.Render(""), "## Proposal with id: 4"))

urequire.Equal(t, true, contains(dao.Render("/?page=2"), "## Proposal with id: 3"))
urequire.Equal(t, true, contains(dao.Render("/?page=2"), "## Proposal with id: 2"))
urequire.Equal(t, true, contains(dao.Render("/?page=2"), "## Proposal with id: 1"))

urequire.Equal(t, true, contains(dao.Render("/?page=3"), "## Proposal with id: 0"))
}
println(dao.Render(""))

func contains(s, substr string) bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also strings.Contains exists

return strings.Index(s, substr) >= 0
urequire.Equal(t, true, strings.Contains(dao.Render(""), "Status: REJECTED"))
}
93 changes: 73 additions & 20 deletions examples/gno.land/r/gov/dao/v3/impl/render.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"gno.land/p/demo/avl/pager"
"gno.land/p/demo/mux"
"gno.land/p/demo/ufmt"
"gno.land/p/moul/helplink"
"gno.land/r/gov/dao"
"gno.land/r/sys/users"
)

type render struct {
Expand All @@ -24,16 +26,12 @@ func NewRender(d *GovDAO) *render {

r := mux.NewRouter()

r.HandleFunc("/", func(rw *mux.ResponseWriter, req *mux.Request) {
rw.Write(ren.renderActiveProposals(req.RawPath, d))
})
Comment on lines -27 to -29
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this checks for pkgpath:/ so kind of useless as well


r.HandleFunc("", func(rw *mux.ResponseWriter, req *mux.Request) {
rw.Write(ren.renderActiveProposals(req.RawPath, d))
})

r.HandleFunc("{pid}", func(rw *mux.ResponseWriter, req *mux.Request) {
rw.Write(ren.renderProposal(req.GetVar("pid"), d))
rw.Write(ren.renderProposalPage(req.GetVar("pid"), d))
})

r.HandleFunc("{pid}/votes", func(rw *mux.ResponseWriter, req *mux.Request) {
Expand All @@ -56,40 +54,61 @@ func (ren *render) Render(path string, pkg string) string {
}

func (ren *render) renderActiveProposals(url string, d *GovDAO) string {
out := "# Active Proposals:\n"

page, err := ren.pssPager.GetPageByPath(url)
if err != nil {
out += ufmt.Sprintf("Error getting selected page: %v", err.Error())
return out
}
out := "# GovDAO Proposals\n"

page := ren.pssPager.MustGetPageByPath(url)
for _, item := range page.Items {
out += ren.renderProposal(item.Key, d)
out += ren.renderProposalListItem(item.Key, d)
out += "---\n\n"
}

out += page.Picker("")

return out
}

func (ren *render) renderProposal(sPid string, d *GovDAO) string {
func (ren *render) renderProposalPage(sPid string, d *GovDAO) string {
pid, err := strconv.Atoi(sPid)
if err != nil {
panic(err.Error())
}
ps := d.pss.GetStatus(dao.ProposalID(pid))

p := dao.MustGetProposal(dao.ProposalID(pid))
out := ufmt.Sprintf("## Prop #%v - %v\n", sPid, p.Title())

out := ""
out += ufmt.Sprintf("## Proposal with id: %v", sPid)
out += StringifyProposal(p)
out += p.Description()
out += "\n\n"

out += "Author: " + tryResolveAddr(p.Author()) + "\n\n"

out += ps.String()
out += ufmt.Sprintf("- [Go to votes list](%v:%v/votes).", ren.relativeRealmPath, sPid)
out += "\n"
out += ufmt.Sprintf("[Detailed voting list](%v:%v/votes)", ren.relativeRealmPath, sPid)
out += "\n\n---\n\n"

out += renderActionBar(sPid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not missing the eligible tiers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's given out by out += ps.String()


return out
}

func (ren *render) renderProposalListItem(sPid string, d *GovDAO) string {
pid, err := strconv.Atoi(sPid)
if err != nil {
panic(err.Error())
}

ps := d.pss.GetStatus(dao.ProposalID(pid))
p := dao.MustGetProposal(dao.ProposalID(pid))
out := ufmt.Sprintf("### [Prop #%v - %v](%v:%v)\n", sPid, p.Title(), ren.relativeRealmPath, sPid)
out += ufmt.Sprintf("Author: %s\n\n", p.Author())

out += "Status: " + getPropStatus(ps)
out += "\n\n"

out += "Tiers eligible to vote: "
out += strings.Join(ps.TiersAllowedToVote, ", ")

out += "\n\n"
return out
}

Expand All @@ -101,8 +120,42 @@ func (ren *render) renderVotesForProposal(sPid string, d *GovDAO) string {
ps := d.pss.GetStatus(dao.ProposalID(pid))

out := ""
out += ufmt.Sprintf("## Voters for Proposal with id: %v\n\n", sPid)
out += ufmt.Sprintf("# Proposal #%v - Vote List\n\n", sPid)
out += StringifyVotes(ps)

return out
}

func isPropActive(ps *proposalStatus) bool {
return !ps.Accepted && !ps.Denied
}

func getPropStatus(ps *proposalStatus) string {
if ps.Accepted {
return "ACCEPTED"
} else if ps.Denied {
return "REJECTED"
}
return "ACTIVE"
}

func renderActionBar(sPid string) string {
out := "### Actions\n"

proxy := helplink.Realm("gno.land/r/gov/dao")
out += proxy.Func("Vote YES", "MustVoteOnProposalSimple", "pid", sPid, "option", "YES") + " | "
out += proxy.Func("Vote NO", "MustVoteOnProposalSimple", "pid", sPid, "option", "NO") + " | "
out += proxy.Func("Vote ABSTAIN", "MustVoteOnProposalSimple", "pid", sPid, "option", "ABSTAIN")

out += "\n\n"
out += "WARN: Please double check transaction data before voting."
return out
}

func tryResolveAddr(addr std.Address) string {
userData := users.ResolveAddress(addr)
if userData == nil {
return addr.String()
}
return userData.RenderLink("")
}
18 changes: 9 additions & 9 deletions examples/gno.land/r/gov/dao/v3/impl/types.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package impl

import (
"std"
"strings"

"gno.land/p/demo/avl"
Expand Down Expand Up @@ -132,7 +133,7 @@ func (ps *proposalStatus) IsAllowed(tier string) bool {

func (ps *proposalStatus) String() string {
var sb strings.Builder
sb.WriteString("### Proposal Status:\n\n")
sb.WriteString("### Stats\n")

if ps.Accepted {
sb.WriteString("- **PROPOSAL HAS BEEN ACCEPTED**\n")
Expand All @@ -143,14 +144,11 @@ func (ps *proposalStatus) String() string {
sb.WriteString(ps.DeniedReason)
}
} else {
sb.WriteString("- **Proposal open for votes**\n")
sb.WriteString("- **Proposal is open for votes**\n")
}

sb.WriteString("- Allowed tiers to vote: ")
for _, t := range ps.TiersAllowedToVote {
sb.WriteString(t)
sb.WriteString(" ")
}
sb.WriteString("- Tiers eligible to vote: ")
sb.WriteString(strings.Join(ps.TiersAllowedToVote, ", "))
sb.WriteString("\n")

sb.WriteString(ufmt.Sprintf("- YES PERCENT: %v%%\n", ps.YesPercent()))
Expand Down Expand Up @@ -184,15 +182,17 @@ func writeVotes(sb *strings.Builder, t memberstore.MembersByTier, title string)

power := tier.PowerHandler(memberstore.Get(), memberstore.Tiers)

sb.WriteString(ufmt.Sprintf("### %v from %v (VPPM %v):\n", title, tn, power))
sb.WriteString(ufmt.Sprintf("%v from %v (VPPM %v):\n\n", title, tn, power))
ms, _ := value.(*avl.Tree)
ms.Iterate("", "", func(addr string, _ interface{}) bool {
sb.WriteString("\n")
sb.WriteString("- " + string(addr) + "\n")
sb.WriteString("- " + tryResolveAddr(std.Address(addr)) + "\n")

return false
})

sb.WriteString("\n")

return false
})
}
Expand Down
Loading