Created
August 7, 2024 18:52
-
-
Save amarachiugwu/510f29422534599ff14e204ee357d6d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.0; | |
| contract ComprehensiveContract { | |
| // State variables with different visibility specifiers | |
| uint256 public publicData; // Public visibility: accessible from outside | |
| uint256 internal internalData; // Internal visibility: accessible within this contract and derived contracts | |
| uint256 private privateData; // Private visibility: accessible only within this contract | |
| // Enum definition | |
| enum Status { Pending, Active, Completed, Cancelled } | |
| // Struct definition | |
| struct Task { | |
| string description; | |
| Status status; | |
| uint256 createdAt; | |
| } | |
| // Mapping definition | |
| mapping(address => Task) public tasks; | |
| // Event declaration | |
| event DataUpdated(address indexed updater, uint256 newValue); | |
| event TaskUpdated(address indexed user, string description, Status status); | |
| // State variable to store the owner's address | |
| address public owner; | |
| // Constructor | |
| constructor(uint256 _initialValue) { | |
| publicData = _initialValue; | |
| internalData = _initialValue; | |
| privateData = _initialValue; | |
| owner = msg.sender; // Set the contract deployer as the owner | |
| } | |
| // Function modifier to restrict access to the owner | |
| modifier onlyOwner() { | |
| require(msg.sender == owner, "Only the contract owner can perform this action"); | |
| _; | |
| } | |
| // Function modifier to validate that the input is positive | |
| modifier onlyPositive(uint256 value) { | |
| require(value > 0, "Value must be positive"); | |
| _; | |
| } | |
| // Public function to get the owner's address | |
| function getOwner() public view returns (address) { | |
| return owner; | |
| } | |
| // Public function with view specifier | |
| function getPublicData() public view returns (uint256) { | |
| return publicData; | |
| } | |
| // External function with pure specifier | |
| function add(uint256 a, uint256 b) external pure returns (uint256) { | |
| return a + b; | |
| } | |
| // Internal function with view specifier | |
| function getInternalData() internal view returns (uint256) { | |
| return internalData; | |
| } | |
| // Private function | |
| function _updatePrivateData(uint256 _newValue) private { | |
| privateData = _newValue; | |
| emit DataUpdated(msg.sender, _newValue); // Emit an event on update | |
| } | |
| // Public function to update state variables and use control structures | |
| function updateData(uint256 _newPublicValue, uint256 _newPrivateValue) public onlyPositive(_newPublicValue) { | |
| if (_newPrivateValue % 2 == 0) { // Control structure: if condition | |
| internalData = _newPublicValue; | |
| _updatePrivateData(_newPrivateValue); // Call private function | |
| } else { | |
| revert("Private value must be even"); // Control structure: revert | |
| } | |
| publicData = _newPublicValue; | |
| } | |
| // Function to demonstrate the use of built-in variables | |
| function getTransactionInfo() public payable returns (address sender, uint256 value, uint256 gasRemaining) { | |
| sender = msg.sender; // Address of the entity (person or contract) that called the function | |
| value = msg.value; // Amount of Ether (in wei) sent with the transaction | |
| gasRemaining = gasleft(); // Amount of gas remaining at the point of call | |
| } | |
| // Function to demonstrate various data types | |
| function exampleDataTypes() public pure returns (string memory, bytes32, bool, int256) { | |
| string memory text = "Solidity Example"; // String data type | |
| bytes32 dataHash = keccak256(abi.encodePacked(text)); // Bytes32 data type | |
| bool flag = true; // Boolean data type | |
| int256 signedNumber = -123; // Signed integer data type | |
| return (text, dataHash, flag, signedNumber); | |
| } | |
| // Function to add or update a task using mapping, struct, and enum | |
| function setTask(string memory _description, Status _status) public { | |
| Task memory newTask = Task({ | |
| description: _description, | |
| status: _status, | |
| createdAt: block.timestamp | |
| }); | |
| tasks[msg.sender] = newTask; | |
| emit TaskUpdated(msg.sender, _description, _status); | |
| } | |
| // Function to get a task based on the sender's address | |
| function getTask() public view returns (string memory description, Status status, uint256 createdAt) { | |
| Task storage userTask = tasks[msg.sender]; | |
| return (userTask.description, userTask.status, userTask.createdAt); | |
| } | |
| // Example function using the onlyOwner modifier to get contract balance | |
| function restrictedFunction() public view onlyOwner returns (uint256) { | |
| return address(this).balance; // Get the balance of the contract | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment