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
| pragma solidity >=0.4.22 <0.7.0; pragma experimental ABIEncoderV2;
contract Bounty { /* 金主发布悬赏 Created 金主取消任务 Cancelled 金主确认猎人 Confirmed 猎人A提交任务 HunterFinished 猎人A超时未提交任务 HunterOutOfTime 金主确认任务完成 Promoterfinished 金主超时未确认任务 PromoterOutOfTime */ enum State { Created, Cancelled, Confirmed, HunterFinished, HunterOutOfTime, Promoterfinished, PromoterOutOfTime} uint public id = 0; struct TaskMessage { string hash; address promoter; address hunterA; address[] hunters; string[] reasonHashs; uint bonus; uint taskStart; uint taskDeadLine; State state; }
mapping(uint => TaskMessage) public tasks; // receive() external payable { } // fallback () external payable { } modifier onlyPromoter(uint _uid) { require(msg.sender == tasks[_uid].promoter); _; } modifier onlyhunterA(uint _uid) { require(msg.sender == tasks[_uid].hunterA); _; } modifier inState(uint _uid, State _state) { require(tasks[_uid].state == _state ); _; } modifier inTime(uint _uid) { require(now <= tasks[_uid].taskStart + tasks[_uid].taskDeadLine); _; } modifier outOfTime(uint _uid) { require(now > tasks[_uid].taskStart + tasks[_uid].taskDeadLine); _; } event SetTask(uint _uid, TaskMessage _task); //金主发布悬赏。 function setTask(string memory _hash, uint _bonus, uint _hour, uint _minunt) public { id += 1; tasks[id].hash = _hash; tasks[id].promoter = msg.sender; tasks[id].bonus = _bonus; tasks[id].taskDeadLine = _hour * 1 hours + _minunt * 1 minutes; token.transferFromOrigin(address(this), _bonus+_bonus/2); emit SetTask(id, tasks[id]); }
//猎人申请任务 function applyTask(uint _uid, string memory _reasonHashs) public inState(_uid, State.Created) { require(tasks[_uid].promoter != msg.sender); tasks[_uid].hunters.push(msg.sender); tasks[_uid].reasonHashs.push(_reasonHashs); } //金主取消任务 function cancelTask(uint _uid) public inState(_uid, State.Created) onlyPromoter(_uid) { token.transfer(msg.sender, tasks[_uid].bonus+tasks[_uid].bonus/2); tasks[_uid].state = State.Cancelled; } //金主确认猎人A function confirmHunter(uint _uid, address a) public inState(_uid, State.Created) onlyPromoter(_uid) { tasks[_uid].hunterA = a; tasks[_uid].taskStart = now; tasks[_uid].state = State.Confirmed; } //猎人A提交任务 function hunterAFinished(uint _uid) public inTime(_uid) inState(_uid, State.Confirmed) onlyhunterA(_uid) { tasks[_uid].taskStart = now; tasks[_uid].state = State.HunterFinished; } //猎人A超时未提交任务 function hunterAOutOfTime(uint _uid) public outOfTime(_uid) onlyPromoter(_uid) { token.transfer(msg.sender, tasks[_uid].bonus+tasks[_uid].bonus/2); tasks[_uid].state = State.HunterOutOfTime; } //金主确认任务完成 function promoterFinished(uint _uid) public inTime(_uid) inState(_uid, State.HunterFinished) onlyPromoter(_uid) { token.transfer(msg.sender, tasks[_uid].bonus/2); token.transfer(tasks[_uid].hunterA, tasks[_uid].bonus); tasks[_uid].state = State.Promoterfinished; } //金主退回任务 function taskFallback(uint _uid) public inTime(_uid) inState(_uid, State.HunterFinished) onlyPromoter(_uid) { tasks[_uid].taskStart = now; tasks[_uid].state = State.Confirmed; } //金主超时未确认任务 function promoterOutOfTime(uint _uid) public outOfTime(_uid) inState(_uid, State.HunterFinished) onlyhunterA(_uid) { token.transfer(msg.sender, tasks[_uid].bonus); tasks[_uid].state = State.PromoterOutOfTime; } //获取任务信息 function getTask(uint _uid) public view returns (TaskMessage memory) { return (tasks[_uid]); } }
|