-
Notifications
You must be signed in to change notification settings - Fork 35
/
TaskList.sol
146 lines (126 loc) · 4.64 KB
/
TaskList.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
pragma solidity >=0.4.22 <0.6.0;
contract TaskList {
enum STATUS {NONE, OPEN, EXECUTING, REVIEW, REJECT, DONE}
struct Task {
string infourl; // wiki link to the task details
uint timestamp; //timestamp of the last status update
STATUS status;
uint expiry; // must finish before the expiry
uint stakereq; // stake requirement
address taskowner; // owner of this task
address delegate; // taker of the task;
string resulturl;
uint prize;
}
enum ROLE {NONE, TASKOWNER, SERVICE, ADMIN}
address owner;
mapping (address => Task[]) public tasklist;
mapping (address => bool) taskownerlist;
mapping (address => bool) servicelist;
mapping (address => bool) adminlist;
address[] public taskaddresses;
constructor() public {
owner = msg.sender;
}
function transferOwner (address newOwner) public {
if (msg.sender == owner) {
owner = newOwner;
}
}
function addTaskOwner (address entity) public {
if (msg.sender == owner || adminlist[msg.sender]) {
taskownerlist[entity] = true;
}
}
function addService (address entity) public {
if (msg.sender == owner || adminlist[msg.sender]) {
servicelist[entity] = true;
}
}
function addAdmin (address entity) public {
if (msg.sender == owner) {
adminlist[entity] = true;
}
}
/// add a new task to the list
/// pass address(0) in delegate to open the task for all
function addTask (string memory infourl, uint expiry, uint prize, address delegate) public returns (int) {
if (taskownerlist[msg.sender] == false) {
return -1;
}
Task[] storage tasks = tasklist[msg.sender];
uint index = 0;
for (index = 0; index < tasks.length; index++) {
if (tasks[index].status == STATUS.DONE) break;
}
Task memory task;
task.infourl = infourl;
task.timestamp = block.timestamp;
task.expiry = expiry;
task.prize = prize;
task.taskowner = msg.sender;
task.delegate = delegate;
task.status = STATUS.OPEN;
if (index == tasks.length) {
if (index == 0) {
// new owner
taskaddresses.push(msg.sender);
}
tasks.push(task);
} else {
tasks[index] = task;
}
return int(index);
}
/// take the task
function takeTask (address taskowner, uint index) public returns (bool) {
if (servicelist[msg.sender] == false) {
return false;
}
if (index >= tasklist[taskowner].length) return false;
Task storage task = tasklist[taskowner][index];
if (task.status == STATUS.OPEN && (task.delegate == address(0) || task.delegate == msg.sender)) {
task.timestamp = block.timestamp;
task.delegate = msg.sender;
task.status = STATUS.EXECUTING;
return true;
}
return false;
}
/// put the task to review
function finshTask (address taskowner, uint index, string memory resulturl) public returns (bool) {
if (servicelist[msg.sender] == false) {
return false;
}
if (index >= tasklist[taskowner].length) return false;
Task storage task = tasklist[taskowner][index];
if (task.delegate == msg.sender && (task.status == STATUS.EXECUTING || task.status == STATUS.REJECT)) {
task.resulturl = resulturl;
task.status = STATUS.REVIEW;
return true;
}
return false;
}
/// review verdict
function reviewTask(uint index, bool verdict) public returns (bool) {
if (taskownerlist[msg.sender] == false) return false;
if (index >= tasklist[msg.sender].length) return false;
Task storage task = tasklist[msg.sender][index];
if (task.status == STATUS.REVIEW) {
if (verdict == true) {
task.status = STATUS.DONE;
}
else {
task.status = STATUS.REJECT;
}
return true;
}
return false;
}
function getNumTaskLists() public view returns (uint) {
return taskaddresses.length;
}
function getNumTaskByAddress(address taskowner) public view returns (uint) {
return tasklist[taskowner].length;
}
}