File size: 8,555 Bytes
dec6d5d |
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 |
pragma solidity ^0.4.0;
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
contract WorkIt is ERC20Interface {
string public constant name = "WorkIt Token";
string public constant symbol = "WIT";
uint _totalSupply = 0;
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowances;
function totalSupply() public constant returns (uint) {
return _totalSupply;
}
function balanceOf(address tokenOwner) public constant returns (uint balance) {
return balances[tokenOwner];
}
function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
return allowances[tokenOwner][spender];
}
function transfer(address to, uint tokens) public returns (bool success) {
require(balances[msg.sender] >= tokens);
balances[msg.sender] = balances[msg.sender] - tokens;
balances[to] = balances[to] + tokens;
emit Transfer(msg.sender, to, tokens);
return true;
}
function approve(address spender, uint tokens) public returns (bool success) {
allowances[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
require(allowances[from][msg.sender] >= tokens);
require(balances[from] >= tokens);
allowances[from][msg.sender] = allowances[from][msg.sender] - tokens;
balances[from] = balances[from] - tokens;
balances[to] = balances[to] + tokens;
emit Transfer(from, to, tokens);
return true;
}
struct WeekCommittment {
uint daysCompleted;
uint daysCommitted;
mapping(uint => uint) workoutProofs;
uint tokensCommitted;
uint tokensEarned;
bool tokensPaid;
}
struct WeekData {
bool initialized;
uint totalPeopleCompleted;
uint totalPeople;
uint totalDaysCommitted;
uint totalDaysCompleted;
uint totalTokensCompleted;
uint totalTokens;
}
uint public weiPerToken = 1000000000000000;
uint secondsPerDay = 86400;
uint daysPerWeek = 7;
mapping(uint => WeekData) public dataPerWeek;
mapping (address => mapping(uint => WeekCommittment)) public commitments;
mapping(uint => string) imageHashes;
uint imageHashCount;
uint public startDate;
address public owner;
constructor() public {
owner = msg.sender;
startDate = (block.timestamp / secondsPerDay) * secondsPerDay - 60 * 6;
}
event Log(string message);
function () public payable {
buyTokens(msg.value / weiPerToken);
}
function buyTokens(uint tokens) public payable {
require(msg.value >= tokens * weiPerToken);
balances[msg.sender] += tokens;
_totalSupply += tokens;
}
function commitToWeek(uint tokens, uint _days) public {
if (balances[msg.sender] < tokens || tokens < 10) {
emit Log("You need to bet at least 10 tokens to commit");
require(false);
}
if (_days == 0) {
emit Log("You cannot register for 0 days of activity");
require(false);
}
if (_days > daysPerWeek) {
emit Log("You cannot register for more than 7 days per week");
require(false);
}
if (_days > daysPerWeek - currentDayOfWeek()) {
emit Log("It is too late in the week for you to register");
require(false);
}
WeekCommittment storage commitment = commitments[msg.sender][currentWeek()];
if (commitment.tokensCommitted != 0) {
emit Log("You have already committed to this week");
require(false);
}
balances[0x0] = balances[0x0] + tokens;
balances[msg.sender] = balances[msg.sender] - tokens;
emit Transfer(msg.sender, 0x0, tokens);
initializeWeekData(currentWeek());
WeekData storage data = dataPerWeek[currentWeek()];
data.totalPeople++;
data.totalTokens += tokens;
data.totalDaysCommitted += _days;
commitment.daysCommitted = _days;
commitment.daysCompleted = 0;
commitment.tokensCommitted = tokens;
commitment.tokensEarned = 0;
commitment.tokensPaid = false;
}
function payout() public {
require(currentWeek() > 0);
for (uint activeWeek = currentWeek() - 1; true; activeWeek--) {
WeekCommittment storage committment = commitments[msg.sender][activeWeek];
if (committment.tokensPaid) {
break;
}
if (committment.daysCommitted == 0) {
committment.tokensPaid = true;
if (activeWeek == 0) break;
continue;
}
initializeWeekData(activeWeek);
WeekData storage week = dataPerWeek[activeWeek];
uint tokensFromPool = 0;
uint tokens = committment.tokensCommitted * committment.daysCompleted / committment.daysCommitted;
if (week.totalPeopleCompleted == 0) {
tokensFromPool = (week.totalTokens - week.totalTokensCompleted) / week.totalPeople;
tokens = 0;
} else if (committment.daysCompleted == committment.daysCommitted) {
tokensFromPool = (week.totalTokens - week.totalTokensCompleted) / week.totalPeopleCompleted;
}
uint totalTokens = tokensFromPool + tokens;
if (totalTokens == 0) {
committment.tokensPaid = true;
if (activeWeek == 0) break;
continue;
}
balances[0x0] = balances[0x0] - totalTokens;
balances[msg.sender] = balances[msg.sender] + totalTokens;
emit Transfer(0x0, msg.sender, totalTokens);
committment.tokensEarned = totalTokens;
committment.tokensPaid = true;
if (activeWeek == 0) break;
}
}
function postProof(string proofHash) public {
WeekCommittment storage committment = commitments[msg.sender][currentWeek()];
if (committment.daysCompleted > currentDayOfWeek()) {
emit Log("You have already uploaded proof for today");
require(false);
}
if (committment.tokensCommitted == 0) {
emit Log("You have not committed to this week yet");
require(false);
}
if (committment.workoutProofs[currentDayOfWeek()] != 0) {
emit Log("Proof has already been stored for this day");
require(false);
}
if (committment.daysCompleted >= committment.daysCommitted) {
return;
}
committment.workoutProofs[currentDayOfWeek()] = storeImageString(proofHash);
committment.daysCompleted++;
initializeWeekData(currentWeek());
WeekData storage week = dataPerWeek[currentWeek()];
week.totalDaysCompleted++;
week.totalTokensCompleted = week.totalTokens * week.totalDaysCompleted / week.totalDaysCommitted;
if (committment.daysCompleted >= committment.daysCommitted) {
week.totalPeopleCompleted++;
}
}
function withdraw(uint tokens) public returns (bool success) {
require(balances[msg.sender] >= tokens);
uint weiToSend = tokens * weiPerToken;
require(address(this).balance >= weiToSend);
balances[msg.sender] = balances[msg.sender] - tokens;
_totalSupply -= tokens;
return msg.sender.send(tokens * weiPerToken);
}
function storeImageString(string hash) public returns (uint index) {
imageHashes[++imageHashCount] = hash;
return imageHashCount;
}
function initializeWeekData(uint _week) public {
if (dataPerWeek[_week].initialized) return;
WeekData storage week = dataPerWeek[_week];
week.initialized = true;
week.totalTokensCompleted = 0;
week.totalPeopleCompleted = 0;
week.totalTokens = 0;
week.totalPeople = 0;
week.totalDaysCommitted = 0;
week.totalDaysCompleted = 0;
}
function currentDay() public view returns (uint day) {
return (block.timestamp - startDate) / secondsPerDay;
}
function currentWeek() public view returns (uint week) {
return currentDay() / daysPerWeek;
}
function currentDayOfWeek() public view returns (uint dayIndex) {
return currentDay() - (currentWeek() * daysPerWeek);
}
} |