0

I don't understand why remix doesn't allow me to compile this code, even though the logic is similar to the answers I found on the internet.

The error: from solidity: MyContract.sol:20:11: TypeError: Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. if (shop.isSold()) { ^-----------^

pragma solidity ^0.6.0;

interface IShop {

  function isSold() external returns(bool);
  function buy() external;

}


contract Attack {
    IShop shop = IShop(0x96678CF02e45F01e31683eDF23edE7dE863D0A76);
    
    function call() external {
      shop.buy();
    }

    
    function price() public view returns (uint) {
     
      if (shop.isSold()) {
        return 10;
      }else{
        return 100;
      }

    }
}

1 Answer 1

2

The error is clear: since price() is view, then all expressions inside can't modify the state. The compiler thinks that isSold can modify state since it's not marked view, so we need to fix that.

Basically, add view on the function in the interface

interface IShop {
  function isSold() view external returns(bool);
  function buy() external;
}
1
  • 1
    Thank you very much. Now everything became clear. Oct 13, 2022 at 15:14

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.