Skip to content

feat(examples): DAO to propose polls and vote #4082

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

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 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
98 changes: 98 additions & 0 deletions examples/gno.land/p/moonia/dao/admin.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package dao

import (
"gno.land/p/moonia/utils"
"std"
)

// Admin Methods //

func (d *DAO) TransferAdmin(addr std.Address) string {
caller := utils.GetCaller()

if !d.IsAdmin(caller) {
panic("Only the admin can transfer admin.")
}
if d.IsAdmin(addr) {
return "Address is already the admin."
}
if !d.IsMember(addr) {
panic("New admin must be a member.")
}
d.Admin = addr
return "Admin transferred to " + addr.String()
}

func (d *DAO) SetAdmin() string {
if d.Admin != "" {
panic("Admin is already set.")
}
caller := utils.GetCaller()
d.Admin = caller
d.Whitelist[caller] = true
return "Admin set to: " + caller.String()
}

func (d *DAO) IsAdmin(addr std.Address) bool {
return addr == d.Admin
}

func (d *DAO) AcceptRequest(addr std.Address) string {
caller := utils.GetCaller()
if !d.IsAdmin(caller) {
panic("Only admin can accept requests.")
}
if !d.Requests[addr] {
panic("No such request.")
}
delete(d.Requests, addr)
return d.AddMember(addr)
}

func (d *DAO) DeclineRequest(addr std.Address) string {
caller := utils.GetCaller()
if !d.IsAdmin(caller) {
panic("Only admin can decline requests.")
}
if !d.Requests[addr] {
panic("No such request.")
}
delete(d.Requests, addr)
return "Request declined for " + addr.String()
}

// Member Methods //

func (d *DAO) IsMember(addr std.Address) bool {
return d.Whitelist[addr]
}

func (d *DAO) AddMember(addr std.Address) string {
caller := utils.GetCaller()

if !d.IsAdmin(caller) {
panic("Only the admin can add members.")
}
if d.IsMember(addr) {
return "Address is already a member."
}
d.Whitelist[addr] = true
return "Member added: " + addr.String()
}

func (d *DAO) KickMember(addr std.Address) string {
caller := utils.GetCaller()

if !d.IsAdmin(caller) {
panic("Only the admin can kick members.")
}
if !d.IsMember(addr) {
return "Address is not a member."
}
delete(d.Whitelist, addr)
return "Member kicked: " + addr.String()
}

func (d* DAO) ListMembers() map[std.Address]bool {
return d.Whitelist
}
79 changes: 79 additions & 0 deletions examples/gno.land/p/moonia/dao/dao.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package dao

import (
"std"
"strconv"
"gno.land/p/moonia/utils"
)

type DAO struct {
Admin std.Address
Whitelist map[std.Address]bool
Requests map[std.Address]bool
Name string
Description string
}

func NewDAO(name, desc string) *DAO {
return &DAO{
Admin: "",
Whitelist: make(map[std.Address]bool),
Requests: make(map[std.Address]bool),
Name: name,
Description: desc,
}
}

func (d *DAO) ListRequests() map[std.Address]bool {
return d.Requests
}

func (d *DAO) ShowAdmin() string {
if d.Admin == "" {
return "_No admin set._"
}
return "Admin: `" + d.Admin.String() + "`"
}

func (d *DAO) RequestDAO() string {
caller := utils.GetCaller()
if d.Whitelist[caller] {
return "You are already a member."
}
if d.Requests[caller] {
return "You have already requested to join."
}
d.Requests[caller] = true
return "Request to join sent."
}

func (d *DAO) LeaveDAO() string {
caller := utils.GetCaller()
if !d.Whitelist[caller] {
panic("You are not a member of the DAO.")
}
delete(d.Whitelist, caller)
return "You have successfully left the DAO."
}

func (d *DAO) ShowWhitelist() string {
out := "## Whitelist Members ✅\n\n"
if len(d.Whitelist) == 0 {
return out + "_Whitelist is empty._\n"
}
for addr := range d.Whitelist {
if addr == d.Admin {
out += "- " + addr.String() + " (Admin)" + "\n"
} else {
out += "- " + addr.String() + "\n"
}
}
return out
}

func (d *DAO) Stats(totalProposals, activeProposals int) string {
return "### Stats\n\n" +
"- Total Proposals: " + strconv.Itoa(totalProposals) + "\n" +
"- Active Proposals: " + strconv.Itoa(activeProposals) + "\n" +
"- Whitelist Members: " + strconv.Itoa(len(d.Whitelist)) + "\n"
}
1 change: 1 addition & 0 deletions examples/gno.land/p/moonia/dao/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/moonia/dao
161 changes: 161 additions & 0 deletions examples/gno.land/p/moonia/dao/proposals.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package dao

import (
"std"
"strconv"
"gno.land/p/moonia/utils"
"gno.land/p/moul/txlink"
"gno.land/p/moul/md"
)

type Proposal struct {
Title string
Description string
Creator std.Address
YesVotes int
NoVotes int
Voters map[std.Address]bool
Active bool
VotingPeriod int64
CreatedAt int64
}

type ProposalStore struct {
Proposals []Proposal
DAO *DAO

}

func NewProposalStore(dao *DAO) *ProposalStore {
return &ProposalStore{
Proposals: []Proposal{},
DAO: dao,
}
}

func (ps *ProposalStore) CreateProposal(title, description string, period int64) string {
caller := utils.GetCaller()
if !ps.DAO.Whitelist[caller] {
panic("Only whitelisted members can create proposals.")
}
if period <= 0 {
panic("Voting period must be greater than 0.")
}

p := Proposal{
Title: title,
Description: description,
Creator: caller,
Voters: make(map[std.Address]bool),
Active: true,
VotingPeriod: period,
CreatedAt: utils.GetBlockTime(),
}
ps.Proposals = append(ps.Proposals, p)
return "Proposal created: " + title + " Voting period: " + strconv.Itoa(int(period))
}

