0

I am working with this Solidity example, using geth console and/or Mist's compiler:

contract InfoFeed {
    function info() payable returns (uint ret) { return 42; }
}

contract Consumer {
  InfoFeed feed;
  function setFeed(address addr) { feed = InfoFeed(addr); }
  function callFeed() { attribut = feed.info.value(10).gas(800); }  
}

If, in the Mist compiler, I only include:

contract Consumer {
  InfoFeed feed;
  function setFeed(address addr) { feed = InfoFeed(addr); }
  function callFeed() { attribut = feed.info.value(10).gas(800); }  
}

which would make sense if e.g., I were to rely on someone else to (have) provide(d) the code for InfoFeed, I get the following compilation error msg:

 Identifier not found or not unique.
  InfoFeed feed;
  ^------^

How to compile Consumer in isolation please? The tutorials I looked at contains code bits of interdependent contracts, but don't tell me how to go about compiling them.

1 Answer 1

1

Some tools are clever about linking contracts stored in different files, but all you need to compile your contract when it calls someone else's contract is the function definition (function name, parameters, return values) for the contracts you want to call. You must already know this information, otherwise you wouldn't know how to write the code to call it. The simplest thing is just to stick that at the top of the file where you define the calling contract.

So here you could have done:

contract InfoFeed {
    function info() payable returns (uint ret) { }
}

contract Consumer {
  InfoFeed feed;
  function setFeed(address addr) { feed = InfoFeed(addr); }
  function callFeed() { attribut = feed.info.value(10).gas(800)(); }  
}

Note that I took the return 42; out of the info() function. The compiler doesn't need this information to compile Consumer. It also doesn't need any definitions for functions that you won't be calling.

The name of the contract you're calling also doesn't matter; Some people like to rename these stub contracts something like InfoFeedAPI, but you could call it Bob for all the compiler will care.

Your Answer

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

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