#100DaysOfSolidity ๐Ÿ“ Demystifying Visibility in Solidity Contracts: Unlocking the Secrets of Access Control ๐Ÿ”’๐Ÿ”“

#100DaysOfSolidity Series 026 โ€œVisibilityโ€

Solidity Academy
5 min readJul 3, 2023

Welcome to the fascinating world of Solidity contracts, where visibility determines the accessibility of functions and state variables. Solidity, the programming language of choice for Ethereum smart contracts, provides a range of visibility options to ensure controlled access and security.

#100DaysOfSolidity Series 026 โ€œVisibilityโ€

In this article, we will dive deep into the concept of visibility, unravel its intricacies, and learn how to wield its power effectively. So, grab your virtual hard hats and letโ€™s embark on this visibility adventure! ๐Ÿš€

Understanding Visibility: Unlocking the Different Levels ๐Ÿ”

In Solidity, visibility acts as a gateway, governing who can interact with specific functions and state variables within a contract. There are four primary visibility levels to explore: public, private, internal, and external. Each level serves a unique purpose and offers distinct access permissions. Letโ€™s unlock the secrets of these visibility levels one by one! ๐Ÿ”

1๏ธโƒฃ Private Visibility: Securing the Inner Sanctum ๐Ÿคซ

Private functions and state variables are like hidden treasures, accessible only within the contract that defines them. Their restricted access ensures that they remain confined within the boundaries of the contract, shielded from external interference. Contracts that inherit from the base contract containing private members cannot directly access them. Privacy is the name of the game when it comes to private visibility. ๐Ÿ™ˆ

2๏ธโƒฃ Internal Visibility: Sharing Secrets with the Inheritors ๐Ÿ‘ฅ

The internal visibility level allows functions and state variables to be shared with the contract that defines them and any derived contracts. It forms a bridge of knowledge, granting access to the inner workings of a contractโ€™s logic to its inheritors. This visibility level facilitates collaboration and seamless integration between contracts, enabling derived contracts to leverage and build upon the internal functionality. ๐Ÿค

3๏ธโƒฃ Public Visibility: Opening the Gates to the World ๐ŸŒ

Public functions and state variables are the global citizens of the Solidity world. They are accessible not only within the defining contract and derived contracts but also by external contracts and accounts. Public visibility breaks down barriers, promoting interoperability and enabling seamless interaction between contracts and external entities. Itโ€™s like throwing open the gates and inviting the world to engage with your contractโ€™s functionalities. ๐ŸŒ

4๏ธโƒฃ External Visibility: Engaging in Secure Handshakes ๐Ÿค

The external visibility level is similar to public visibility in terms of accessibility to external contracts and accounts. However, it comes with an intriguing twist โ€” external functions can only be called by external contracts and accounts. This level of visibility restricts internal access, ensuring a controlled and secure environment for contract interactions. External visibility is often used when contracts need to expose specific functionalities to other contracts or external accounts securely. ๐Ÿ”’

Putting Visibility into Practice: Sample Contracts and Use Cases ๐Ÿ’ก

Now that we have unlocked the secrets of visibility, letโ€™s dive into some sample contracts to see how visibility levels are implemented and explore their practical use cases.

๐Ÿ” Code Snippet 1: The Base Contract ๐Ÿ›๏ธ

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Base {
// Private function can only be called
// - inside this contract
// Contracts that inherit this contract cannot call this function.
function privateFunc() private pure returns (string memory) {
return "private function called";
}
function testPrivateFunc() public pure returns (string memory) {
return privateFunc();
}
// Internal function can be called
// - inside this contract
// - inside contracts that inherit this contract
function internalFunc
() internal pure returns (string memory) {
return "internal function called";
}
function testInternalFunc() public pure virtual returns (string memory) {
return internalFunc();
}
// Public functions can be called
// - inside this contract
// - inside contracts that inherit this contract
// - by other contracts and accounts
function publicFunc() public pure returns (string memory) {
return "public function called";
}
// External functions can only be called
// - by other contracts and accounts
function externalFunc() external pure returns (string memory) {
return "external function called";
}
// This function will not compile since we're trying to call
// an external function here.
// function testExternalFunc() public pure returns (string memory) {
// return externalFunc();
// }
// State variables
string private privateVar = "my private variable";
string internal internalVar = "my internal variable";
string public publicVar = "my public variable";
// State variables cannot be external so this code won't compile.
// string external externalVar = "my external variable";
}

This code snippet showcases the visibility levels in action. The `Base` contract demonstrates private, internal, public, and external functions and state variables. It also highlights the compilation errors that arise when trying to access external functions internally or declaring state variables as external.

๐Ÿ”„ Code Snippet 2: The Derived Contract ๐Ÿ—๏ธ

contract Child is Base {
// Inherited contracts do not have access to private functions
// and state variables.
// function testPrivateFunc() public pure returns (string memory) {
// return privateFunc();
// }
// Internal function call be called inside child contracts.
function testInternalFunc() public pure override returns (string memory) {
return internalFunc();
}
}

The `Child` contract extends the `Base` contract, inheriting its visibility-restricted functions. It showcases how internal functions can be accessed and overridden in derived contracts, while private functions and state variables remain inaccessible.

๐Ÿ”— Conclusion: Unlock the Power of Visibility in Your Contracts ๐Ÿ—๏ธ

Visibility is a powerful tool in Solidity that allows you to control the accessibility and security of your smart contracts. By strategically choosing the appropriate visibility level for your functions and state variables, you can ensure proper access control and enhance the interoperability of your contracts.

In this article, we explored the four visibility levels โ€” private, internal, public, and external โ€” and examined their use cases and limitations. We also delved into sample contracts that brought these visibility levels to life, helping you understand their practical implementation.

Now that you possess the key to visibility, go forth and unlock the full potential of Solidity contracts. Remember to carefully consider the access requirements and security implications of your functions and state variables. With the proper use of visibility, you can build robust and interoperable smart contracts that stand the test of time.

๐Ÿ”— Additional Resources:

So, letโ€™s venture into the world of Solidity with confidence, armed with the knowledge of visibility! ๐Ÿ”“๐Ÿ’ช

Happy coding, and may your smart contracts shine brightly in the decentralized universe! โœจ๐ŸŒŒ

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/