-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInheritance.sol
71 lines (57 loc) · 2.35 KB
/
Inheritance.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Inheritance {
address public owner;
address public recipient;
bool public isActive;
uint256 public startDate;
uint256 public endDate;
event RecipientUpdated(address indexed oldRecipient, address indexed newRecipient);
event FundsReleased(address indexed to, uint256 amount);
event ContractActivated(uint256 startDate, uint256 endDate);
event ContractDeactivated();
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
modifier isContractActive() {
require(isActive, "Contract is not active");
_;
}
constructor(uint256 _startDate, uint256 _endDate) payable {
require(_startDate < _endDate, "Start date must be earlier than end date");
require(msg.value > 0, "Initial funding is required");
owner = msg.sender;
startDate = _startDate;
endDate = _endDate;
isActive = true;
emit ContractActivated(_startDate, _endDate);
}
function addOrUpdateRecipient(address _recipient) external onlyOwner isContractActive {
require(_recipient != address(0), "Invalid recipient address");
emit RecipientUpdated(recipient, _recipient);
recipient = _recipient;
}
function deactivateContract() external onlyOwner isContractActive {
isActive = false;
emit ContractDeactivated();
}
function activateContract() external onlyOwner {
require(address(this).balance > 0, "No funds available to reactivate");
require(block.timestamp < endDate, "Cannot reactivate after endDate");
isActive = true;
emit ContractActivated(startDate, endDate);
}
function releaseFunds() external isContractActive {
require(block.timestamp >= endDate, "End date has not passed yet");
uint256 balance = address(this).balance;
require(balance > 0, "No funds available to release");
address to = recipient != address(0) ? recipient : owner;
isActive = false; // Automatically deactivate the contract after releasing funds.
(bool success, ) = to.call{value: balance}("");
require(success, "Funds transfer failed");
emit FundsReleased(to, balance);
}
// Fallback function to accept Ether
receive() external payable {}
}