-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathv2.sol
57 lines (45 loc) · 1.51 KB
/
v2.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
// State variables
uint256 private myVariable;
address private owner;
// Events
event ValueChanged(address indexed owner, uint256 indexed newValue);
// Constructor
constructor() {
owner = msg.sender;
}
// Modifiers
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
// Functions
function setValue(uint256 newValue) external onlyOwner {
require(newValue != myVariable, "New value must be different");
myVariable = newValue;
emit ValueChanged(msg.sender, newValue);
}
function getValue() external view returns (uint256) {
return myVariable;
}
// Function to get the contract owner
function getOwner() external view returns (address) {
return owner;
}
// Function to transfer ownership to a new address
function transferOwnership(address newOwner) external onlyOwner {
require(newOwner != address(0), "Invalid new owner address");
require(newOwner != owner, "New owner is the same as the current owner");
owner = newOwner;
}
// Function to withdraw accidentally sent Ether
function withdrawEther() external onlyOwner {
payable(owner).transfer(address(this).balance);
}
// Fallback function to reject any Ether sent to the contract
receive() external payable {
revert("Contract does not accept Ether");
}
}