-
Notifications
You must be signed in to change notification settings - Fork 418
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
Closed
Closed
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f3a177c
feat(examples): DAO to propose polls and vote
moonia dd8ac24
fix: unused parsing function
moonia d374e16
fix: admin is automatically added to whitelist
moonia 0557d24
feat: Kick and add members by admin
moonia c53fa3a
fix: rm useless comments for gnokey
moonia e3b95b1
feat(wip): set et edit voting period
moonia 3b60ed4
feat: transfer admin + IsMember method
moonia 6399fcf
feat: dashboard to join or create a DAO + view DAO by id
moonia 2b53ee6
fix: format when displaying existing DAOs
moonia 106bc51
refacto: move query parsing function to utils
moonia f99e50a
fix: requests methods by admin + see DAOs user
moonia c81eec3
fix: CreateProposalSample function name
moonia ac048f3
fix: create proposal for DAO using its ID
moonia 28b32a7
fix: show stats for dao using its id
moonia 0cb8185
fix: nil pointer when requestDAO because no dao id specified
moonia 8cda97d
fix: display request number
moonia 63eefae
fix: membership to a dao and display your DAOs
moonia 14f2002
refacto: rm non-used interfaces
moonia b610fc6
refacto: rm GetCaller useless encapsulation
moonia c363daf
fix: unused import of std
moonia d0a581b
Merge branch 'gnolang:master' into master
moonia 51e50f6
feat(gnodev): flamegraph cmd to profile binaries & generate flamegraphs
moonia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
moonia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if !d.IsAdmin(caller) { | ||
panic("Only the admin can transfer admin.") | ||
} | ||
if d.IsAdmin(addr) { | ||
return "Address is already the admin." | ||
} | ||
moonia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/moonia/dao |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/moonia/utils |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
moonia marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.