2

For

pragma solidity ^0.6.0;

interface ExternalContract {
    function initialize() external;
}

contract Test {
  ExternalContract c;
  
  constructor(address externalContractAddress) public {
      c = ExternalContract(externalContractAddress);
  }

  function testA() public {
    c.initialize();
  }

  function testB(address externalContractAddress) public {
    ExternalContract(externalContractAddress).initialize();
  }
}

Is there a preferred option in terms of cost between testA and testB? I can see that B allows you to save on a state variable, but don't know whether on the spot instantiation is somehow more expensive.

2
  • Please rewrite the code in this question as a working example. The address keyword cannot be used as an argument, so rename it to begin with. Then, please explain where it is coming from (i.e., a state-variable or a function argument, because at present it is neither, which doesn't make any sense). Dec 2 '20 at 6:58
  • @goodvibration done
    – Joël
    Dec 2 '20 at 7:41
1

With c being a state-variable, this:

c.initialize();

Costs 800 more gas units than this:

ExternalContract(externalContractAddress).initialize();

Because it includes loading the value of c from storage (i.e., an additional SLOAD operation).

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.