Search Apps Documentation Source Content File Folder Download Copy Actions Download

proposal_text.gno

1.87 Kb · 74 lines
 1package commondao
 2
 3import (
 4	"strings"
 5	"time"
 6
 7	"gno.land/p/nt/commondao/v0"
 8)
 9
10const (
11	maxTextBody         = 15_000
12	maxTextTitle        = 255
13	minTextVotingPeriod = time.Hour * 24
14)
15
16// NewTextPropDefinition creates a new general text proposal definition.
17func NewTextPropDefinition(title, body string, quorum float64, votingPeriod time.Duration) TextPropDefinition {
18	title = strings.TrimSpace(title)
19	if title == "" {
20		panic("proposal title is empty")
21	}
22
23	if len(title) > maxTextTitle {
24		panic("proposal title is too long, max length is 255 chars")
25	}
26
27	body = strings.TrimSpace(body)
28	if body == "" {
29		panic("proposal body is empty")
30	}
31
32	if len(body) > maxTextBody {
33		panic("proposal body is too long, max length is 15000 chars")
34	}
35
36	if votingPeriod < minTextVotingPeriod {
37		panic("minimum proposal voting period is one day")
38	}
39
40	if quorum < commondao.QuorumOneThird {
41		panic("minimum quorum allowed is one third (0.33)")
42	}
43
44	return TextPropDefinition{
45		title:        title,
46		body:         body,
47		votingPeriod: votingPeriod,
48		quorum:       quorum,
49	}
50}
51
52// TextPropDefinition defines a proposal type for general text proposals.
53// These type of proposals are not executable so nothing happens when they pass.
54type TextPropDefinition struct {
55	title, body  string
56	votingPeriod time.Duration
57	quorum       float64
58}
59
60func (p TextPropDefinition) Title() string               { return p.title }
61func (p TextPropDefinition) Body() string                { return p.body }
62func (p TextPropDefinition) VotingPeriod() time.Duration { return p.votingPeriod }
63
64func (p TextPropDefinition) Tally(ctx commondao.VotingContext) (bool, error) {
65	if !commondao.IsQuorumReached(p.quorum, ctx.VotingRecord, ctx.Members) {
66		return false, commondao.ErrNoQuorum
67	}
68
69	c, success := commondao.SelectChoiceByPlurality(ctx.VotingRecord)
70	if success {
71		return c == commondao.ChoiceYes, nil
72	}
73	return false, nil
74}