0

Is it somehow possible to do something like this?

contract Test {
    function test (bool isTransaction) public view {
        if (isTransaction) {
          foo();
        }

        ...
    }
    
    function foo () public {
        ...
    }
}

In the best case I would be able to call test() like an usual view/pure function without the need of making a transaction but if isTransaction is true test() requires me to make a transaction in order to call it. Maybe I can throw an exception or something similar to be able to compile and run this code?

EDIT: bool transaction got bool isTransaction

2
  • I think foo() must be pure or view, so no
    – Majd TL
    Commented Apr 4, 2021 at 13:34
  • I saw the answer, but I still don’t get it :/ maybe I’m messing something or u are making it complicated. For me -> if u know it is a transaction then call foo directly, if not then call a normal view function that has nothing to do with foo :/ it is always better to reduce the logic in smart contract as much as u can
    – Majd TL
    Commented Apr 4, 2021 at 20:31

2 Answers 2

3

You can accomplish something like this using staticcall:

contract Test {
  function test (bool transaction) public view {
    if (transaction) {
      (bool success,) = address(this).staticcall(abi.encodeWithSignature("foo()"));
      require(success, "Contract call failed");
    }
  }
    
  function foo () public {
    // ...
  }
}

This will compile, and test will execute without errors as long as foo() does not modify the state. If foo() does modify state, it will revert because success is false.

5
  • interesting didn't think about that, but where can we use such a thing or logic :/?
    – Majd TL
    Commented Apr 4, 2021 at 16:25
  • I´m not sure what you mean by "where can we use such a thing or logic"?
    – Morten
    Commented Apr 4, 2021 at 16:29
  • For calling foo() I need to make a transaction so is it possible to call test() making an transaction if isTransaction is true and if not I call test() like a normal view function so without needing a transaction?
    – Paulus
    Commented Apr 4, 2021 at 16:33
  • I edited the question because it was not quite clear what I wanted although @Morten's answer comes closer to what I need
    – Paulus
    Commented Apr 4, 2021 at 16:38
  • @Morten I mean what is the purpose of a view function that call a normal function based on a given parameter :) in other words any useful use case for that
    – Majd TL
    Commented Apr 4, 2021 at 20:08
1

the answer is no. it will not compile

view functions cannot modify the state and the compiler check this and give you back

TypeError: Function declared as view, but this expression (potentially) modifies the state

Tested it with solidity 0.8.3

enter image description here

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.