1

To reference another contract, the ABI is easily included like this. What information needs to be there? The function name, I guess, what about accessability such as public/private/internal/external/pure/view, or if it returns a type and what it returns? Asking for what is needed, not so much what looks good or makes it more human readable.

contract A { function foo() public returns (uint) {} }

contract B {

    A a = A();
3
  • 1
    Needed: function name, function access-level, type of each input argument, type of the return-value. Not needed: name of each input argument, name of the return-value. Commented Jan 13, 2019 at 8:15
  • thanks! why is function access-level needed?
    – aSmurf
    Commented Jan 13, 2019 at 8:45
  • to clarify, to call a function on a contract, the name of the function needs to be known, for the function signature, same with type of each input argument, and for the return value, I think I see how that needs to be known as well (manual calls with bytes4(keccak256(“foo(uint256)”)) cannot take return values without assembly "hack"), but, function access-level, I do not see why it is needed or what it does
    – aSmurf
    Commented Jan 13, 2019 at 13:03

2 Answers 2

1

You just need an interface for the contract that you want to reference. Just add the functions prototype and you're good to go.

Example

contract ERC20 {
  function allowance(address owner, address spender) constant returns (uint);
  function transferFrom(address from, address to, uint value);
  function approve(address spender, uint value);
  event Approval(address indexed owner, address indexed spender, uint value);
}

contract test {
    ERC20 token = ERC20(address); // to reference 
    ERC20 token = new ERC20(); //creates new contract

    token.transferFrom(from, to, value); //call the transferFrom function of the reference contract or newly created one if thats the case.
}
1

In order to construct proper call code, Solidity need the following information about the function being called:

  1. Name
  2. Argument types
  3. Return types

Call code generated by Solidity basically does no following:

  1. Packs 4-byte function selector (derived from function name and argument types)
  2. Pack arguments (needs to know argument types here)
  3. Parse returned value (needs to know return types here)

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.