All Products
Search
Document Center

Blockchain as a Service:Solidity Keywords

Last Updated:Sep 05, 2019

The Solidity syntax supported by Ant Blockchain is basically consistent with the Solidity official documentation. For more information, see Solidity documentation (English).

This topic describes the permission keyword and the modifier keyword that are widely used in Solidity smart contracts.

The construction method of the function type

  1. function (<parameter types>) {public | private | internal | external} [modifier] [pure|constant|view|payable] [returns (<return types>)]

Permission keywords of the function

  • public: Only functions of type public can be accessed externally. When the permission of a state variable is public, this variable automatically generates a get function that can be called externally. When the function is declared, it defaults to the public type. When the state variable is declared, it defaults to the internal type.
  • private: Access can only be made in the current class. The subclasses cannot be inherited, called, or accessed.
  • internal: The subclass inherits the parent class, and the subclass can access the internal function of the parent class. At the same time, after the using for keyword is used, the class can use the internal function of the called class.
  • external: A declared function can only be called outside the contract.

Modifier keywords of the function

  • modifier: A function modified by a keyword declared by the modifier keyword can only be executed when the requirements of the keyword declared by the modifier keyword are satisfied. For example, a function that can only be executed by the administrator:

    1. modifier onlyAdmin() {
    2. require(msg.sender == admin, "Permission denied");
    3. _;
    4. }
    5. function set(uint a) public onlyAdmin returns(uint) {
    6. .....
    7. }
  • constant: State variables declared as constant can only be assigned values ​​using expressions that have certain values ​​during the compiling process. Any assignment using the memory, blockchain data (such as now, this.balance or block.number), execution data (msg.gas), or calls to external contracts is not allowed. Only some of state variables can be modified using constant. Currently, only value types and strings are supported.

  • pure: The value of the state variable modified by this keyword can only be read and cannot be modified.

  • view: The value of the state variable modified by this keyword cannot be read or modified.

  • Storage variables are variables that are permanently stored in the blockchain.

  • The Memory variable is temporary. It will be removed when the calling of a contract by an external function is completed.