DEV Community

Kristaps Grinbergs
Kristaps Grinbergs

Posted on

Accessibility and visibility in Solidity

Solidity language allows specifying accessibility for contract functions and state variables. That will enable us to set constraints on constructing the smart contract and whether other contracts or addresses can access the data. Contract state variables and functions have a slight difference that we're going to check out. I want to mention that accessibility and visibility are different things in blockchain development context.

Function visibility

Functions in smart contracts can have four accessibility levels.

Public

When a function is defined as public it can be called from any other contract, including a smart contract that contains it or address.

function publicFunction() public {
  publicInt += 1;
}
Enter fullscreen mode Exit fullscreen mode

External

Keyword external means that a function can be called by other contracts or addresses. If we want to call an external function, we can do it using this keyword.

function externalFunction() external {
  externalInt += 1;
}

function callExternalFunction() public {
  this.externalFunction();
}
Enter fullscreen mode Exit fullscreen mode

Internal

Internal functions can be called from the same smart contract or any other inherited from it.

function internalFunction() internal {
  internalInt += 1;
}
Enter fullscreen mode Exit fullscreen mode

Private

Private functions aren't visible and can't be accessed from other smart contracts or addresses. These functions aren't accessible in contracts that inherit a smart contract where this function is defined.

function internalFunction() internal {
  internalInt += 1;
}
Enter fullscreen mode Exit fullscreen mode

Contract state variable visibility

Smart contract state variables can be public, internal and private.

Public

Public state variables can be accessed from any smart contract, including those that hold them or any address. Solidity generates for the public state variable a getter function.

contract Accessibility {
  uint public publicInt;
}

// can get and set from other smart contracts
contract AccessPublic {
  Accessibility accessibility = new Accessibility();

  function accessPublicInt() public view returns(uint) {
    uint publicInt = accessibility.publicInt();
    return publicInt;
  }
}

Enter fullscreen mode Exit fullscreen mode

Internal

Similar to functions, internal state variables can be accessed from a contract that defined them or any other that inherits from a smart contract. Important to note that smart contract state variables are internal by default.

uint internal internalInt;
Enter fullscreen mode Exit fullscreen mode

Private

A private state variable has the same characteristics as functions. It can be accessed only within the same contract, not any other contracts.

uint private privateInt;
Enter fullscreen mode Exit fullscreen mode

Visibility and accessibility

In blockchain development, visibility and accessibility have a different meaning. Everything inside a smart contract can be visible because all transactions are open in a ledger that anyone can see. Private or internal prevents other contracts or addresses from modifying the data. It can be visible from the outside because nothing can be hidden on the blockchain. It means that you can restrict access to a state variable or a function, but it is visible.

TL;DR

Solidity programming language has multiple options to define access levels for smart contract functions and state variables. In blockchain development, visibility and accessibility aren't the same. We can restrict access to the functions or state variables, but it can be viewed from the outside.

Links

Top comments (0)