Last active
October 21, 2021 11:26
-
-
Save connorphill/a95ed6ff8684f0f8ece4a4093c1ac4c6 to your computer and use it in GitHub Desktop.
Example of using visibility in a solidity smart contract
This file contains 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 VisibiltyExample | |
/// @author Connor Phillips (blockchain@connorphillips.com) | |
/// @notice Purpose of the contract is to show how visibility works | |
/// @dev All function calls are currently implemented without side effects | |
contract VisibiltyExample { | |
string greeting = "Hello World!"; // The default visibility for state variables are internal | |
uint8 public randomNum = 123; | |
/// @notice Function will be publicly accessable on the blockchain | |
function publicFunc() public view returns(string memory, uint){ | |
return ( | |
greeting, | |
randomNum | |
); | |
} | |
/// @notice Function is not accessable on the blockchain, but accessable within the smart contract | |
function privateFunc() private view returns(string memory, uint){ | |
return ( | |
greeting, | |
randomNum | |
); | |
} | |
/// @notice Due to use of public and accessability of private within other functions in the smart contract, privateFunc will be publicly accessable on the blockchain | |
function exposedPrivateFunc() public view returns(string memory, uint){ | |
return privateFunc(); | |
} | |
/// @notice Function is not accessable on the blockchain, but accessable within the smart contract | |
function internalFunc() internal view returns(string memory, uint){ | |
return ( | |
greeting, | |
randomNum | |
); | |
} | |
/// @notice Due to use of public and accessability of internal within other functions in the smart contract, internalFunc will be publicly accessable on the blockchain | |
function exposedInternalFunc() public view returns(string memory, uint){ | |
return internalFunc(); | |
} | |
/// @notice Function will be publicly accessable on the blockchain | |
function externalFunc() external view returns(string memory, uint){ | |
return ( | |
greeting, | |
randomNum | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment