-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): add
r/demo/daoweb
realm (#3074)
## Description This PR adds a simple JSON adapter for `r/gov/dao/v2`, used by [govdao-web](https://govdao.gnoteam.com/). <details><summary>Contributors' checklist...</summary> - x ] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [ ] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests </details>
- Loading branch information
1 parent
9dad8f1
commit 2173b49
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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"` | ||
} |
This file contains 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,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 | ||
) |