-
Notifications
You must be signed in to change notification settings - Fork 3
/
MVDFunctionalityProposal.sol
417 lines (361 loc) · 13.8 KB
/
MVDFunctionalityProposal.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
pragma solidity ^0.6.0;
import "./IMVDFunctionalityProposal.sol";
import "./IMVDProxy.sol";
import "./IERC20.sol";
contract MVDFunctionalityProposal is IMVDFunctionalityProposal{
bool private _collateralDataSet;
address private _proxy;
address private _token;
string private _codeName;
bool private _emergency;
address private _sourceLocation;
uint256 private _sourceLocationId;
address private _location;
bool private _submitable;
string private _methodSignature;
string private _returnAbiParametersArray;
bool private _isInternal;
bool private _needsSender;
string private _replaces;
uint256 private _surveyEndBlock;
uint256 private _surveyDuration;
bool private _terminated;
address private _proposer;
bool private _disabled;
mapping(address => uint256) private _accept;
mapping(address => uint256) private _refuse;
uint256 private _totalAccept;
uint256 private _totalRefuse;
mapping(address => bool) private _withdrawed;
uint256 private _votesHardCap;
bool private _votesHardCapReached;
constructor(string memory codeName, address location, string memory methodSignature, string memory returnAbiParametersArray,
string memory replaces, address proxy) public {
init(codeName, location, methodSignature, returnAbiParametersArray, replaces, proxy);
}
function init(string memory codeName, address location, string memory methodSignature, string memory returnAbiParametersArray,
string memory replaces, address proxy) public override {
require(_proxy == address(0), "Already initialized!");
_token = IMVDProxy(_proxy = proxy).getToken();
_codeName = codeName;
_location = location;
_methodSignature = methodSignature;
_returnAbiParametersArray = returnAbiParametersArray;
_replaces = replaces;
}
function setCollateralData(bool emergency, address sourceLocation, uint256 sourceLocationId, bool submitable, bool isInternal, bool needsSender, address proposer, uint256 votesHardCap) public override {
require(!_collateralDataSet, "setCollateralData already called!");
require(_proxy == msg.sender, "Only Original Proxy can call this method!");
_sourceLocation = sourceLocation;
_sourceLocationId = sourceLocationId;
_submitable = submitable;
_isInternal = isInternal;
_needsSender = needsSender;
_proposer = proposer;
_surveyDuration = toUint256(IMVDProxy(_proxy).read((_emergency = emergency) ? "getMinimumBlockNumberForEmergencySurvey" : "getMinimumBlockNumberForSurvey", bytes("")));
_votesHardCap = votesHardCap;
_collateralDataSet = true;
}
function getProxy() public override view returns(address) {
return _proxy;
}
function getCodeName() public override view returns(string memory) {
return _codeName;
}
function isEmergency() public override view returns(bool) {
return _emergency;
}
function getSourceLocation() public override view returns(address) {
return _sourceLocation;
}
function getSourceLocationId() public override view returns(uint256) {
return _sourceLocationId;
}
function getLocation() public override view returns(address) {
return _location;
}
function isSubmitable() public override view returns(bool) {
return _submitable;
}
function getMethodSignature() public override view returns(string memory) {
return _methodSignature;
}
function getReturnAbiParametersArray() public override view returns(string memory) {
return _returnAbiParametersArray;
}
function isInternal() public override view returns(bool) {
return _isInternal;
}
function needsSender() public override view returns(bool) {
return _needsSender;
}
function getReplaces() public override view returns(string memory) {
return _replaces;
}
function getProposer() public override view returns(address) {
return _proposer;
}
function getSurveyEndBlock() public override view returns(uint256) {
return _surveyEndBlock;
}
function getSurveyDuration() public override view returns(uint256) {
return _surveyDuration;
}
function getVote(address addr) public override view returns(uint256 accept, uint256 refuse) {
accept = _accept[addr];
refuse = _refuse[addr];
}
function getVotes() public override view returns(uint256, uint256) {
return (_totalAccept, _totalRefuse);
}
function isTerminated() public override view returns(bool) {
return _terminated;
}
function isDisabled() public override view returns(bool) {
return _disabled;
}
function isVotesHardCapReached() public override view returns(bool) {
return _votesHardCapReached;
}
function getVotesHardCapToReach() public override view returns(uint256) {
return _votesHardCap;
}
function start() public override {
require(_collateralDataSet, "Still waiting for setCollateralData to be called!");
require(msg.sender == _proxy, "Only Proxy can call this function!");
require(_surveyEndBlock == 0, "Already started!");
require(!_disabled, "Already disabled!");
_surveyEndBlock = block.number + _surveyDuration;
}
function disable() public override {
require(_collateralDataSet, "Still waiting for setCollateralData to be called!");
require(msg.sender == _proxy, "Only Proxy can call this function!");
require(_surveyEndBlock == 0, "Already started!");
_disabled = true;
_terminated = true;
}
function toJSON() public override view returns(string memory) {
return string(abi.encodePacked(
'{',
getFirstJSONPart(_sourceLocation, _sourceLocationId, _location),
'","submitable":',
_submitable ? "true" : "false",
',"emergency":',
_emergency ? "true" : "false",
',"isInternal":',
_isInternal ? "true" : "false",
',"needsSender":',
_needsSender ? "true" : "false",
',',
getSecondJSONPart(),
',"proposer":"',
toString(_proposer),
'","endBlock":',
toString(_surveyEndBlock),
',"terminated":',
_terminated ? "true" : "false",
',"accepted":',
toString(_totalAccept),
',"refused":',
toString(_totalRefuse),
',"disabled":',
_disabled ? 'true' : 'false',
'}')
);
}
function getSecondJSONPart() private view returns (string memory){
return string(abi.encodePacked(
'"codeName":"',
_codeName,
'","methodSignature":"',
_methodSignature,
'","returnAbiParametersArray":',
formatReturnAbiParametersArray(_returnAbiParametersArray),
',"replaces":"',
_replaces,
'"'));
}
modifier duringSurvey() {
require(_collateralDataSet, "Still waiting for setCollateralData to be called!");
require(!_disabled, "Survey disabled!");
require(!_terminated, "Survey Terminated!");
require(!_votesHardCapReached, "Votes Hard Cap reached!");
require(_surveyEndBlock > 0, "Survey Not Started!");
require(block.number < _surveyEndBlock, "Survey ended!");
_;
}
modifier onSurveyEnd() {
require(_collateralDataSet, "Still waiting for setCollateralData to be called!");
require(!_disabled, "Survey disabled!");
require(_surveyEndBlock > 0, "Survey Not Started!");
if(!_votesHardCapReached) {
require(block.number >= _surveyEndBlock, "Survey is still running!");
}
_;
}
function _checkVotesHardCap() private {
if(_votesHardCap == 0 || (_totalAccept < _votesHardCap && _totalRefuse < _votesHardCap)) {
return;
}
_votesHardCapReached = true;
terminate();
}
function accept(uint256 amount) external override duringSurvey {
IERC20(_token).transferFrom(msg.sender, address(this), amount);
uint256 vote = _accept[msg.sender];
vote += amount;
_accept[msg.sender] = vote;
_totalAccept += amount;
emit Accept(msg.sender, amount);
_checkVotesHardCap();
}
function retireAccept(uint256 amount) external override duringSurvey {
require(_accept[msg.sender] >= amount, "Insufficient funds!");
IERC20(_token).transfer(msg.sender, amount);
uint256 vote = _accept[msg.sender];
vote -= amount;
_accept[msg.sender] = vote;
_totalAccept -= amount;
emit RetireAccept(msg.sender, amount);
}
function moveToAccept(uint256 amount) external override duringSurvey {
require(_refuse[msg.sender] >= amount, "Insufficient funds!");
uint256 vote = _refuse[msg.sender];
vote -= amount;
_refuse[msg.sender] = vote;
_totalRefuse -= amount;
vote = _accept[msg.sender];
vote += amount;
_accept[msg.sender] = vote;
_totalAccept += amount;
emit MoveToAccept(msg.sender, amount);
_checkVotesHardCap();
}
function refuse(uint256 amount) external override duringSurvey {
IERC20(_token).transferFrom(msg.sender, address(this), amount);
uint256 vote = _refuse[msg.sender];
vote += amount;
_refuse[msg.sender] = vote;
_totalRefuse += amount;
emit Refuse(msg.sender, amount);
_checkVotesHardCap();
}
function retireRefuse(uint256 amount) external override duringSurvey {
require(_refuse[msg.sender] >= amount, "Insufficient funds!");
IERC20(_token).transfer(msg.sender, amount);
uint256 vote = _refuse[msg.sender];
vote -= amount;
_refuse[msg.sender] = vote;
_totalRefuse -= amount;
emit RetireRefuse(msg.sender, amount);
}
function moveToRefuse(uint256 amount) external override duringSurvey {
require(_accept[msg.sender] >= amount, "Insufficient funds!");
uint256 vote = _accept[msg.sender];
vote -= amount;
_accept[msg.sender] = vote;
_totalAccept -= amount;
vote = _refuse[msg.sender];
vote += amount;
_refuse[msg.sender] = vote;
_totalRefuse += amount;
emit MoveToRefuse(msg.sender, amount);
_checkVotesHardCap();
}
function retireAll() external override duringSurvey {
require(_accept[msg.sender] + _refuse[msg.sender] > 0, "No votes!");
uint256 acpt = _accept[msg.sender];
uint256 rfs = _refuse[msg.sender];
IERC20(_token).transfer(msg.sender, acpt + rfs);
_accept[msg.sender] = 0;
_refuse[msg.sender] = 0;
_totalAccept -= acpt;
_totalRefuse -= rfs;
emit RetireAll(msg.sender, acpt + rfs);
}
function withdraw() external override onSurveyEnd {
if(!_terminated && !_disabled) {
terminate();
return;
}
_withdraw(true);
}
function terminate() public override onSurveyEnd {
require(!_terminated, "Already terminated!");
IMVDProxy(_proxy).setProposal();
_withdraw(false);
}
function _withdraw(bool launchError) private {
require(!launchError || _accept[msg.sender] + _refuse[msg.sender] > 0, "Nothing to Withdraw!");
require(!launchError || !_withdrawed[msg.sender], "Already Withdrawed!");
if(_accept[msg.sender] + _refuse[msg.sender] > 0 && !_withdrawed[msg.sender]) {
IERC20(_token).transfer(msg.sender, _accept[msg.sender] + _refuse[msg.sender]);
_withdrawed[msg.sender] = true;
}
}
function set() public override onSurveyEnd {
require(msg.sender == _proxy, "Unauthorized Access!");
require(!_terminated, "Already terminated!");
_terminated = true;
}
function toUint256(bytes memory bs) public pure returns(uint256 x) {
if(bs.length >= 32) {
assembly {
x := mload(add(bs, add(0x20, 0)))
}
}
}
function toString(address _addr) public pure returns(string memory) {
bytes32 value = bytes32(uint256(_addr));
bytes memory alphabet = "0123456789abcdef";
bytes memory str = new bytes(42);
str[0] = '0';
str[1] = 'x';
for (uint i = 0; i < 20; i++) {
str[2+i*2] = alphabet[uint(uint8(value[i + 12] >> 4))];
str[3+i*2] = alphabet[uint(uint8(value[i + 12] & 0x0f))];
}
return string(str);
}
function toString(uint _i) public pure returns(string memory) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
while (_i != 0) {
bstr[k--] = byte(uint8(48 + _i % 10));
_i /= 10;
}
return string(bstr);
}
function getFirstJSONPart(address sourceLocation, uint256 sourceLocationId, address location) public pure returns(bytes memory) {
return abi.encodePacked(
'"sourceLocation":"',
toString(sourceLocation),
'","sourceLocationId":',
toString(sourceLocationId),
',"location":"',
toString(location)
);
}
function formatReturnAbiParametersArray(string memory m) public pure returns(string memory) {
bytes memory b = bytes(m);
if(b.length < 2) {
return "[]";
}
if(b[0] != bytes1("[")) {
return "[]";
}
if(b[b.length - 1] != bytes1("]")) {
return "[]";
}
return m;
}
}