whenNotPaused modifier throws a compilation error when applying to pure functions

The whenNotPaused modifier throws a compilation error when applying to pure functions, why is that?

1 Like

Hi @ThomasEnder,

The error is that the modifier is reading from state so should be view.

I recommend doing a simple contract and compiling in Remix.

MyContract.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyContract is Pausable, Ownable {
    constructor() {}

    function pause() public onlyOwner {
        _pause();
    }

    function unpause() public onlyOwner {
        _unpause();
    }

    function viewStuff()
        public
        pure
        whenNotPaused
        returns (uint256)
    {
        return 42;
    }
}

Compilation Error

TypeError: Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". --> contract-43842b302e.sol:21:9: | 21 | whenNotPaused | ^^^^^^^^^^^^^

As an aside, I am not sure of the value of using whenNotPaused on a view function unless you are trying to prevent it being read onchain whilst paused, as it is trivial to read state offchain.

1 Like