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?

| improve this question | | | | |
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.

| improve this answer | | | | |
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;
  }
}
| improve this answer | | | | |
  • @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 2 '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 2 '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 2 '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 2 '16 at 11:42
  • a trace to the solution could be that you can execute ABI code that you send to the contract before: ethereum.stackexchange.com/questions/4124/… – Roland Kofler Jun 3 '16 at 19:24
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.

| improve this answer | | | | |
1

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);
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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