0

I'm prototyping an application that needs to call another deployed function in order to work properly & while prototyping I had a weird error appear on Remix IDE stating that TypeError: Functions in interfaces must be declared external, yet when I go to solidity lang spec towards the bottom is states that public: visible externally and internally (creates a getter function for storage/state variables) for public functions.

My reading this I would assume I'd be able to make an interface as such to represent the functions I'd like to call as such.

// Contract to Call
interface HelloInterface {
    function set(uint x) public;
    function get() public view returns (uint);
}

contract Main {

    HelloInterface helloContract;

    address hwAddress = 0xf8e81D47203A594245E36C48e151709F0C19fBe8;

    function setHelloInt(uint v) external {
        helloContract = HelloInterface(hwAddress).set(v);
    }

}

Can I only call functions that are labeled external in solidty or do I have to model my call structure a different way? I've come across CALL | DELEGATECALL | CALLCODE but haven't look into them yet.

Should I change the HelloInterface to a contract instead & create the functions to call?

1 Answer 1

2

An interface is used to communicate with the original contract from a external contract.

If there is an already deployed contract and if you want to communicate with it or call the functions of the contract, you need things below and should meet the following requirements.

  • An interface of the contract.
  • Address of the smart contract.
  • The function visibility should be either public or external. You cannot make a call to internal or private functions.

Sometimes compiler errors are senseless and stupid. However in this case, solidity compiler error does make sense.

It is like, if you are using an interface to make external calls and you only write abstract view of the original function, why do you want it to be public or internal or private?

Functions in the interface need not to be public because even if the compiler automatically generates getter function, it is useless.

In short, function visibility in the interfaces should be external and you can use those functions in the contract normally.

In your case,

// Contract to Call
interface HelloInterface {
    function set(uint x) external;
    function get() external view returns (uint);
}

contract Main {

    HelloInterface helloContract = HelloInterface(0xf8e81D47203A594245E36C48e151709F0C19fBe8);

    function setHelloInt(uint v) public{
       helloContract.set(v);
    }

}

Tell me if it helps!

2
  • So even if get is a public function, I'm still able to call it, all I have to do is write it as external on the Interface. Does this sound correct?
    – CoderMan
    Commented Nov 15, 2022 at 18:39
  • Exactly! Functions with public and external visibilities can be called from the outside the contract and the difference between the both are that the external functions cannot be called internally.
    – Ad-h0c
    Commented Feb 21, 2023 at 10:52

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.