-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path029-restricted-access.sol
63 lines (53 loc) · 1.6 KB
/
029-restricted-access.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Restricted Access
*
* By Default a contract is made readonly unless it is made public
* We can restrict who can modify a contract's state or call a
* function using modifiers.
*/
contract RestrictedAccess {
address public owner = msg.sender;
uint256 public creationTime = block.timestamp;
modifier onlyBy(address _account) {
// check if the current sender is the owner
// require(msg.sender == owner);
require(msg.sender == _account, "Access Restricted to Owners Only!");
_;
}
modifier onlyAfter(uint256 _time) {
require(block.timestamp >= _time, "Function called to early!");
_;
}
// function that changes the owner address
// restrict this access only to the owner of the contract
function changeOwnerAddress(address newAddres) public onlyBy(owner) {
owner = newAddres;
}
// function to disown the current owner
// only run the contract after three weeks of creation
function disownOwner()
public
onlyBy(owner)
onlyAfter(creationTime + 3 weeks)
{
delete owner;
}
// add more ethers to the owner
// not the best way but this will do
// for this case.
uint256 balance = msg.value + 200 ether;
// cost restriction
modifier costs(uint256 _amount) {
require(balance > _amount, "Not enough Ether provided!");
_;
}
function forceOwnerChange(address _newOwner)
public
payable
costs(200 ether)
{
owner = _newOwner;
}
}