You are currently viewing Solidity Modifier Explained: How to Use Solidity Modifier

Solidity Modifier Explained: How to Use Solidity Modifier


Back to New at Nirolution Solidity Tutorial Blog

Solidity Modifier Simply Explained

Our Smart Contract is now almost finished. The only problem we still have is that everyone can access our Smart Contract. In this case, we don’t necessarily want that. So to change that we need Solidity Modifier. This way we can ensure that only the person who created the Smart Contract can change something.

“Solidity Modifier Simply Explained” – Content:

  1. Solidity Modifier Simply Explained
  2. Smart Contract Constructor
  3. Create a Solidity Modifier

Solidity Modifier Simply Explained

When you create a Smart Contract, some kind of message is sent to the Blockchain. This message allows you to access your contract. So that no one else but the creator of the Smart Contract can change anything, we create a new variable of type address called “owner”.

pragma solidity ^0.4.0;

contract Bank {

uint private _value;
adresse private _owner;

}

With “address” we can store a 20-byte value and perform operations such as “==” (equality) or “>” (greater than). This data type also has a so-called “members”. These are additional prescribed methods like “send” or “balance”, which can be called via a modifier. But we’ll get to that later.

Smart Contract Constructor

Now we have to assign the message to the variable “_owner” in the constructor of Bank when creating the Smart Contract. Constructors basically work like normal methods or functions, but they have a few significant differences. First we need to explain the construction of a constructor. They only have the keyword “function” in front of the actual name. Also, the name is not randomly selectable, because this always depends on the name of the class or in our case on the “contract” (in our example: “bank”). Unlike methods, the constructor has no return values. A class or contract can have as many constructors as you want, but they must differ in at least one parameter (the variables in the round brackets). These values have to be specified in the round brackets after “new Contract ()” when creating a new object or Smart Contracts. (Contract c = new Contract(- here -) )

The last and perhaps most important feature of a constructor is that you can call it only once when creating an object or Smart Contracts after compiling. Therefore, the constructor is usually used to set start values (default values) and to prevent unwanted entries with plausibility checks.

pragma solidity ^0.4.0;

contract Bank {
uint private _value;
adresse private _owner;

  function Bank(uint amount){

  value = amount;
  owner = msg.sender;
  }

  modifier _ownerFunc {
 
  require(owner == msg.sender);
  _;
  }
}

Create a Solidity Modifier

So to be able to use the variable “owner” now, we have to create a modifier.

pragma solidity ^0.4.0;

contract Bank {
uint private _value;
adresse private _owner;

  function Bank(uint amount){

  value = amount;
  owner = msg.sender;
  }

  modifier _ownerFunc {
  
  require(owner == msg.sender);
  _;
  }
}

For this, we use the keyword modifier and call our construct “ownerFunc”. Now we specify the requirement with “require”. The “owner” and the message of the creator must be the same. Only the address of the creator is compared with that of the sender. If this statement is “true”, the code is processed. If this statement is “false”, nothing happens. Then we set “_;” so that the function is always executed first. This will display an error if someone who is not the creator of the smart contract wants to change something. Now we just need to add “ownerFunc” to each function where we don’t want anyone other than the creator to be able to change anything.

At the end of each affected method we add our modifier “ownerFunc” after the parameter values.

pragma solidity ^0.4.0;

contract Bank {

uint private _value;
adresse private _owner;

  modifier _ownerFunc {
  
  require(owner == msg.sender);
  _;
  }

  function deposit(unit amount) _ownerFunc {
  
  value += amount;
  }

  function withdraw(unit amount) _ownerFunc {

    if(checkValue(amount)) {
  
    value -= amount;
    }
  }
}

Now only the creators of the Smart Contract can deposit and withdraw money.

With this, you have created a simple and working Smart Contract.

If you want to be informed about the latest updates, follow us on Facebook, Pinterest and Steemit.

Stay current on your favorite topics

Leave a Reply