Created
November 6, 2022 22:54
-
-
Save siraben/d5def211446f9dad2ed5f22ab9e46100 to your computer and use it in GitHub Desktop.
Example Solidity contract with an owner field
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: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
/** | |
* @title Owner | |
* @dev Set & change owner | |
*/ | |
contract Owner { | |
address private owner; // the "owner" of the contract | |
uint256 number; | |
// event for EVM logging | |
event OwnerSet(address indexed oldOwner, address indexed newOwner); | |
/** | |
* @dev Set contract deployer as owner | |
*/ | |
constructor() { | |
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor | |
emit OwnerSet(address(0), owner); | |
} | |
function changeOwner(address newOwner) public { | |
require(msg.sender == owner, "Caller is not owner"); | |
emit OwnerSet(owner, newOwner); // | |
owner = newOwner; // update the owner | |
} | |
function getOwner() external view returns (address) { | |
return owner; | |
} | |
function store(uint256 num) public { | |
require(msg.sender == owner, "Caller is not owner"); | |
number = num; | |
} | |
function retrieve() public view returns (uint256){ | |
return number; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment