Skip to content
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

feat: add r/demo/daoweb realm #3074

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
116 changes: 116 additions & 0 deletions examples/gno.land/r/demo/daoweb/daoweb.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package daoweb

import (
"std"

"gno.land/p/demo/dao"
"gno.land/p/demo/json"
"gno.land/r/gov/dao/bridge"
)

// Proposals returns the paginated GovDAO proposals
func Proposals(offset, count uint64) string {
var (
propStore = bridge.GovDAO().GetPropStore()
size = propStore.Size()
)

// Get the props
props := propStore.Proposals(offset, count)

resp := ProposalsResponse{
Proposals: make([]Proposal, 0, count),
Total: uint64(size),
}

for _, p := range props {
prop := Proposal{
Author: p.Author(),
Description: p.Description(),
Status: p.Status(),
Stats: p.Stats(),
IsExpired: p.IsExpired(),
}

resp.Proposals = append(resp.Proposals, prop)
}

// Encode the response into JSON
encodedProps, err := json.Marshal(encodeProposalsResponse(resp))
if err != nil {
panic(err)
}

return string(encodedProps)
}

// ProposalByID fetches the proposal using the given ID
func ProposalByID(id uint64) string {
propStore := bridge.GovDAO().GetPropStore()

p, err := propStore.ProposalByID(id)
if err != nil {
panic(err)
}

// Encode the response into JSON
prop := Proposal{
Author: p.Author(),
Description: p.Description(),
Status: p.Status(),
Stats: p.Stats(),
IsExpired: p.IsExpired(),
}

encodedProp, err := json.Marshal(encodeProposal(prop))
if err != nil {
panic(err)
}

return string(encodedProp)
}

// encodeProposal encodes a proposal into a json node
func encodeProposal(p Proposal) *json.Node {
return json.ObjectNode("", map[string]*json.Node{
"author": json.StringNode("author", p.Author.String()),
"description": json.StringNode("description", p.Description),
"status": json.StringNode("status", p.Status.String()),
"stats": json.ObjectNode("stats", map[string]*json.Node{
"yay_votes": json.NumberNode("yay_votes", float64(p.Stats.YayVotes)),
"nay_votes": json.NumberNode("nay_votes", float64(p.Stats.NayVotes)),
"abstain_votes": json.NumberNode("abstain_votes", float64(p.Stats.AbstainVotes)),
"total_voting_power": json.NumberNode("total_voting_power", float64(p.Stats.TotalVotingPower)),
}),
"is_expired": json.BoolNode("is_expired", p.IsExpired),
})
}

// encodeProposalsResponse encodes a proposal response into a JSON node
func encodeProposalsResponse(props ProposalsResponse) *json.Node {
proposals := make([]*json.Node, 0, len(props.Proposals))

for _, p := range props.Proposals {
proposals = append(proposals, encodeProposal(p))
}

return json.ObjectNode("", map[string]*json.Node{
"proposals": json.ArrayNode("proposals", proposals),
"total": json.NumberNode("total", float64(props.Total)),
})
}

// ProposalsResponse is a paginated proposal response
type ProposalsResponse struct {
Proposals []Proposal `json:"proposals"`
Total uint64 `json:"total"`
}

// Proposal is a single GovDAO proposal
type Proposal struct {
Author std.Address `json:"author"`
Description string `json:"description"`
Status dao.ProposalStatus `json:"status"`
Stats dao.Stats `json:"stats"`
IsExpired bool `json:"is_expired"`
}
7 changes: 7 additions & 0 deletions examples/gno.land/r/demo/daoweb/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module gno.land/r/demo/daoweb

require (
gno.land/p/demo/dao v0.0.0-latest
gno.land/p/demo/json v0.0.0-latest
gno.land/r/gov/dao/bridge v0.0.0-latest
)
Loading