-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnodao.gno
402 lines (351 loc) · 11.4 KB
/
gnodao.gno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package gnodao
import (
"gno.land/p/demo/avl"
"std"
"strconv"
"strings"
"time"
)
type VoteOption uint32
const (
YES VoteOption = 0 // Indicates approval of the proposal in its current form.
NO VoteOption = 1 // Indicates disapproval of the proposal in its current form.
NO_WITH_VETO VoteOption = 2 // Indicates stronger opposition to the proposal than simply voting No. Not available for SuperMajority-typed proposals as a simple No of 1/3 out of total votes would result in the same outcome.
ABSTAIN VoteOption = 3 // Indicates that the voter is impartial to the outcome of the proposal. Although Abstain votes are counted towards the quorum, they're excluded when calculating the ratio of other voting options above.
)
// GNODAO VOTE
type Vote struct {
address std.Address // address of the voter
timestamp uint64 // block timestamp of the vote
option VoteOption // vote option
}
type DAO struct {
id uint64
uri string // DAO homepage link
metadata string // DAO metadata reference link
funds uint64 // DAO managing funds
depositHistory []string // deposit history - reserved for later use
spendHistory []string // spend history - reserved for later use
permissions []string // permissions managed on DAO - reserved for later use
permMap *avl.MutTree // permission map - reserved for later use
votingPowers *avl.MutTree
totalVotingPower uint64
votingPeriod uint64
voteQuorum uint64
threshold uint64
vetoThreshold uint64
}
type ProposalStatus uint32
const (
NIL ProposalStatus = 0
VOTING_PERIOD ProposalStatus = 1
PASSED ProposalStatus = 2
REJECTED ProposalStatus = 3
FAILED ProposalStatus = 4
)
func (s ProposalStatus) String() string {
switch s {
case NIL:
return "Nil"
case VOTING_PERIOD:
return "VotingPeriod"
case PASSED:
return "Passed"
case REJECTED:
return "Rejected"
case FAILED:
return "Failed"
}
return ""
}
type VotingPower struct {
address string
power uint64
}
type Proposal struct {
daoId uint64 // dao id of the proposal
id uint64 // unique id assigned for each proposal
title string // proposal title
summary string // proposal summary
spendAmount uint64 // amount of tokens to spend as part the proposal
spender std.Address // address to receive spending tokens
vpUpdates []VotingPower // updates on voting power - optional
newMetadata string // new metadata for the DAO - optional
newURI string // new URI for the DAO - optional
submitTime uint64 // proposal submission time
voteEndTime uint64 // vote end time for the proposal
status ProposalStatus // StatusNil | StatusVotingPeriod | StatusPassed | StatusRejected | StatusFailed
votes *avl.MutTree // votes on the proposal
votingPowers []uint64 // voting power sum per voting option
}
// GNODAO STATE
var daos []DAO
var proposals [][]Proposal
func getDAOVotingPower(daoId uint64, address string) uint64 {
if len(daos) <= int(daoId) {
return 0
}
res, ok := daos[daoId].votingPowers.Get(address)
if ok {
return res.(uint64)
}
return 0
}
func IsDAOMember(daoId uint64, address std.Address) bool {
return getDAOVotingPower(daoId, address.String()) > 0
}
func getVote(daoId, proposalId uint64, address std.Address) (Vote, bool) {
if int(daoId) >= len(daos) {
return Vote{}, false
}
if int(proposalId) >= len(proposals[daoId]) {
return Vote{}, false
}
vote, ok := proposals[daoId][proposalId].votes.Get(address.String())
if ok {
return vote.(Vote), true
}
return Vote{}, false
}
func parseVotingPowers(daoMembers, votingPowers string) []VotingPower {
parsedVPs := []VotingPower{}
if len(daoMembers) == 0 {
return parsedVPs
}
memberAddrs := strings.Split(daoMembers, ",")
memberPowers := strings.Split(votingPowers, ",")
if len(memberAddrs) != len(memberPowers) {
panic("mismatch between members and voting powers count")
}
for i, memberAddr := range memberAddrs {
power, err := strconv.Atoi(memberPowers[i])
if err != nil {
panic(err)
}
parsedVPs = append(parsedVPs, VotingPower{
address: memberAddr,
power: uint64(power),
})
}
return parsedVPs
}
// GNODAO FUNCTIONS
func CreateDAO(
uri string,
metadata string,
daoMembers string,
votingPowers string,
votingPeriod uint64,
voteQuorum uint64,
threshold uint64,
vetoThreshold uint64,
) {
daoId := uint64(len(daos))
daos = append(daos, DAO{
id: daoId,
uri: uri,
metadata: metadata,
funds: 0,
depositHistory: []string{},
spendHistory: []string{},
permissions: []string{},
permMap: avl.NewMutTree(),
votingPowers: avl.NewMutTree(),
totalVotingPower: 0,
votingPeriod: votingPeriod,
voteQuorum: voteQuorum,
threshold: threshold,
vetoThreshold: vetoThreshold,
})
parsedVPs := parseVotingPowers(daoMembers, votingPowers)
totalVotingPower := uint64(0)
for _, vp := range parsedVPs {
daos[daoId].votingPowers.Set(vp.address, vp.power)
totalVotingPower += vp.power
}
daos[daoId].totalVotingPower = totalVotingPower
proposals = append(proposals, []Proposal{})
// TODO: emit events
}
func CreateProposal(
daoId uint64,
title, summary string,
spendAmount uint64, spender std.Address,
daoMembers string,
vpUpdates string,
newMetadata string,
newURI string,
) {
caller := std.GetOrigCaller()
// if sender is not a dao member, revert
isCallerDaoMember := IsDAOMember(daoId, caller)
if !isCallerDaoMember {
panic("caller is not a dao member")
}
parsedVPUpdates := parseVotingPowers(daoMembers, vpUpdates)
proposals[daoId] = append(proposals[daoId], Proposal{
daoId: daoId,
id: uint64(len(proposals[daoId])),
title: title,
summary: summary,
spendAmount: spendAmount,
spender: spender,
vpUpdates: parsedVPUpdates,
newMetadata: newMetadata,
newURI: newURI,
submitTime: uint64(time.Now().Unix()),
voteEndTime: uint64(time.Now().Unix()) + daos[daoId].votingPeriod,
status: VOTING_PERIOD,
votes: avl.NewMutTree(),
votingPowers: []uint64{0, 0, 0, 0}, // initiate as zero for 4 vote types
})
}
func VoteProposal(daoId, proposalId uint64, option VoteOption) {
caller := std.GetOrigCaller()
// if sender is not a dao member, revert
isCallerDaoMember := IsDAOMember(daoId, caller)
if !isCallerDaoMember {
panic("caller is not a gnodao member")
}
// if invalid proposal, panic
if int(proposalId) >= len(proposals[daoId]) {
panic("invalid proposal id")
}
// if vote end time is reached panic
if time.Now().Unix() > int64(proposals[daoId][proposalId].voteEndTime) {
panic("vote end time reached")
}
// Original vote cancel
callerVotingPower := getDAOVotingPower(daoId, caller.String())
vote, ok := getVote(daoId, proposalId, caller)
if ok {
if proposals[daoId][proposalId].votingPowers[int(vote.option)] > callerVotingPower {
proposals[daoId][proposalId].votingPowers[int(vote.option)] -= callerVotingPower
} else {
proposals[daoId][proposalId].votingPowers[int(vote.option)] = 0
}
}
// Create a vote
proposals[daoId][proposalId].votes.Set(caller.String(), Vote{
address: caller,
timestamp: uint64(time.Now().Unix()),
option: option,
})
// Voting power by option update for new vote
proposals[daoId][proposalId].votingPowers[int(option)] += callerVotingPower
}
// TODO: handle voting power change during voting period for other proposal
// TODO: experiment with gas limit
func TallyAndExecute(daoId, proposalId uint64) {
caller := std.GetOrigCaller()
// if sender is not a dao member, revert
isCallerDaoMember := IsDAOMember(daoId, caller)
if !isCallerDaoMember {
panic("caller is not a gnodao member")
}
// validation for proposalId
if int(proposalId) >= len(proposals[daoId]) {
panic("invalid proposal id")
}
dao := daos[daoId]
proposal := proposals[daoId][proposalId]
votingPowers := proposal.votingPowers
if time.Now().Unix() < int64(proposal.voteEndTime) {
panic("proposal is in voting period")
}
// reference logic for tally - https://github.com/cosmos/cosmos-sdk/blob/main/x/gov/keeper/tally.go
totalVotes := votingPowers[YES] + votingPowers[NO] + votingPowers[NO_WITH_VETO] + votingPowers[ABSTAIN]
if totalVotes < dao.totalVotingPower*dao.voteQuorum/100 {
proposals[daoId][proposalId].status = REJECTED
}
// If no one votes (everyone abstains), proposal rejected
if totalVotes == votingPowers[ABSTAIN] {
proposals[daoId][proposalId].status = REJECTED
}
// If more than 1/3 of voters veto, proposal rejected
vetoThreshold := dao.vetoThreshold
if votingPowers[NO_WITH_VETO] > totalVotes*vetoThreshold/100 {
proposals[daoId][proposalId].status = REJECTED
}
// If more than 1/2 of non-abstaining voters vote Yes, proposal passes
threshold := dao.threshold
if votingPowers[YES] > (totalVotes-votingPowers[ABSTAIN])*threshold/100 {
proposals[daoId][proposalId].status = PASSED
// TODO: spend coins when spendAmount is positive & spender is a valid address
if proposal.spendAmount > 0 {
if daos[daoId].funds >= proposal.spendAmount {
daos[daoId].funds -= proposal.spendAmount
} else {
proposals[daoId][proposalId].status = FAILED
return
}
}
if proposal.newMetadata != "" {
daos[daoId].metadata = proposal.newMetadata
}
if proposal.newURI != "" {
daos[daoId].uri = proposal.newURI
}
for _, vp := range proposal.vpUpdates {
daos[daoId].totalVotingPower -= getDAOVotingPower(daoId, vp.address)
daos[daoId].votingPowers.Set(vp.address, vp.power)
daos[daoId].totalVotingPower += vp.power
}
// TODO: contract does not own account that can hold coins - this is one of limitations
// TODO: Adena Wallet from OnBloc - investigate on how they manage coins (swap - custody?)
// Manual sending for funds (Address <-> Address) - Miloš Živković
// https://github.com/gnolang/gno/blob/e392ab51bc05a5efbceaa8dbe395bac2e01ad808/tm2/pkg/crypto/keys/client/send.go#L109-L119
return
}
// If more than 1/2 of non-abstaining voters vote No, proposal rejected
proposals[daoId][proposalId].status = REJECTED
}
func DepositDAO(daoId uint64, amount uint64) {
caller := std.GetOrigCaller()
// if sender is not a dao member, revert
isCallerDaoMember := IsDAOMember(daoId, caller)
if !isCallerDaoMember {
panic("caller is not a gnodao member")
}
// TODO: send coins from caller to DAO
// TODO: verify received amount
// daos[daoId].depositHistory = append(daos[daoId].depositHistory, Deposit{
// address: caller,
// amount: amount,
// })
}
func GetDAO(daoId uint64) DAO {
if int(daoId) >= len(daos) {
panic("invalid dao id")
}
return daos[daoId]
}
func GetDAOs(startAfter, limit uint64) []DAO {
max := uint64(len(daos))
if startAfter+limit < max {
max = startAfter + limit
}
return daos[startAfter:max]
}
func GetProposal(daoId, proposalId uint64) Proposal {
if int(daoId) >= len(daos) {
panic("invalid dao id")
}
if int(proposalId) >= len(proposals[daoId]) {
panic("invalid proposal id")
}
return proposals[daoId][proposalId]
}
func GetProposals(daoId, startAfter, limit uint64) []Proposal {
if int(daoId) >= len(daos) {
panic("invalid dao id")
}
max := uint64(len(proposals[daoId]))
if startAfter+limit < max {
max = startAfter + limit
}
return proposals[daoId][startAfter:max]
}
func Render(path string) string {
return ""
}