-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontract2.sol
114 lines (89 loc) · 3.15 KB
/
contract2.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract InvestmentContract {
mapping(address => uint256) private investments;
address payable[] private validUsers;
uint256 public totalInvestmentAmount;
uint256 public totalInvestedUserCount;
uint256 public charges;
uint256 public adminBalance;
address payable public admin;
modifier onlyValidUser() {
require(
investments[msg.sender] == 0,
"User is not allowed to reinvest."
);
_;
}
modifier onlyAdmin() {
require(msg.sender == admin, "Only the admin can call this function.");
_;
}
constructor() {
admin = payable(msg.sender);
charges = 0.1 ether;
}
receive() external payable {}
function invest() external payable onlyValidUser returns (bool) {
require(msg.sender != admin, "Admin not allowed");
require(msg.value > charges, "Amount should be greater than 0.1 ETH");
uint256 investedAmount = msg.value - charges;
investments[msg.sender] = investedAmount;
validUsers.push(payable(msg.sender));
totalInvestmentAmount += investedAmount;
totalInvestedUserCount++;
// Transfer charges to admin's wallet
adminBalance += charges;
admin.transfer(charges);
return true;
}
// function removeUser() external returns (bool) {
// require(investments[msg.sender] > 0, "User has no investment.");
// for (uint256 i = 0; i < validUsers.length; i++) {
// if (validUsers[i] == payable(msg.sender)) {
// delete validUsers[i];
// break;
// }
// }
// return true;
// }
function distributeFunds() external onlyAdmin returns (bool) {
uint256 validUsersLength = validUsers.length;
if (validUsersLength == 0) {
return false;
}
uint256 amountPerUser = totalInvestmentAmount / validUsersLength;
for (uint256 i = 0; i < validUsers.length; i++) {
address payable user = validUsers[i];
user.transfer(amountPerUser);
}
return true;
}
function getTotalInvestment() external view returns (uint256) {
return totalInvestmentAmount;
}
function getMyInvestment() external view returns (uint256) {
return investments[msg.sender];
}
function getTotalUserCount() external view returns (uint256) {
return totalInvestedUserCount;
}
function getValidUsersCount() external view returns (uint256) {
return validUsers.length;
}
function getInvalidUsersCount() external view returns (uint256) {
return totalInvestedUserCount - validUsers.length;
}
function getAdminBalance() external view returns (uint256) {
return adminBalance;
}
function isUserValid() external view returns (bool) {
// Find the user in validUsers array and return true if found
for (uint256 i = 0; i < validUsers.length; i++) {
if (validUsers[i] == payable(msg.sender)) {
return true;
}
}
return false;
}
}