0

pragma solidity ^0.8.4;

This modifier:

    modifier onlyNotContract() {
        require(!msg.sender.isContract(), "Contracts are prohibited");
        _;
    }

Is generating this error:

TypeError: Member "isContract" not found or not visible after argument-dependent lookup in address. --> contracts/test.sol:141:18: | 141 | require(!msg.sender.isContract(), "Contracts are prohibited"); | ^^^^^^^^^^^^^^^^^^^^^

This is looks like fairly routine boiler plate for a modifier. Any ideas?

0

1 Answer 1

0

Edited:

The method isContract is missing. You can just validate this way.

modifier onlyNotContract() {
  require(msg.sender == tx.origin);
  _;
}

Read more: https://ethereum.stackexchange.com/a/14016/76945

Thanks Nulik for suggesting.

--

Prior Answer:

⚠️ Warning: there is no secure way to prevent contract from calling for now. You can be hacked. Be mindful of using this method.

However if you still need it considering the risk, isContract() is missing from your source code. I am assuming it is from OpenZeppelin Address Library.

There are a few approaches to import,

Approach 1. You can copy the function isContract to do like this

  function isContract(address account) internal view returns (bool) {
    ..
  }
  
  modifier onlyNotContract() {
    require(!isContract(msg.sender), "...");
    _;
  }

Approach 2. Or if you want the whole library, you can import and use.

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Address.sol";

Contract Sample {
  using Address for address;
  modifier onlyNotContract() {
        require(!msg.sender.isContract(), "...");
        _;
    }
}
2
  • 1
    require(msg.sender == tx.origin) is pretty much it
    – Nulik
    Jul 18, 2021 at 10:48
  • 1
    the approaches 1 and 2 are incorrect, as it says in answer #2 in the link you referred
    – Nulik
    Jul 18, 2021 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.