ReasonJun

Solidity : Visibility 본문

Blockchain/Solidity

Solidity : Visibility

ReasonJun 2023. 10. 15. 23:07
728x90

Visibility in Solidity is used to control who can access and modify functions and state variables within a contract. There are four visibility levels in Solidity:

  • Public: Public functions and state variables can be accessed by anyone, inside or outside of the contract.
  • Internal: Internal functions and state variables can be accessed by any function within the contract, as well as any function within any contract that inherits from the contract.
  • External: External functions can only be called by other contracts, not by internal functions or state variables.
  • Private: Private functions and state variables can only be accessed by functions within the contract itself.

The visibility of a function or state variable is specified using the public, internal, external, or private keyword. For example:

contract MyContract {
    // Public state variable
    public uint256 publicVariable;

    // Internal function
    internal function internalFunction() {
        // ...
    }

    // External function
    external function externalFunction() {
        // ...
    }

    // Private function
    private function privateFunction() {
        // ...
    }
}

In this example, the publicVariable state variable can be accessed by anyone, inside or outside of the contract. The internalFunction() function can be called by any function within the contract, as well as any function within any contract that inherits from the contract. The externalFunction() function can only be called by other contracts, not by internal functions or state variables. The privateFunction() function can only be called by functions within the contract itself.

 

Visibility is an important concept in Solidity, as it can be used to protect sensitive data and prevent unauthorized access to contract functions. For example, a contract that manages a token supply might have a private function that can only be called by the contract owner to mint new tokens. This would help to prevent unauthorized users from minting new tokens and inflating the token supply.

 

Here are some tips for using visibility effectively in Solidity:

  • Use public visibility for functions and state variables that need to be accessed by other contracts or by users outside of the contract.
  • Use internal visibility for functions and state variables that only need to be accessed by functions within the contract or by contracts that inherit from the contract.
  • Use external visibility for functions that can only be called by other contracts.
  • Use private visibility for functions and state variables that should only be accessed by functions within the contract itself.
  • Be careful about using public visibility for state variables, as this could expose them to unauthorized access.

By following these tips, you can use visibility to improve the security and reliability of your Solidity contracts.

 

 

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract visibility {
    uint a = 5; // default : internal 
    uint public pub = 2; // getter 
    uint public constant c = 5; // getter
    uint private pri = 24;
    uint internal inter = 3;
    // uint external ext = 4;

    function funPub() public view returns(uint, uint, uint) { // getter
        return(pub, pri, inter); // 2 24 3
    }

    function funPriv() private view returns(uint, uint, uint) {
        return(pub, pri, inter);
    }

    function funInter() internal view returns(uint, uint, uint) {
        return(pub, pri, inter);
    }

    function funExt() external  view returns(uint, uint, uint) { // getter
        return(pub, pri, inter); // 2 24 3
    }


}

 

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract visibilityFunction {

    function  funExt() external pure returns(uint) {
        return 2; // 2
    }

    function  funPri() private  pure returns(uint) {
        return 3;
    }

    function  outPutPri() public pure returns(uint) {
        return funPri(); // 3
    }


}

 

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Ex3 {
    // Pure function : ensure that they not read or modify the state. A function can be declared as pure.
    function funExt() external pure returns(uint) {
        return 2; // 2
    }

    function outPutExt() public view returns(uint) {
        return this.funExt(); // 2
    }
}
728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : function / Parameter / wei / ether / gas  (0) 2023.10.16
Solidity : Modifier (pure, view)  (0) 2023.10.16
Solidity : Operator / Shift operator  (1) 2023.10.15
Solidity : constant  (0) 2023.10.15
Solidity : Data Type (1)  (0) 2023.10.15
Comments