-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlarity.sol
574 lines (525 loc) · 20.1 KB
/
Blarity.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
pragma solidity ^0.4.24;
interface ERC20 {
function totalSupply() external view returns (uint supply);
function balanceOf(address _owner) external view returns (uint balance);
function transfer(address _to, uint _value) external returns (bool success);
function transferFrom(address _from, address _to, uint _value) external returns (bool success);
function approve(address _spender, uint _value) external returns (bool success);
function allowance(address _owner, address _spender) external view returns (uint remaining);
function decimals() external view returns(uint digits);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
contract VotingModel {
function weightOf(uint x) public pure returns (uint y) {
// Currently using Quadratic voting (Square Root)
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
uint z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}
}
// File: contracts/ReentrancyGuard.sol
/**
* @title Helps contracts guard against reentrancy attacks.
*/
contract ReentrancyGuard {
/// @dev lengther to allow mutex lock with only one SSTORE operation
uint256 private guardCounter = 1;
/**
* @dev Prevents a function from calling itself, directly or indirectly.
* Calling one `nonReentrant` function from
* another is not supported. Instead, you can implement a
* `private` function doing the actual work, and an `external`
* wrapper marked as `nonReentrant`.
*/
modifier nonReentrant() {
guardCounter += 1;
uint256 localCounter = guardCounter;
_;
require(localCounter == guardCounter);
}
}
// File: contracts/KyberNetworkProxyInterface.sol
/// @title Kyber Network interface
interface KyberNetworkProxyInterface {
function maxGasPrice() external view returns(uint);
function getUserCapInWei(address user) external view returns(uint);
function getUserCapInTokenWei(address user, ERC20 token) external view returns(uint);
function enabled() external view returns(bool);
function info(bytes32 id) external view returns(uint);
function getExpectedRate(ERC20 src, ERC20 dest, uint srcQty) external view
returns (uint expectedRate, uint slippageRate);
function tradeWithHint(ERC20 src, uint srcAmount, ERC20 dest, address destAddress, uint maxDestAmount,
uint minConversionRate, address walletId, bytes hint) external payable returns(uint);
}
contract Blarity is ReentrancyGuard, VotingModel {
ERC20 constant internal ETH_TOKEN_ADDRESS = ERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee);
// Withdraw funds
event EtherWithdraw(uint amount, address sendTo);
/**
* @dev Withdraw Ethers
*/
function withdrawEther(uint amount, address sendTo) public onlyOwner {
require(amount <= address(this).balance);
sendTo.transfer(amount);
emit EtherWithdraw(amount, sendTo);
}
event TokenWithdraw(ERC20 token, uint amount, address sendTo);
/**
* @dev Withdraw all ERC20 compatible tokens
* @param token ERC20 The address of the token contract
*/
function withdrawToken(ERC20 token, uint amount, address sendTo) public onlyOwner {
if (token != acceptedToken) {
require(token.transfer(sendTo, amount));
emit TokenWithdraw(token, amount, sendTo);
} else {
// withdraw accepted token, only can withdraw amount does not belong to campaign
// contract could get some airdrop
require(acceptedToken.balanceOf(address(this)) >= amount + currentBalance);
require(acceptedToken.transfer(sendTo, amount));
emit TokenWithdraw(token, amount, sendTo);
}
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
// Transfer ownership
event TransferOwner(address newOwner);
function transferOwner(address newOwner) public onlyOwner {
require(newOwner != address(0));
owner = newOwner;
emit TransferOwner(newOwner);
}
// owner/creator address
address public owner;
// start and end time
uint public endTime;
// accepted token, e.g: DAI
ERC20 public acceptedToken;
// target money of this campaign, if target is not reached, money could be reverted back to contributors
uint public targetedMoney;
// current balance of this campaign
uint public currentBalance;
// if campaign is timed out and not got enough money,
// contributors can submit a request to claim funds back (no need approve of Delegator)
bool public isReverted = false;
// a contributor will have:
// - amount contributed
// - delegator address, who will decide the vote
struct Contributor {
// address of the contributor
address ownerAddress;
// contributed amount
uint amount;
// delegator address
address delegator;
}
// list of contributors
Contributor[] contributors;
// who will decide the vote based on their weight
struct Delegator {
// address of Delegator
address ownerAddress;
// weight for the vote from this Delegator
uint weight;
}
Delegator[] delegators;
struct RequestFund {
// amount to request
uint amount;
// address to transfer funds to
address toAddress;
// is ended the request
uint endTime;
bool isEnded;
// status of the request: 0: reject, 1: unknown, 2: approved
uint status;
}
RequestFund[] requestFunds;
struct Vote {
address ownerVote;
uint weight;
bool isApproved;
}
mapping(uint => Vote[]) votings;
constructor(
uint _endTime,
ERC20 _acceptedToken,
uint _targetedMoney
) public {
require(_targetedMoney > 0);
require(_endTime > now);
owner = msg.sender;
endTime = _endTime;
acceptedToken = _acceptedToken;
targetedMoney = _targetedMoney;
currentBalance = 0;
}
// Do we allow owner to change targeted money?
function updateTargetedMoney(uint _money) public onlyOwner {
require(now < endTime); // must not be ended
targetedMoney = _money;
}
// Should be able to update end time
function updateEndTime(uint _endTime) public onlyOwner {
endTime = _endTime;
}
event UpdateIsReverted(bool isReverted);
function updateIsRevertedEndTimeReached() public onlyOwner {
require(now >= endTime);
require(isReverted == false);
if (acceptedToken.balanceOf(address(this)) < targetedMoney) {
isReverted = true;
emit UpdateIsReverted(true);
}
}
event TargetedMoneyReached();
function updateTargetedMoneyReached() public onlyOwner {
require(currentBalance >= targetedMoney);
endTime = now;
emit TargetedMoneyReached();
}
// Creator requests a fund transfer with amount and to address
event CreatorRequestFundTransfer(uint _amount, address _address, uint _endTime);
function creatorRequestFundTransfer(uint _amount, address _to, uint _endTime) public onlyOwner {
require(now >= endTime); // must be ended to start requesting funds
require(_endTime > now); // end time must be greater than current time
if (acceptedToken == ETH_TOKEN_ADDRESS) {
require(address(this).balance >= _amount);
} else {
require(acceptedToken.balanceOf(address(this)) >= _amount);
}
// require all previous request funds must be Refunded
for(uint i = 0; i < requestFunds.length; i++) {
require(requestFunds[i].isEnded); // only 1 request at any time
}
// push new request fund with amount, to _to address, not ended and status is unknown (1)
requestFunds.push(RequestFund({amount: _amount, toAddress: _to, isEnded: false, status: 1, endTime: endTime}));
emit CreatorRequestFundTransfer(_amount, _to, _endTime);
}
// Creator Reject a request
event CreatorRejectRequestFundTransfer(uint _amount, address _address);
function creatorRejectRequestFundTransfer(uint id) public onlyOwner {
require(now >= endTime); // must be ended
require(requestFunds.length > id); // id must be in range of number request funds
require(!requestFunds[id].isEnded); // must of be ended
requestFunds[id].status = 0; // reject the request
emit CreatorRejectRequestFundTransfer(requestFunds[id].amount, requestFunds[id].toAddress);
}
// Creator claim fund transfer
event CreatorClaimedFundTransfer(uint _amount, address _address);
function creatorClaimFundTransfer(uint id) public onlyOwner {
require(now >= endTime); // must be ended
require(requestFunds.length > id); // id must be in range of number request funds
require(requestFunds[id].status == 1); // status of the request must be unknown
require(currentBalance >= requestFunds[id].amount);
// check if it is success or failed
uint totalWeight = 0;
uint i;
for(i = 0; i < delegators.length; i++) {
totalWeight += weightOf(delegators[i].weight);
}
Vote[] storage votes = votings[id];
uint positive = 0;
uint negavtive = 0;
for(i = 0; i < votes.length; i++) {
if (votes[i].isApproved) { positive += weightOf(votes[i].weight); }
else { negavtive += weightOf(votes[i].weight); }
}
// positive or negative is more than 50%, immediate update the request
if (positive > totalWeight / 2) {
// more than 50% approved
if (acceptedToken == ETH_TOKEN_ADDRESS) {
requestFunds[id].toAddress.transfer(requestFunds[id].amount);
} else {
require(acceptedToken.transfer(requestFunds[id].toAddress, requestFunds[id].amount));
}
currentBalance -= requestFunds[id].amount;
requestFunds[id].isEnded = true;
requestFunds[id].status = 2; // approved
emit CreatorClaimedFundTransfer(requestFunds[id].amount, requestFunds[id].toAddress);
return;
} else if (negavtive > totalWeight / 2) {
requestFunds[id].isEnded = true;
requestFunds[id].status = 1; // rejected
return;
}
// End time reached
if (now >= requestFunds[id].endTime) {
// more approves and rejects
if (positive > negavtive) {
if (acceptedToken == ETH_TOKEN_ADDRESS) {
requestFunds[id].toAddress.transfer(requestFunds[id].amount);
} else {
require(acceptedToken.transfer(requestFunds[id].toAddress, requestFunds[id].amount));
}
currentBalance -= requestFunds[id].amount;
requestFunds[id].isEnded = true;
requestFunds[id].status = 2; // approved
emit CreatorClaimedFundTransfer(requestFunds[id].amount, requestFunds[id].toAddress);
} else {
requestFunds[id].isEnded = true;
requestFunds[id].status = 1; // rejected
}
}
}
event Voted(uint _requestID, address _delegator, uint _weight, bool _isApproved);
function vote(uint _requestID, bool isApproved) public {
require(now >= endTime); // must be ended
require(requestFunds.length > _requestID); // id must be in range of number request funds
require(!requestFunds[_requestID].isEnded && requestFunds[_requestID].status == 1); // status of the request must be unknown, request must not be ended
uint weight;
for(uint i0 = 0; i0 < delegators.length; i0++) {
if (delegators[i0].ownerAddress == msg.sender) {
weight = delegators[i0].weight;
}
}
require(weight > 0);
Vote[] storage voters = votings[_requestID];
for(uint i = 0; i < voters.length; i++) {
if (voters[i].ownerVote == msg.sender) {
// already vote
return;
}
}
voters.push(Vote({ownerVote: msg.sender, weight: weight, isApproved: isApproved}));
emit Voted(_requestID, msg.sender, weight, isApproved);
}
event Contributed(address _address, uint _amount, address _delegator);
function contribute(KyberNetworkProxyInterface network, ERC20 src, uint srcAmount, address _delegator) public payable {
require(currentBalance < targetedMoney); // target money is not reached
require(srcAmount > 0);
require(now < endTime); // not ended yet
if (src == ETH_TOKEN_ADDRESS) require(srcAmount == msg.value);
uint paidAmount;
bytes memory hint;
if (src == acceptedToken) {
// normal transfer, no need swap
paidAmount = doContributeWithoutKyber(src, srcAmount);
} else {
// do swap with KyberNetwork protocol
PayData memory payData = PayData({
src: src,
network: network,
srcAmount: srcAmount,
dest: acceptedToken,
destAddress: address(this),
minConversionRate: 0,
walletId: address(0),
maxDestAmount: targetedMoney - currentBalance,
hint: hint});
paidAmount = doContributeWithKyber(payData);
}
currentBalance += paidAmount;
// add paid amount to Contributor
bool contributorExist = false;
uint i;
for(i = 0; i < contributors.length; i++) {
if (contributors[i].ownerAddress == msg.sender) {
require(contributors[i].delegator == _delegator);
contributors[i].amount += paidAmount;
contributorExist = true;
}
}
if (!contributorExist) {
contributors.push(Contributor({ownerAddress: msg.sender, amount: paidAmount, delegator: _delegator}));
}
// add weight to delegator
for(i = 0; i < delegators.length; i++) {
if (delegators[i].ownerAddress == _delegator) {
delegators[i].weight += paidAmount;
emit Contributed(msg.sender, paidAmount, _delegator);
return;
}
}
delegators.push(Delegator({ownerAddress: _delegator, weight: paidAmount}));
if (currentBalance >= targetedMoney) { endTime = now; } // stop campaign targeted money is reached
emit Contributed(msg.sender, paidAmount, _delegator);
}
// normal transfer token without Kyber Network
function doContributeWithoutKyber(ERC20 src, uint srcAmount) internal returns (uint paidAmount) {
uint returnAmount;
if (srcAmount + currentBalance > targetedMoney) {
returnAmount = srcAmount + currentBalance - targetedMoney;
paidAmount = srcAmount - returnAmount;
} else {
returnAmount = 0;
paidAmount = srcAmount;
}
if (src == ETH_TOKEN_ADDRESS) {
address(this).transfer(paidAmount);
if (returnAmount > 0) {
msg.sender.transfer(returnAmount);
}
} else {
require(src.transferFrom(msg.sender, address(this), paidAmount));
}
}
struct PayData {
KyberNetworkProxyInterface network;
ERC20 src;
uint srcAmount;
ERC20 dest;
address destAddress;
uint maxDestAmount;
uint minConversionRate;
address walletId;
bytes hint;
}
// Swap token using KyberNetwork protocol
function doContributeWithKyber(PayData payData) internal returns (uint paidAmount) {
if (payData.src != ETH_TOKEN_ADDRESS) {
require(payData.src.transferFrom(msg.sender, address(this), payData.srcAmount));
require(payData.src.approve(payData.network, 0));
require(payData.src.approve(payData.network, payData.srcAmount));
}
uint wrapperSrcBalanceBefore;
uint destAddressBalanceBefore;
uint wrapperSrcBalanceAfter;
uint destAddressBalanceAfter;
uint srcAmountUsed;
(wrapperSrcBalanceBefore, destAddressBalanceBefore) = getBalances(payData.src, payData.dest, payData.destAddress);
paidAmount = doTradeWithHint(payData);
if (payData.src != ETH_TOKEN_ADDRESS) {
require(payData.src.approve(payData.network, 0));
}
(wrapperSrcBalanceAfter, destAddressBalanceAfter) = getBalances(payData.src, payData.dest, payData.destAddress);
// verify the amount the user got is same as returned from Kyber Network
require(destAddressBalanceAfter > destAddressBalanceBefore);
require(paidAmount == (destAddressBalanceAfter - destAddressBalanceBefore));
// calculate the returned change amount
require(wrapperSrcBalanceBefore >= wrapperSrcBalanceAfter);
srcAmountUsed = wrapperSrcBalanceBefore - wrapperSrcBalanceAfter;
require(payData.srcAmount >= srcAmountUsed);
if (payData.srcAmount - srcAmountUsed > 0) {
if (payData.src == ETH_TOKEN_ADDRESS) {
msg.sender.transfer(payData.srcAmount - srcAmountUsed);
} else {
require(payData.src.transfer(msg.sender, payData.srcAmount - srcAmountUsed));
}
}
return paidAmount;
}
function doTradeWithHint(PayData memory payData) internal returns (uint paidAmount) {
paidAmount = payData.network.tradeWithHint.value(msg.value)({
src: payData.src,
srcAmount: payData.srcAmount,
dest: payData.dest,
destAddress: payData.destAddress,
maxDestAmount: payData.maxDestAmount,
minConversionRate: payData.minConversionRate,
walletId: payData.walletId,
hint: payData.hint
});
}
function getBalances(ERC20 src, ERC20 dest, address destAddress) internal view returns (uint wrapperSrcBalance, uint destAddressBalance) {
if (src == ETH_TOKEN_ADDRESS) {
wrapperSrcBalance = address(this).balance;
} else {
wrapperSrcBalance = src.balanceOf(address(this));
}
if (dest == ETH_TOKEN_ADDRESS) {
destAddressBalance = destAddress.balance;
} else {
destAddressBalance = dest.balanceOf(destAddress);
}
}
// transfer delegator
event DelegatorTransferFrom(address _from, address _to, uint _weight);
function transferDelegator(address _newDelegator) public {
address oldDelegator;
uint amount = 0;
uint i;
for(i = 0; i < contributors.length; i++) {
if (contributors[i].ownerAddress == msg.sender) {
oldDelegator = contributors[i].delegator;
amount = contributors[i].amount;
require(oldDelegator != _newDelegator);
contributors[i].delegator = _newDelegator;
}
}
require(amount > 0);
for(i = 0; i < delegators.length; i++) {
if (delegators[i].ownerAddress == oldDelegator) {
require(delegators[i].weight >= amount);
delegators[i].weight -= amount;
}
}
for(i = 0; i < delegators.length; i++) {
if (delegators[i].ownerAddress == _newDelegator) {
delegators[i].weight += amount;
}
}
emit DelegatorTransferFrom(oldDelegator, _newDelegator, amount);
}
event RefundedContributor(address _address, uint _amount);
function requestRefundContributor() public {
require(isReverted == true); // only refund if it is reverted
uint refundAmount;
address delegator;
uint i;
for(i = 0; i < contributors.length; i++) {
if (contributors[i].ownerAddress == msg.sender) {
refundAmount = contributors[i].amount;
delegator = contributors[i].delegator;
require(refundAmount > 0);
}
}
if (acceptedToken == ETH_TOKEN_ADDRESS) {
msg.sender.transfer(refundAmount);
} else {
require(acceptedToken.transferFrom(address(this), msg.sender, refundAmount));
}
for(i = 0; i < delegators.length; i++) {
if (delegators[i].ownerAddress == delegator) {
require(delegators[i].weight >= refundAmount);
delegators[i].weight -= refundAmount;
}
}
emit RefundedContributor(msg.sender, refundAmount);
}
function getVoteStatus(uint _requestID) public view returns (uint negative, uint unknown, uint positive) {
require(now > endTime); // ended
require(requestFunds.length > _requestID);
negative = 0;
unknown = 0;
positive = 0;
uint i;
for(i = 0; i < delegators.length; i++) {
unknown += delegators[i].weight;
}
Vote[] storage votes = votings[_requestID];
for(i = 0; i < votes.length; i++) {
require(unknown >= votes[i].weight);
if (votes[i].isApproved) {
positive += votes[i].weight;
unknown -= votes[i].weight;
} else {
negative += votes[i].weight;
unknown -= votes[i].weight;
}
}
return (negative, unknown, positive);
}
function getRequestFundInfo(uint _requestID) public view returns (uint _amount, address _toAddress, uint _endTime, bool _isEnded, uint _status) {
require(requestFunds.length > _requestID);
_amount = requestFunds[_requestID].amount;
_toAddress = requestFunds[_requestID].toAddress;
_endTime = requestFunds[_requestID].endTime;
_isEnded = requestFunds[_requestID].isEnded;
_status = requestFunds[_requestID].status;
return (_amount, _toAddress, _endTime, _isEnded, _status);
}
function getLastestRequestFundID() public view returns (uint requestID) {
return requestFunds.length;
}
}
// TEST:
// Big End time: 999999999999
// Ropsten DAI Address: 0xad6d458402f60fd3bd25163575031acdce07538d
// Amount 10: 0x8AC7230489E80000