-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ico.sol
89 lines (74 loc) · 2.41 KB
/
Ico.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
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
contract Ico {
struct Payment {
string description;
uint amount;
address receiver;
bool completed;
address[] voters;
}
address public owner;
string public description;
uint public minInvest;
uint public maxInvest;
uint public goal;
address[] public investors;
Payment[] public payments;
constructor(string memory _description, uint _minInvest, uint _maxInvest, uint _goal) {
owner = msg.sender;
description = _description;
minInvest = _minInvest;
maxInvest = _maxInvest;
goal = _goal;
}
function contribute() public payable {
require(msg.value >= minInvest);
require(msg.value <= maxInvest);
require(address(this).balance <= goal);
investors.push(msg.sender);
}
function createPayment(string memory _description, uint _amount, address _receiver) public {
Payment memory newPayment = Payment({
description: _description,
amount: _amount,
receiver: _receiver,
completed: false,
voters: new address[](0)
});
payments.push(newPayment);
}
function approvePayment(uint index) public {
Payment storage payment = payments[index];
// must be investor to vote
bool isInvestor = false;
for (uint i = 0; i < investors.length; i++) {
isInvestor = investors[i] == msg.sender;
if (isInvestor) {
break;
}
}
require(isInvestor);
// can not vote twice
bool hasVoted = false;
for (uint j = 0; j < payment.voters.length; j++) {
hasVoted = payment.voters[j] == msg.sender;
if (hasVoted) {
break;
}
}
require(!hasVoted);
payment.voters.push(msg.sender);
}
function doPayment(uint index) public {
Payment storage payment = payments[index];
require(!payment.completed);
require(payment.voters.length > (investors.length / 2));
payable(payment.receiver).transfer(payment.amount);
payment.completed = true;
}
function getPaymentVoteByIndex(uint paymentIndex) public view returns (address[] memory voters) {
Payment storage payment = payments[paymentIndex];
return payment.voters;
}
}