Search code examples
shared-librariesethereumsoliditysmartcontractsopenzeppelin

What does a single underscore-command do in a modifier in solidity?


I would like to understand a part of the Ownable() contract of the OpenZeppelin Solidity library:

modifier onlyOwner() {
   require(isOwner());
   _;
}

The last line of this modifier consists only of an underscore. Can anybody please explain to me or refer what the underscore does?

I checked other questions involving modifiers but could only find out that an underscore-command is an existential part of a modifier.


Solution

  • It's used to specify when the instructions in the modifier will be executed

    If the instructions goes before _;, then the code in the modifier will be executed before the function is executed.

    modifier onlyOwner() {
       require(isOwner());
       _;
    }
    

    On the opposite, if the modifier is after _;, then the instructions in the modifier will be executed after the function is executed.

    modifier onlyOwner() {
       _;
       require(isOwner());
    }
    

    Source: https://www.educative.io/answers/what-is-in-solidity