-
Notifications
You must be signed in to change notification settings - Fork 20
/
KaliDAO.sol
542 lines (390 loc) · 18.2 KB
/
KaliDAO.sol
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.4;
import './KaliDAOtoken.sol';
import './utils/Multicall.sol';
import './utils/NFTreceiver.sol';
import './utils/ReentrancyGuard.sol';
import './interfaces/IKaliDAOextension.sol';
/// @notice Simple gas-optimized Kali DAO core module.
contract KaliDAO is KaliDAOtoken, Multicall, NFTreceiver, ReentrancyGuard {
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event NewProposal(
address indexed proposer,
uint256 indexed proposal,
ProposalType indexed proposalType,
string description,
address[] accounts,
uint256[] amounts,
bytes[] payloads
);
event ProposalCancelled(address indexed proposer, uint256 indexed proposal);
event ProposalSponsored(address indexed sponsor, uint256 indexed proposal);
event VoteCast(address indexed voter, uint256 indexed proposal, bool indexed approve);
event ProposalProcessed(uint256 indexed proposal, bool indexed didProposalPass);
/*///////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error Initialized();
error PeriodBounds();
error QuorumMax();
error SupermajorityBounds();
error InitCallFail();
error TypeBounds();
error NotProposer();
error Sponsored();
error NotMember();
error NotCurrentProposal();
error AlreadyVoted();
error NotVoteable();
error VotingNotEnded();
error PrevNotProcessed();
error NotExtension();
/*///////////////////////////////////////////////////////////////
DAO STORAGE
//////////////////////////////////////////////////////////////*/
string public docs;
uint256 private currentSponsoredProposal;
uint256 public proposalCount;
uint32 public votingPeriod;
uint32 public gracePeriod;
uint32 public quorum; // 1-100
uint32 public supermajority; // 1-100
bytes32 public constant VOTE_HASH =
keccak256('SignVote(address signer,uint256 proposal,bool approve)');
mapping(address => bool) public extensions;
mapping(uint256 => Proposal) public proposals;
mapping(uint256 => ProposalState) public proposalStates;
mapping(ProposalType => VoteType) public proposalVoteTypes;
mapping(uint256 => mapping(address => bool)) public voted;
mapping(address => uint256) public lastYesVote;
enum ProposalType {
MINT, // add membership
BURN, // revoke membership
CALL, // call contracts
VPERIOD, // set `votingPeriod`
GPERIOD, // set `gracePeriod`
QUORUM, // set `quorum`
SUPERMAJORITY, // set `supermajority`
TYPE, // set `VoteType` to `ProposalType`
PAUSE, // flip membership transferability
EXTENSION, // flip `extensions` whitelisting
ESCAPE, // delete pending proposal in case of revert
DOCS // amend org docs
}
enum VoteType {
SIMPLE_MAJORITY,
SIMPLE_MAJORITY_QUORUM_REQUIRED,
SUPERMAJORITY,
SUPERMAJORITY_QUORUM_REQUIRED
}
struct Proposal {
ProposalType proposalType;
string description;
address[] accounts; // member(s) being added/kicked; account(s) receiving payload
uint256[] amounts; // value(s) to be minted/burned/spent; gov setting [0]
bytes[] payloads; // data for CALL proposals
uint256 prevProposal;
uint96 yesVotes;
uint96 noVotes;
uint32 creationTime;
address proposer;
}
struct ProposalState {
bool passed;
bool processed;
}
/*///////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
function init(
string memory name_,
string memory symbol_,
string memory docs_,
bool paused_,
address[] memory extensions_,
bytes[] memory extensionsData_,
address[] calldata voters_,
uint256[] calldata shares_,
uint32[16] memory govSettings_
) public payable nonReentrant virtual {
if (extensions_.length != extensionsData_.length) revert NoArrayParity();
if (votingPeriod != 0) revert Initialized();
if (govSettings_[0] == 0 || govSettings_[0] > 365 days) revert PeriodBounds();
if (govSettings_[1] > 365 days) revert PeriodBounds();
if (govSettings_[2] > 100) revert QuorumMax();
if (govSettings_[3] <= 51 || govSettings_[3] > 100) revert SupermajorityBounds();
KaliDAOtoken._init(name_, symbol_, paused_, voters_, shares_);
if (extensions_.length != 0) {
// cannot realistically overflow on human timescales
unchecked {
for (uint256 i; i < extensions_.length; i++) {
extensions[extensions_[i]] = true;
if (extensionsData_[i].length > 3) {
(bool success, ) = extensions_[i].call(extensionsData_[i]);
if (!success) revert InitCallFail();
}
}
}
}
docs = docs_;
votingPeriod = govSettings_[0];
gracePeriod = govSettings_[1];
quorum = govSettings_[2];
supermajority = govSettings_[3];
// set initial vote types
proposalVoteTypes[ProposalType.MINT] = VoteType(govSettings_[4]);
proposalVoteTypes[ProposalType.BURN] = VoteType(govSettings_[5]);
proposalVoteTypes[ProposalType.CALL] = VoteType(govSettings_[6]);
proposalVoteTypes[ProposalType.VPERIOD] = VoteType(govSettings_[7]);
proposalVoteTypes[ProposalType.GPERIOD] = VoteType(govSettings_[8]);
proposalVoteTypes[ProposalType.QUORUM] = VoteType(govSettings_[9]);
proposalVoteTypes[ProposalType.SUPERMAJORITY] = VoteType(govSettings_[10]);
proposalVoteTypes[ProposalType.TYPE] = VoteType(govSettings_[11]);
proposalVoteTypes[ProposalType.PAUSE] = VoteType(govSettings_[12]);
proposalVoteTypes[ProposalType.EXTENSION] = VoteType(govSettings_[13]);
proposalVoteTypes[ProposalType.ESCAPE] = VoteType(govSettings_[14]);
proposalVoteTypes[ProposalType.DOCS] = VoteType(govSettings_[15]);
}
/*///////////////////////////////////////////////////////////////
PROPOSAL LOGIC
//////////////////////////////////////////////////////////////*/
function getProposalArrays(uint256 proposal) public view virtual returns (
address[] memory accounts,
uint256[] memory amounts,
bytes[] memory payloads
) {
Proposal storage prop = proposals[proposal];
(accounts, amounts, payloads) = (prop.accounts, prop.amounts, prop.payloads);
}
function propose(
ProposalType proposalType,
string calldata description,
address[] calldata accounts,
uint256[] calldata amounts,
bytes[] calldata payloads
) public payable nonReentrant virtual returns (uint256 proposal) {
if (accounts.length != amounts.length || amounts.length != payloads.length) revert NoArrayParity();
if (proposalType == ProposalType.VPERIOD) if (amounts[0] == 0 || amounts[0] > 365 days) revert PeriodBounds();
if (proposalType == ProposalType.GPERIOD) if (amounts[0] > 365 days) revert PeriodBounds();
if (proposalType == ProposalType.QUORUM) if (amounts[0] > 100) revert QuorumMax();
if (proposalType == ProposalType.SUPERMAJORITY) if (amounts[0] <= 51 || amounts[0] > 100) revert SupermajorityBounds();
if (proposalType == ProposalType.TYPE) if (amounts[0] > 11 || amounts[1] > 3 || amounts.length != 2) revert TypeBounds();
bool selfSponsor;
// if member or extension is making proposal, include sponsorship
if (balanceOf[msg.sender] != 0 || extensions[msg.sender]) selfSponsor = true;
// cannot realistically overflow on human timescales
unchecked {
proposalCount++;
}
proposal = proposalCount;
proposals[proposal] = Proposal({
proposalType: proposalType,
description: description,
accounts: accounts,
amounts: amounts,
payloads: payloads,
prevProposal: selfSponsor ? currentSponsoredProposal : 0,
yesVotes: 0,
noVotes: 0,
creationTime: selfSponsor ? _safeCastTo32(block.timestamp) : 0,
proposer: msg.sender
});
if (selfSponsor) currentSponsoredProposal = proposal;
emit NewProposal(msg.sender, proposal, proposalType, description, accounts, amounts, payloads);
}
function cancelProposal(uint256 proposal) public payable nonReentrant virtual {
Proposal storage prop = proposals[proposal];
if (msg.sender != prop.proposer) revert NotProposer();
if (prop.creationTime != 0) revert Sponsored();
delete proposals[proposal];
emit ProposalCancelled(msg.sender, proposal);
}
function sponsorProposal(uint256 proposal) public payable nonReentrant virtual {
Proposal storage prop = proposals[proposal];
if (balanceOf[msg.sender] == 0) revert NotMember();
if (prop.proposer == address(0)) revert NotCurrentProposal();
if (prop.creationTime != 0) revert Sponsored();
prop.prevProposal = currentSponsoredProposal;
currentSponsoredProposal = proposal;
prop.creationTime = _safeCastTo32(block.timestamp);
emit ProposalSponsored(msg.sender, proposal);
}
function vote(uint256 proposal, bool approve) public payable nonReentrant virtual {
_vote(msg.sender, proposal, approve);
}
function voteBySig(
address signer,
uint256 proposal,
bool approve,
uint8 v,
bytes32 r,
bytes32 s
) public payable nonReentrant virtual {
bytes32 digest =
keccak256(
abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
VOTE_HASH,
signer,
proposal,
approve
)
)
)
);
address recoveredAddress = ecrecover(digest, v, r, s);
if (recoveredAddress == address(0) || recoveredAddress != signer) revert InvalidSignature();
_vote(signer, proposal, approve);
}
function _vote(
address signer,
uint256 proposal,
bool approve
) internal virtual {
Proposal storage prop = proposals[proposal];
if (voted[proposal][signer]) revert AlreadyVoted();
// this is safe from overflow because `votingPeriod` is capped so it will not combine
// with unix time to exceed the max uint256 value
unchecked {
if (block.timestamp > prop.creationTime + votingPeriod) revert NotVoteable();
}
uint96 weight = getPriorVotes(signer, prop.creationTime);
// this is safe from overflow because `yesVotes` and `noVotes` are capped by `totalSupply`
// which is checked for overflow in `KaliDAOtoken` contract
unchecked {
if (approve) {
prop.yesVotes += weight;
lastYesVote[signer] = proposal;
} else {
prop.noVotes += weight;
}
}
voted[proposal][signer] = true;
emit VoteCast(signer, proposal, approve);
}
function processProposal(uint256 proposal) public payable nonReentrant virtual returns (
bool didProposalPass, bytes[] memory results
) {
Proposal storage prop = proposals[proposal];
VoteType voteType = proposalVoteTypes[prop.proposalType];
if (prop.creationTime == 0) revert NotCurrentProposal();
// this is safe from overflow because `votingPeriod` and `gracePeriod` are capped so they will not combine
// with unix time to exceed the max uint256 value
unchecked {
if (block.timestamp <= prop.creationTime + votingPeriod + gracePeriod) revert VotingNotEnded();
}
// skip previous proposal processing requirement in case of escape hatch
if (prop.proposalType != ProposalType.ESCAPE)
if (proposals[prop.prevProposal].creationTime != 0) revert PrevNotProcessed();
didProposalPass = _countVotes(voteType, prop.yesVotes, prop.noVotes);
if (didProposalPass) {
// cannot realistically overflow on human timescales
unchecked {
if (prop.proposalType == ProposalType.MINT)
for (uint256 i; i < prop.accounts.length; i++) {
_mint(prop.accounts[i], prop.amounts[i]);
}
if (prop.proposalType == ProposalType.BURN)
for (uint256 i; i < prop.accounts.length; i++) {
_burn(prop.accounts[i], prop.amounts[i]);
}
if (prop.proposalType == ProposalType.CALL)
for (uint256 i; i < prop.accounts.length; i++) {
results = new bytes[](prop.accounts.length);
(, bytes memory result) = prop.accounts[i].call{value: prop.amounts[i]}
(prop.payloads[i]);
results[i] = result;
}
// governance settings
if (prop.proposalType == ProposalType.VPERIOD)
if (prop.amounts[0] != 0) votingPeriod = uint32(prop.amounts[0]);
if (prop.proposalType == ProposalType.GPERIOD)
if (prop.amounts[0] != 0) gracePeriod = uint32(prop.amounts[0]);
if (prop.proposalType == ProposalType.QUORUM)
if (prop.amounts[0] != 0) quorum = uint32(prop.amounts[0]);
if (prop.proposalType == ProposalType.SUPERMAJORITY)
if (prop.amounts[0] != 0) supermajority = uint32(prop.amounts[0]);
if (prop.proposalType == ProposalType.TYPE)
proposalVoteTypes[ProposalType(prop.amounts[0])] = VoteType(prop.amounts[1]);
if (prop.proposalType == ProposalType.PAUSE)
_flipPause();
if (prop.proposalType == ProposalType.EXTENSION)
for (uint256 i; i < prop.accounts.length; i++) {
if (prop.amounts[i] != 0)
extensions[prop.accounts[i]] = !extensions[prop.accounts[i]];
if (prop.payloads[i].length > 3) IKaliDAOextension(prop.accounts[i])
.setExtension(prop.payloads[i]);
}
if (prop.proposalType == ProposalType.ESCAPE)
delete proposals[prop.amounts[0]];
if (prop.proposalType == ProposalType.DOCS)
docs = prop.description;
proposalStates[proposal].passed = true;
}
}
delete proposals[proposal];
proposalStates[proposal].processed = true;
emit ProposalProcessed(proposal, didProposalPass);
}
function _countVotes(
VoteType voteType,
uint256 yesVotes,
uint256 noVotes
) internal view virtual returns (bool didProposalPass) {
// fail proposal if no participation
if (yesVotes == 0 && noVotes == 0) return false;
// rule out any failed quorums
if (voteType == VoteType.SIMPLE_MAJORITY_QUORUM_REQUIRED || voteType == VoteType.SUPERMAJORITY_QUORUM_REQUIRED) {
uint256 minVotes = (totalSupply * quorum) / 100;
// this is safe from overflow because `yesVotes` and `noVotes`
// supply are checked in `KaliDAOtoken` contract
unchecked {
uint256 votes = yesVotes + noVotes;
if (votes < minVotes) return false;
}
}
// simple majority check
if (voteType == VoteType.SIMPLE_MAJORITY || voteType == VoteType.SIMPLE_MAJORITY_QUORUM_REQUIRED) {
if (yesVotes > noVotes) return true;
// supermajority check
} else {
// example: 7 yes, 2 no, supermajority = 66
// ((7+2) * 66) / 100 = 5.94; 7 yes will pass
uint256 minYes = ((yesVotes + noVotes) * supermajority) / 100;
if (yesVotes >= minYes) return true;
}
}
/*///////////////////////////////////////////////////////////////
EXTENSIONS
//////////////////////////////////////////////////////////////*/
receive() external payable virtual {}
modifier onlyExtension {
if (!extensions[msg.sender]) revert NotExtension();
_;
}
function callExtension(
address extension,
uint256 amount,
bytes calldata extensionData
) public payable nonReentrant virtual returns (bool mint, uint256 amountOut) {
if (!extensions[extension]) revert NotExtension();
(mint, amountOut) = IKaliDAOextension(extension).callExtension{value: msg.value}
(msg.sender, amount, extensionData);
if (mint) {
if (amountOut != 0) _mint(msg.sender, amountOut);
} else {
if (amountOut != 0) _burn(msg.sender, amount);
}
}
function mintShares(address to, uint256 amount) public payable onlyExtension virtual {
_mint(to, amount);
}
function burnShares(address from, uint256 amount) public payable onlyExtension virtual {
_burn(from, amount);
}
}