func (ps *ProposalStore) CloseProposal(indexStr string) string {
index := utils.ParseIndex(indexStr, len(ps.Proposals))
p := &ps.Proposals[index]
if p.Creator != utils.GetCaller() {
panic("Only the proposal creator can close it.")
}
if !p.Active {
panic("Proposal is already closed.")
}
p.Active = false
return "Proposal '" + p.Title + "' has been closed."
}

func (ps *ProposalStore) Vote(indexStr, voteYesStr string) string {
index := utils.ParseIndex(indexStr, len(ps.Proposals))
voteYes := voteYesStr == "true"
caller := utils.GetCaller()

if !ps.DAO.Whitelist[caller] {
panic("Only whitelisted members can vote.")
}

p := &ps.Proposals[index]

currentTime := utils.GetBlockTime()
if currentTime - p.CreatedAt >= p.VotingPeriod {
p.Active = false
panic("Voting period is over. Proposal is now closed.")
}

if !p.Active {
panic("Voting is closed for this proposal.")
}
if p.Voters[caller] {
panic("You have already voted.")
}

p.Voters[caller] = true
if voteYes {
p.YesVotes++
return "Vote recorded: YES for '" + p.Title + "'"
} else {
p.NoVotes++
return "Vote recorded: NO for '" + p.Title + "'"
}
}

func (ps *ProposalStore) ShowProposals() string {
activeOut := "## Active Proposals\n\n"
closedOut := "## Closed Proposals\n\n"
hasActive := false
hasClosed := false

for i, p := range ps.Proposals {
proposalStr := "**[" + strconv.Itoa(i) + "]** " + p.Title + "\n"
proposalStr += p.Description + "\n\n"
proposalStr += "by _" + p.Creator.String() + "_\n\n"
proposalStr += "📅 Voting ends at: " + utils.FormatTimestamp(p.CreatedAt + p.VotingPeriod) + "\n\n"
proposalStr += "✅ " + strconv.Itoa(p.YesVotes) + " | ❌ " + strconv.Itoa(p.NoVotes) + "\n"

if p.Active {
hasActive = true
proposalStr += "(Active) — " +
md.Link("Vote Yes", txlink.Call("Vote", "args", strconv.Itoa(i), "args", "true")) + " | " +
md.Link("Vote No", txlink.Call("Vote", "args", strconv.Itoa(i), "args", "false")) + "\n\n" +
md.Link("❌ Close proposal", txlink.Call("CloseProposal", "args", strconv.Itoa(i))) + "\n\n ---"
activeOut += proposalStr + "\n\n"
} else {
hasClosed = true
proposalStr += "(Closed)\n"
closedOut += proposalStr + "\n\n ---- \n\n"
}
}
if !hasActive {
activeOut += "_No active proposals._\n\n"
}
if !hasClosed {
closedOut += "_No closed proposals._\n\n"
}
return activeOut + "\n" + closedOut
}

func (ps *ProposalStore) EditProposal(indexStr, newTitle, newDescription string, newPeriod int64) string {
index := utils.ParseIndex(indexStr, len(ps.Proposals))
p := &ps.Proposals[index]
if p.Creator != utils.GetCaller() {
panic("Only the creator can edit the proposal.")
}
if !p.Active {
panic("Cannot edit a closed proposal.")
}
if newPeriod <= 0 {
panic("Voting period must be greater than 0.")
}
p.Title = newTitle
p.Description = newDescription
p.VotingPeriod = newPeriod
return "Proposal updated: " + newTitle + newDescription + strconv.Itoa(int(newPeriod))
}

func (ps *ProposalStore) GetProposal(indexStr string) Proposal {
index := utils.ParseIndex(indexStr, len(ps.Proposals))
return ps.Proposals[index]
}
1 change: 1 addition & 0 deletions examples/gno.land/p/moonia/utils/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/moonia/utils
49 changes: 49 additions & 0 deletions examples/gno.land/p/moonia/utils/utils.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package utils

import (
"std"
"strconv"
"time"
"strings"
)

func GetBlockTime() int64 {
return time.Now().Unix()
}

func FormatTimestamp(timestamp int64) string {
t := time.Unix(timestamp, 0)
return t.Format("02 Jan 2006, 15:04")
}

func GetCaller() std.Address {
return std.PreviousRealm().Address()
}

func ParseIndex(indexStr string, max int) int {
index, err := strconv.Atoi(indexStr)
if err != nil {
panic("Invalid index.")
}
if index < 0 || index >= max {
panic("Proposal does not exist.")
}
return index
}

func ParseQuery(path string) (daoID string, showStats bool) {
parts := strings.Split(path, "?")
if len(parts) < 2 {
return "", false
}

args := strings.Split(parts[1], "&")
for _, arg := range args {
if strings.HasPrefix(arg, "dao=") {
daoID = arg[len("dao="):]
} else if arg == "stats" {
showStats = true
}
}
return
}
Comment on lines +29 to +44
Copy link
Contributor

Choose a reason for hiding this comment

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

this package seems like general utils package, but then you see that it's actually not, as it has things replated to your specific app. When you separate code like this, it also makes sense to think about how other people might use your code.

In this case, I have two things to say:

  • maybe this utils package should be under r/moonia/home/utils, or /internal
  • for handling queries and paths, you can use the p/demo/mux package. Check this tutorial out: https://gno.land/r/docs/routing

Loading