How to call a method of a contract whose address is known, In Another contract.?

mahendra.d.panchalmahendra.d.panchal Ahmedabad, IndiaMember Posts: 10
edited July 2015 in Solidity

I have created basic contract(Age)


contract Age {
    uint ageNo = 123;
    function getAge() returns(uint) {
        return ageNo ;

 and put it on the test net.  then create second contract(Memebr)

contract Member {
   // declare age contract
   Age age;
   uint memberAge;    

    function setAge (address addr) {
       // explicit type conversion
        age = Age(addr);
      // calling age contract method
      memberAge = age.getAge().value(10).gas(800)();
    function getMemberAge() returns(uint) {
        return memberAge;
But I'm try to compile 'Member' contract, I throw me compilation error.

at declaration of Age contract :
Age age; // Declaration error: Undeclared identifier.

Is it right way to call method, from one contract to another contract, mean we perform explicit type conversion with address.

Hope someone help me to understand.

Post edited by StephanTual on


  • chrisethchriseth Member Posts: 170 ✭✭✭
    The simplest way to fix this is to put both contracts in the same file.
    If you want to call getAge normally, just use
    If you want to control the value and gas, use
  • mahendra.d.panchalmahendra.d.panchal Ahmedabad, IndiaMember Posts: 10
    edited July 2015
    Thanks @chriseth
    When I put both contract on single file. it works fine.


    Suppose, my both contract on different file,
    Is it possible for this way.?
  • chrisethchriseth Member Posts: 170 ✭✭✭
    yes, use
    import "filename";
    and specify all files of your project at the time of compilation.
  • mahendra.d.panchalmahendra.d.panchal Ahmedabad, IndiaMember Posts: 10
    In directory "/home/user/con" contain my two files "Age.sol" and "Member.sol".

    I have to import path file, as

    import "/home/user/con/Age.sol";

    into Member contract, but still is not importing this file.?

  • chrisethchriseth Member Posts: 170 ✭✭✭
    You have to give all files that are imported at any point as arguments to the commandline compiler. The strings used for the import statements have to be exactly the same as the ones used as arguments to the compiler.
    solc file1.sol file2.sol
    import "file2.sol";

    The routine that resolves imports has no idea of a filesystem and only uses the names it is given by the commandline compiler. This way it is flexible enough to be embedded into a framework where e.g. imports are directly loaded via swarm and are hash-authenticated.
  • mahendra.d.panchalmahendra.d.panchal Ahmedabad, IndiaMember Posts: 10
  • mdjmdj Member Posts: 14
    Thanks for the information in this thread. How would an import work when compiling over RPC using web3 for example?
  • SergeonSergeon Member Posts: 2
    @criseth What if I want my contract to interact with another external, deployed contract, like the DAO main contract or whatever? How can I reference that in my solidity code?

    I.E. I would want to call a buyToken(amount) and maybe later a refund() method with my Test contract, on en external contract (i.e. a TokenMinter contract), which is actually deployed on the blockchain and owned by another account which I don't control on some known address.

    So my Test contract should be something like:

    contract Test{ address owner; TokenMinter tokenMinter; function Test( address tokenMinterAddress ){ owner = tx.origin; tokenMinter = //??? I don't know how to reference the external TokenMinter contract. //maybe the parameter shouldn't even be an address. //I imagine I should somehow get the TokenMinter abi i.e. using web3 and then pass related info //to this contract on creation. } function start(){ tokenMinter.buyToken(100); } function refund(){ tokenMinter.refund(); } function withdraw(){ //withdraw contract funds to owner account. } }

    Sorry to refloat and old thread but I am really having difficulty to find related info. Thanks in advance
  • chrisethchriseth Member Posts: 170 ✭✭✭
    Yes, this thread is really old :)
    You might want to consider posting future questions on the ethereum stackexchange.
    Basically what you have to do is: DAO x = DAO(0x123...). Then you can call functions on x as usual.
  • SergeonSergeon Member Posts: 2
    Thanks @chriseth ! Roger that
Sign In or Register to comment.