-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVault.sol
More file actions
65 lines (53 loc) · 2.11 KB
/
Copy pathVault.sol
File metadata and controls
65 lines (53 loc) · 2.11 KB
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
pragma solidity ^0.8.7;
contract Vault {
struct Investor {
address addr;
uint amount;
uint index;
}
mapping(address => Investor) public investorsData;
uint number_of_investors;
address[] investors;
uint total_invested;
mapping(address => bool) keepers;
event Deposited(address addr, uint amount);
event Withdrawn(address addr, uint amount);
event RewardsEarned(uint amount);
constructor() payable {
require(msg.value > 0, "Vault must be initialized with nonzero funds");
total_invested = msg.value;
}
function deposit() public payable {
require(msg.value > 0, "Need to deposit a nonzero amount of fund");
Investor storage investor = investorsData[msg.sender];
investor.addr = msg.sender;
investor.amount += msg.value;
investor.index = number_of_investors;
investorsData[msg.sender] = investor;
number_of_investors += 1;
total_invested += msg.value;
emit Deposited(msg.sender, msg.value);
}
function earn() public payable {
require(keepers[msg.sender], "Not a keeper");
uint amount_to_give = msg.value / number_of_investors;
for (uint i=0; i < number_of_investors; i++) {
address investorAddr = investors[i];
Investor storage investor = investorsData[investorAddr];
investor.amount += amount_to_give;
investorAddr.call{value: amount_to_give}("");
}
total_invested += msg.value;
emit RewardsEarned(amount_to_give);
}
function withdraw() public payable {
require(investorsData[msg.sender].amount >= 0, "Not enough funds deposited to withdraw");
Investor storage investor = investorsData[msg.sender];
uint amount_to_withdraw = investor.amount;
(bool success, ) = msg.sender.call{value: amount_to_withdraw}("");
total_invested -= amount_to_withdraw;
investor.amount -= amount_to_withdraw;
emit Withdrawn(msg.sender, msg.value);
}
receive() external payable {}
}