Skip to content

Instantly share code, notes, and snippets.

@sandoche
Created October 2, 2022 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandoche/1f0c5146a919db67b047ee184c952441 to your computer and use it in GitHub Desktop.
Save sandoche/1f0c5146a919db67b047ee184c952441 to your computer and use it in GitHub Desktop.
Example of a Counter with Owner access control in Solidity (for a blog post)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Counter {
uint public count;
address owner;
// Modifier that makes sure the caller is the owner
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
// Constructor that sets the owner as the initial creator of the smart contract
constructor() {
owner = msg.sender;
}
// Function to get the current count
function get() public view returns (uint) {
return count;
}
// Function to increment count by 1
function inc() public {
count += 1;
}
// Function to decrement count by 1, method only accessible by the owner
function dec() public onlyOwner {
// This function will fail if count = 0
count -= 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment