-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPassContractor.sol
358 lines (281 loc) · 12.5 KB
/
PassContractor.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
import "PassProject.sol";
pragma solidity ^0.4.8;
/*
*
* This file is part of Pass DAO.
*
* The Project smart contract is used for the management of the Pass Dao projects.
*
*/
/// @title Contractor smart contract of the Pass Decentralized Autonomous Organisation
contract PassContractor {
// The project smart contract
PassProject passProject;
// The address of the creator of this smart contract
address public creator;
// Address of the recipient;
address public recipient;
// End date of the setup procedure
uint public smartContractStartDate;
struct proposal {
// Amount (in wei) of the proposal
uint amount;
// A description of the proposal
string description;
// The hash of the proposal's document
bytes32 hashOfTheDocument;
// A unix timestamp, denoting the date when the proposal was created
uint dateOfProposal;
// The amount submitted to a vote
uint submittedAmount;
// The sum amount (in wei) ordered for this proposal
uint orderAmount;
// A unix timestamp, denoting the date of the last order for the approved proposal
uint dateOfLastOrder;
}
// Proposals to work for Pass Dao
proposal[] public proposals;
// Events
event RecipientUpdated(address indexed By, address LastRecipient, address NewRecipient);
event Withdrawal(address indexed By, address indexed Recipient, uint Amount);
event ProposalAdded(address Creator, uint indexed ProposalID, uint Amount, string Description, bytes32 HashOfTheDocument);
event ProposalSubmitted(address indexed Client, uint Amount);
event Order(address indexed Client, uint indexed ProposalID, uint Amount);
// Constant functions
/// @return the actual committee room of the Dao
function Client() constant returns (address) {
return passProject.Client();
}
/// @return the project smart contract
function Project() constant returns (PassProject) {
return passProject;
}
/// @notice Function used by the client to check the proposal before submitting
/// @param _sender The creator of the Dao proposal
/// @param _proposalID The index of the proposal
/// @param _amount The amount of the proposal
/// @return true if the proposal can be submitted
function proposalChecked(
address _sender,
uint _proposalID,
uint _amount) constant external onlyClient returns (bool) {
if (_sender != recipient && _sender != creator) return;
if (_amount <= proposals[_proposalID].amount - proposals[_proposalID].submittedAmount) return true;
}
/// @return The number of proposals
function numberOfProposals() constant returns (uint) {
return proposals.length - 1;
}
// Modifiers
// Modifier for contractor functions
modifier onlyContractor {if (msg.sender != recipient) throw; _;}
// Modifier for client functions
modifier onlyClient {if (msg.sender != Client()) throw; _;}
// Constructor function
function PassContractor(
address _creator,
PassProject _passProject,
address _recipient,
bool _restore) {
if (address(_passProject) == 0) throw;
creator = _creator;
if (_recipient == 0) _recipient = _creator;
recipient = _recipient;
passProject = _passProject;
if (!_restore) smartContractStartDate = now;
proposals.length = 1;
}
// Setting functions
/// @notice Function to clone a proposal from the last contractor
/// @param _amount Amount (in wei) of the proposal
/// @param _description A description of the proposal
/// @param _hashOfTheDocument The hash of the proposal's document
/// @param _dateOfProposal A unix timestamp, denoting the date when the proposal was created
/// @param _orderAmount The sum amount (in wei) ordered for this proposal
/// @param _dateOfOrder A unix timestamp, denoting the date of the last order for the approved proposal
/// @param _cloneOrder True if the order has to be cloned in the project smart contract
/// @return Whether the function was successful or not
function cloneProposal(
uint _amount,
string _description,
bytes32 _hashOfTheDocument,
uint _dateOfProposal,
uint _orderAmount,
uint _dateOfOrder,
bool _cloneOrder
) returns (bool success) {
if (smartContractStartDate != 0 || recipient == 0
|| msg.sender != creator) throw;
uint _proposalID = proposals.length++;
proposal c = proposals[_proposalID];
c.amount = _amount;
c.description = _description;
c.hashOfTheDocument = _hashOfTheDocument;
c.dateOfProposal = _dateOfProposal;
c.orderAmount = _orderAmount;
c.dateOfLastOrder = _dateOfOrder;
ProposalAdded(msg.sender, _proposalID, _amount, _description, _hashOfTheDocument);
if (_cloneOrder) passProject.cloneOrder(address(this), _proposalID, _orderAmount, _dateOfOrder);
return true;
}
/// @notice Function to close the setting procedure and start to use this smart contract
/// @return True if successful
function closeSetup() returns (bool) {
if (smartContractStartDate != 0
|| (msg.sender != creator && msg.sender != Client())) return;
smartContractStartDate = now;
return true;
}
// Account Management
/// @notice Function to update the recipent address
/// @param _newRecipient The adress of the recipient
function updateRecipient(address _newRecipient) onlyContractor {
if (_newRecipient == 0) throw;
RecipientUpdated(msg.sender, recipient, _newRecipient);
recipient = _newRecipient;
}
/// @notice Function to receive payments
function () payable { }
/// @notice Function to allow contractors to withdraw ethers
/// @param _amount The amount (in wei) to withdraw
function withdraw(uint _amount) onlyContractor {
if (!recipient.send(_amount)) throw;
Withdrawal(msg.sender, recipient, _amount);
}
// Project Manager Functions
/// @notice Function to allow the project manager updating the description of the project
/// @param _projectDescription A description of the project
/// @param _hashOfTheDocument The hash of the last document
function updateProjectDescription(string _projectDescription, bytes32 _hashOfTheDocument) onlyContractor {
passProject.updateDescription(_projectDescription, _hashOfTheDocument);
}
// Management of proposals
/// @notice Function to make a proposal to work for the client
/// @param _creator The address of the creator of the proposal
/// @param _amount The amount (in wei) of the proposal
/// @param _description String describing the proposal
/// @param _hashOfTheDocument The hash of the proposal document
/// @return The index of the contractor proposal
function newProposal(
address _creator,
uint _amount,
string _description,
bytes32 _hashOfTheDocument
) external returns (uint) {
if (msg.sender == Client() && _creator != recipient && _creator != creator) throw;
if (msg.sender != Client() && msg.sender != recipient && msg.sender != creator) throw;
if (_amount == 0) throw;
uint _proposalID = proposals.length++;
proposal c = proposals[_proposalID];
c.amount = _amount;
c.description = _description;
c.hashOfTheDocument = _hashOfTheDocument;
c.dateOfProposal = now;
ProposalAdded(msg.sender, _proposalID, c.amount, c.description, c.hashOfTheDocument);
return _proposalID;
}
/// @notice Function used by the client to infor about the submitted amount
/// @param _sender The address of the sender who submits the proposal
/// @param _proposalID The index of the contractor proposal
/// @param _amount The amount (in wei) submitted
function submitProposal(
address _sender,
uint _proposalID,
uint _amount) onlyClient {
if (_sender != recipient && _sender != creator) throw;
proposals[_proposalID].submittedAmount += _amount;
ProposalSubmitted(msg.sender, _amount);
}
/// @notice Function used by the client to order according to the contractor proposal
/// @param _proposalID The index of the contractor proposal
/// @param _orderAmount The amount (in wei) of the order
/// @return Whether the order was made or not
function order(
uint _proposalID,
uint _orderAmount
) external onlyClient returns (bool) {
proposal c = proposals[_proposalID];
uint _sum = c.orderAmount + _orderAmount;
if (_sum > c.amount
|| _sum < c.orderAmount
|| _sum < _orderAmount) return;
c.orderAmount = _sum;
c.dateOfLastOrder = now;
Order(msg.sender, _proposalID, _orderAmount);
return true;
}
}
contract PassContractorCreator {
// Address of the pass Dao smart contract
PassDao public passDao;
// Address of the Pass Project creator
PassProjectCreator public projectCreator;
struct contractor {
// The address of the creator of the contractor
address creator;
// The contractor smart contract
PassContractor contractor;
// The address of the recipient for withdrawals
address recipient;
// True if meta project
bool metaProject;
// The address of the existing project smart contract
PassProject passProject;
// The name of the project (if the project smart contract doesn't exist)
string projectName;
// A description of the project (can be updated after)
string projectDescription;
// The unix creation date of the contractor
uint creationDate;
}
// contractors created to work for Pass Dao
contractor[] public contractors;
event NewPassContractor(address indexed Creator, address indexed Recipient, PassProject indexed Project, PassContractor Contractor);
function PassContractorCreator(PassDao _passDao, PassProjectCreator _projectCreator) {
passDao = _passDao;
projectCreator = _projectCreator;
contractors.length = 0;
}
/// @return The number of created contractors
function numberOfContractors() constant returns (uint) {
return contractors.length;
}
/// @notice Function to create a contractor smart contract
/// @param _creator The address of the creator of the contractor
/// @param _recipient The address of the recipient for withdrawals
/// @param _metaProject True if meta project
/// @param _passProject The address of the existing project smart contract
/// @param _projectName The name of the project (if the project smart contract doesn't exist)
/// @param _projectDescription A description of the project (can be updated after)
/// @param _restore True if orders or proposals are to be cloned from other contracts
/// @return The address of the created contractor smart contract
function createContractor(
address _creator,
address _recipient,
bool _metaProject,
PassProject _passProject,
string _projectName,
string _projectDescription,
bool _restore) returns (PassContractor) {
PassProject _project;
if (_creator == 0) _creator = msg.sender;
if (_metaProject) _project = PassProject(passDao.MetaProject());
else if (address(_passProject) == 0)
_project = projectCreator.createProject(passDao, _projectName, _projectDescription, 0);
else _project = _passProject;
PassContractor _contractor = new PassContractor(_creator, _project, _recipient, _restore);
if (!_metaProject && address(_passProject) == 0 && !_restore) _project.setProjectManager(address(_contractor));
uint _contractorID = contractors.length++;
contractor c = contractors[_contractorID];
c.creator = _creator;
c.contractor = _contractor;
c.recipient = _recipient;
c.metaProject = _metaProject;
c.passProject = _passProject;
c.projectName = _projectName;
c.projectDescription = _projectDescription;
c.creationDate = now;
NewPassContractor(_creator, _recipient, _project, _contractor);
return _contractor;
}
}