config.gno
4.17 Kb · 146 lines
1package governance
2
3import (
4 "errors"
5
6 bptree "gno.land/p/nt/bptree/v0"
7)
8
9// Config represents the configuration of the governor contract
10// All parameters in this struct can be modified through governance.
11type Config struct {
12 // VotingStartDelay is the delay before voting starts after proposal creation (in seconds)
13 VotingStartDelay int64
14 // VotingPeriod is the duration during which votes are collected (in seconds)
15 VotingPeriod int64
16 // VotingWeightSmoothingDuration is the period over which voting weight is averaged
17 // for proposal creation and cancellation threshold calculations (in seconds)
18 VotingWeightSmoothingDuration int64
19 // Quorum is the percentage of active xGNS supply required for proposal approval
20 Quorum int64
21 // ProposalCreationThreshold is the minimum xGNS amount required to create a proposal
22 ProposalCreationThreshold int64
23 // ExecutionDelay is the waiting period after voting ends before a proposal can be executed (in seconds)
24 ExecutionDelay int64
25 // ExecutionWindow is the time window during which an approved proposal can be executed (in seconds)
26 ExecutionWindow int64
27}
28
29func (c Config) IsValid(currentTime int64) error {
30 if c.VotingStartDelay < 0 {
31 return errors.New("votingStartDelay cannot be negative")
32 }
33
34 if c.VotingPeriod < 0 {
35 return errors.New("votingPeriod cannot be negative")
36 }
37
38 if c.ExecutionDelay < 0 {
39 return errors.New("executionDelay cannot be negative")
40 }
41
42 if c.ExecutionWindow < 0 {
43 return errors.New("executionWindow cannot be negative")
44 }
45
46 if c.VotingWeightSmoothingDuration < 0 {
47 return errors.New("votingWeightSmoothingDuration cannot be negative")
48 }
49
50 if c.ProposalCreationThreshold < 0 {
51 return errors.New("proposalCreationThreshold cannot be negative")
52 }
53
54 if c.Quorum < 0 || c.Quorum > 100 {
55 return errors.New("quorum must be between 0 and 100")
56 }
57
58 sum := int64(0)
59
60 sum += c.VotingStartDelay
61 if sum < 0 {
62 return errors.New("votingStartDelay cannot be negative")
63 }
64
65 sum += c.VotingPeriod
66 if sum < 0 {
67 return errors.New("votingPeriod cannot be negative")
68 }
69
70 sum += c.ExecutionDelay
71 if sum < 0 {
72 return errors.New("executionDelay cannot be negative")
73 }
74
75 sum += c.ExecutionWindow
76 if sum < 0 {
77 return errors.New("executionWindow cannot be negative")
78 }
79
80 sum += currentTime
81 if sum < 0 {
82 return errors.New("total delay cannot be negative")
83 }
84
85 return nil
86}
87
88func NewConfig(
89 votingStartDelay,
90 votingPeriod,
91 votingWeightSmoothingDuration,
92 quorum,
93 proposalCreationThreshold,
94 executionDelay,
95 executionWindow int64,
96) Config {
97 return Config{
98 VotingStartDelay: votingStartDelay,
99 VotingPeriod: votingPeriod,
100 VotingWeightSmoothingDuration: votingWeightSmoothingDuration,
101 Quorum: quorum,
102 ProposalCreationThreshold: proposalCreationThreshold,
103 ExecutionDelay: executionDelay,
104 ExecutionWindow: executionWindow,
105 }
106}
107
108// NewConfigPtr constructs a Config and returns a pointer to it. The Config is
109// allocated within the governance domain realm, satisfying realm allocation
110// checks for callers (such as tests) that require a *Config value.
111func NewConfigPtr(
112 votingStartDelay,
113 votingPeriod,
114 votingWeightSmoothingDuration,
115 quorum,
116 proposalCreationThreshold,
117 executionDelay,
118 executionWindow int64,
119) *Config {
120 c := NewConfig(
121 votingStartDelay,
122 votingPeriod,
123 votingWeightSmoothingDuration,
124 quorum,
125 proposalCreationThreshold,
126 executionDelay,
127 executionWindow,
128 )
129 return &c
130}
131
132func NewDefaultConfig() Config {
133 return Config{
134 VotingStartDelay: 86400, // 1 day - delay before voting starts
135 VotingPeriod: 604800, // 7 days - duration for collecting votes
136 VotingWeightSmoothingDuration: 86400, // 1 day - period for averaging voting weight
137 Quorum: 50, // 50% of total xGNS supply required
138 ProposalCreationThreshold: 1_000_000_000, // 1 billion xGNS - minimum amount to create proposals
139 ExecutionDelay: 86400, // 1 day - waiting period before execution
140 ExecutionWindow: 2592000, // 30 days - window for executing proposals
141 }
142}
143
144func NewConfigTree() *bptree.BPTree {
145 return bptree.NewBPTreeN(16)
146}