9

How to import a contract in another one without using import but using the address of that particular contract and accessing its functions and variables in different files in Solidity?

Gagan
  • 225
  • 3
  • 6

4 Answers4

8

Here's how an example of 2 contracts in 1 file can be split into 2 files:

C1.sol has:

contract C1 {
    function f1() returns(uint) {
        return(10);
    }
}

C2.sol has:

contract C1 { function f1() returns(uint) {} }
contract C2 {
    function f2(address addrC1) returns(uint) {
        C1 c1 = C1(addrC1);
        return c1.f1();
    }
}

C2.sol does need to have a stub (abstract contract code) of C1, so that the compiler knows the functions/interface of C1.

eth
  • 85,679
  • 53
  • 285
  • 406
3

When you instantiate a contract with its address as parameter, then you have access to this very contract:

contract C1 {

  function call (address myContractsAddress, uint aParameter) returns(uint){
    C2 c2= new C2(myContractsAddress);
    return c2.aFunction(aParameter);
  }
}

contract C2 {
  function aFunction(uint aParameter) returns(uint) {
    return 1;
  }
}
Roland Kofler
  • 11,638
  • 3
  • 44
  • 83
  • @Roland that's right but can i use it without importing a contract into another and just by using the address i can let a contract know which one i am using just by giving address possible or not ?example i have two contracts in different files and i want to use one in another for certain functionalities. – Gagan Jun 02 '16 at 11:17
  • At least you need to know the type of contract, i.e. calling the right constructor. I edit it accordingly. – Roland Kofler Jun 02 '16 at 11:19
  • But you want to call the other contract without knowing what type of contract it is right? I've seen this somewhere... – Roland Kofler Jun 02 '16 at 11:28
  • yup exactly..i don't want to use import just by the address ,like if i know the address of that particular contract. – Gagan Jun 02 '16 at 11:42
  • a trace to the solution could be that you can execute ABI code that you send to the contract before: https://ethereum.stackexchange.com/questions/4124/is-there-a-way-to-call-a-solidity-function-by-its-string-name – Roland Kofler Jun 03 '16 at 19:24
  • @RolandKofler contract C2 doesn't have a constructor with argument. Possibly it will throw error at compile time. I checked it in online solidity browser. May be wrong. – Aniket Jul 28 '16 at 13:55
2

If you know the address and function signature of the other contract, you can call based on it's address. The solidity docs have an example:

address nameReg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2;
nameReg.call(bytes4(sha3("fun(uint256)")), a);

Note that the last line generates the hex of the function signature which you could precompute...

nameReg.call('0x7a9839c2', a);
o0ragman0o
  • 4,320
  • 18
  • 35
1

That is not possible. Importing contracts is exactly the way to inform the compiler that functions the other address have. A pure address may only contains EVM bytecode but it is impossible to recreate high-level API out of it.

There are ideas to publish contracts' interface descriptions next to the blockchain but nothing of that has been done yet.

Paweł Bylica
  • 1,355
  • 1
  • 9
  • 28