| 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 |
| // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "./interfaces/IDAOProposals.sol";
import "./interfaces/IDAOStaking.sol";
import "./interfaces/IDAOFactory.sol";
import "./interfaces/IDAOModule.sol";
import "./storage/CoreStorage.sol";
import "./storage/ProposalStorage.sol";
import "./internal/DAOEvents.sol";
/**
* @dev Implementation of proposal creation and voting functionality
* Uses ERC-7201 namespaced storage pattern
*/
abstract contract DAOProposals is Initializable, UUPSUpgradeable, OwnableUpgradeable, IDAOProposals {
using CoreStorage for CoreStorage.Layout;
using ProposalStorage for ProposalStorage.Layout;
using DAOEvents for *;
modifier whenNotPaused() {
require(!_getCore().paused, "DAO: paused");
_;
}
function _validateAndInitializeProposal() internal returns (uint256 proposalId) {
CoreStorage.Layout storage core = _getCore();
ProposalStorage.Layout storage proposals = _getProposals();
// Check proposer has enough stake
require(
IDAOStaking(core.upgradeableContracts[IDAOBase.UpgradeableContract.Staking])
.getVotingPower(msg.sender) >= core.minProposalStake,
"Insufficient stake"
);
proposalId = proposals.proposalCount++;
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.endTime = block.timestamp + core.votingPeriod;
return proposalId;
}
function proposeModuleUpgrade(
IDAOModule.ModuleType moduleType,
address moduleAddress,
string calldata newVersion
) external whenNotPaused returns (uint256) {
require(moduleAddress != address(0), "Zero module address");
CoreStorage.Layout storage core = _getCore();
ProposalStorage.Layout storage proposals = _getProposals();
// Verify new implementation exists
address newImpl = IDAOFactory(core.factory).getModuleImplementation(moduleType, newVersion);
require(newImpl != address(0), "Invalid version");
uint256 proposalId = _validateAndInitializeProposal();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.proposalType = IDAOBase.ProposalType.ModuleUpgrade;
// Store module upgrade data
proposals.moduleUpgradeData[proposalId].moduleType = moduleType;
proposals.moduleUpgradeData[proposalId].moduleAddress = moduleAddress;
proposals.moduleUpgradeData[proposalId].newVersion = newVersion;
DAOEvents.emitProposalCreated(
proposalId,
IDAOBase.ProposalType.ModuleUpgrade,
proposal.endTime
);
DAOEvents.emitModuleUpgradeProposalCreated(
proposalId,
moduleType,
moduleAddress,
newVersion
);
return proposalId;
}
function proposeTransfer(
address token,
address recipient,
uint256 amount
) external whenNotPaused returns (uint256) {
require(recipient != address(0), "Zero recipient");
require(amount > 0, "Zero amount");
ProposalStorage.Layout storage proposals = _getProposals();
uint256 proposalId = _validateAndInitializeProposal();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.proposalType = IDAOBase.ProposalType.Transfer;
// Set transfer specific data
proposals.transferData[proposalId].token = token;
proposals.transferData[proposalId].recipient = recipient;
proposals.transferData[proposalId].amount = amount;
DAOEvents.emitProposalCreated(
proposalId,
IDAOBase.ProposalType.Transfer,
proposal.endTime
);
DAOEvents.emitTransferProposalCreated(
proposalId,
token,
recipient,
amount
);
return proposalId;
}
function proposePresalePause(
address presaleContract,
bool pause
) external whenNotPaused returns (uint256) {
require(presaleContract != address(0), "Zero presale address");
ProposalStorage.Layout storage proposals = _getProposals();
uint256 proposalId = _validateAndInitializeProposal();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.proposalType = IDAOBase.ProposalType.PresalePause;
// Set presale pause data
proposals.presalePauseData[proposalId].presaleContract = presaleContract;
proposals.presalePauseData[proposalId].pause = pause;
DAOEvents.emitProposalCreated(
proposalId,
IDAOBase.ProposalType.PresalePause,
proposal.endTime
);
DAOEvents.emitPresalePauseProposalCreated(
proposalId,
presaleContract,
pause
);
return proposalId;
}
function proposePresaleWithdraw(
address presaleContract
) external whenNotPaused returns (uint256) {
require(presaleContract != address(0), "Zero presale address");
ProposalStorage.Layout storage proposals = _getProposals();
uint256 proposalId = _validateAndInitializeProposal();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.proposalType = IDAOBase.ProposalType.PresaleWithdraw;
// Set presale withdraw data
proposals.presaleWithdrawData[proposalId].presaleContract = presaleContract;
DAOEvents.emitProposalCreated(
proposalId,
IDAOBase.ProposalType.PresaleWithdraw,
proposal.endTime
);
DAOEvents.emitPresaleWithdrawProposalCreated(
proposalId,
presaleContract
);
return proposalId;
}
function proposePause() external whenNotPaused returns (uint256) {
CoreStorage.Layout storage core = _getCore();
ProposalStorage.Layout storage proposals = _getProposals();
require(!core.paused, "Already paused");
uint256 proposalId = _validateAndInitializeProposal();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.proposalType = IDAOBase.ProposalType.Pause;
DAOEvents.emitProposalCreated(
proposalId,
IDAOBase.ProposalType.Pause,
proposal.endTime
);
return proposalId;
}
function proposeUnpause() external returns (uint256) {
CoreStorage.Layout storage core = _getCore();
ProposalStorage.Layout storage proposals = _getProposals();
require(core.paused, "Not paused");
uint256 proposalId = _validateAndInitializeProposal();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.proposalType = IDAOBase.ProposalType.Unpause;
DAOEvents.emitProposalCreated(
proposalId,
IDAOBase.ProposalType.Unpause,
proposal.endTime
);
return proposalId;
}
function proposePresale(
uint256 tokenAmount,
uint256 initialPrice
) external whenNotPaused returns (uint256) {
require(tokenAmount > 0, "Zero token amount");
require(initialPrice > 0, "Zero initial price");
CoreStorage.Layout storage core = _getCore();
ProposalStorage.Layout storage proposals = _getProposals();
uint256 proposalId = _validateAndInitializeProposal();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.proposalType = IDAOBase.ProposalType.Presale;
// Set presale specific data
proposals.presaleData[proposalId].token = core.upgradeableContracts[
IDAOBase.UpgradeableContract.Token
];
proposals.presaleData[proposalId].amount = tokenAmount;
proposals.presaleData[proposalId].initialPrice = initialPrice;
DAOEvents.emitProposalCreated(
proposalId,
IDAOBase.ProposalType.Presale,
proposal.endTime
);
DAOEvents.emitPresaleProposalCreated(
proposalId,
core.upgradeableContracts[IDAOBase.UpgradeableContract.Token],
tokenAmount,
initialPrice
);
return proposalId;
}
function proposeUpgrade(
string calldata newVersion
) external whenNotPaused returns (uint256) {
CoreStorage.Layout storage core = _getCore();
ProposalStorage.Layout storage proposals = _getProposals();
(
address daoImpl,
address tokenImpl,
address treasuryImpl,
address stakingImpl
) = IDAOFactory(core.factory).getCoreImplementation(newVersion);
require(daoImpl != address(0), "Invalid version");
require(tokenImpl != address(0), "Invalid version");
require(treasuryImpl != address(0), "Invalid version");
require(stakingImpl != address(0), "Invalid version");
uint256 proposalId = _validateAndInitializeProposal();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
proposal.proposalType = IDAOBase.ProposalType.Upgrade;
// Store all implementations
address[] memory newImplementations = new address[](4);
newImplementations[0] = daoImpl;
newImplementations[1] = tokenImpl;
newImplementations[2] = treasuryImpl;
newImplementations[3] = stakingImpl;
proposals.upgradeData[proposalId].newImplementations = newImplementations;
proposals.upgradeData[proposalId].newVersion = newVersion;
DAOEvents.emitProposalCreated(
proposalId,
IDAOBase.ProposalType.Upgrade,
proposal.endTime
);
DAOEvents.emitUpgradeProposalCreated(
proposalId,
newImplementations,
newVersion
);
return proposalId;
}
function vote(uint256 proposalId, bool support) external {
CoreStorage.Layout storage core = _getCore();
ProposalStorage.Layout storage proposals = _getProposals();
ProposalStorage.Proposal storage proposal = proposals.proposals[proposalId];
require(block.timestamp < proposal.endTime, "Voting ended");
require(!proposal.hasVoted[msg.sender], "Already voted");
require(!proposal.executed, "Proposal already executed");
uint256 votingPower = IDAOStaking(
core.upgradeableContracts[IDAOBase.UpgradeableContract.Staking]
).getVotingPower(msg.sender);
require(votingPower > 0, "No voting power");
proposal.hasVoted[msg.sender] = true;
if (support) {
proposal.forVotes += votingPower;
} else {
proposal.againstVotes += votingPower;
}
DAOEvents.emitVoted(proposalId, msg.sender, support, votingPower);
}
function getModuleUpgradeData(uint256 proposalId)
external
view
returns (
IDAOModule.ModuleType moduleType,
address moduleAddress,
string memory newVersion
)
{
ProposalStorage.ModuleUpgradeData storage data = _getProposals().moduleUpgradeData[proposalId];
return (data.moduleType, data.moduleAddress, data.newVersion);
}
function getProposal(uint256 proposalId)
external
view
returns (
IDAOBase.ProposalType proposalType,
uint256 forVotes,
uint256 againstVotes,
uint256 endTime,
bool executed
)
{
ProposalStorage.Proposal storage proposal = _getProposals().proposals[proposalId];
return (
proposal.proposalType,
proposal.forVotes,
proposal.againstVotes,
proposal.endTime,
proposal.executed
);
}
function getTransferData(uint256 proposalId)
external
view
returns (address token, address recipient, uint256 amount)
{
ProposalStorage.TransferData storage data = _getProposals().transferData[proposalId];
return (data.token, data.recipient, data.amount);
}
function getUpgradeData(uint256 proposalId)
external
view
returns (
address[] memory newImplementations,
string memory newVersion
)
{
ProposalStorage.UpgradeData storage data = _getProposals().upgradeData[proposalId];
return (
data.newImplementations,
data.newVersion
);
}
function getPresaleData(uint256 proposalId)
external
view
returns (
address token,
uint256 amount,
uint256 initialPrice
)
{
ProposalStorage.PresaleData storage data = _getProposals().presaleData[proposalId];
return (data.token, data.amount, data.initialPrice);
}
function getPresalePauseData(uint256 proposalId)
external
view
returns (
address presaleContract,
bool pause
)
{
ProposalStorage.PresalePauseData storage data = _getProposals().presalePauseData[proposalId];
return (data.presaleContract, data.pause);
}
function getPresaleWithdrawData(uint256 proposalId)
external
view
returns (address presaleContract)
{
ProposalStorage.PresaleWithdrawData storage data = _getProposals().presaleWithdrawData[proposalId];
return data.presaleContract;
}
// IDAOBase implementation
function name() external view returns (string memory) {
return _getCore().name;
}
function factory() external view returns (address) {
return _getCore().factory;
}
function upgradeableContracts(IDAOBase.UpgradeableContract contractType) external view returns (address) {
return _getCore().upgradeableContracts[contractType];
}
function proposalCount() external view returns (uint256) {
return _getProposals().proposalCount;
}
function votingPeriod() external view returns (uint256) {
return _getCore().votingPeriod;
}
function minProposalStake() external view returns (uint256) {
return _getCore().minProposalStake;
}
function quorum() external view returns (uint256) {
return _getCore().quorum;
}
function paused() external view returns (bool) {
return _getCore().paused;
}
function _getCore() internal pure returns (CoreStorage.Layout storage) {
return CoreStorage.layout();
}
function _getProposals() internal pure returns (ProposalStorage.Layout storage) {
return ProposalStorage.layout();
}
}
|