How msg.sender changes through relaying function calls between contracts?

by Jung Chun   Last Updated December 06, 2018 13:28 PM

Here is the example for question.

contract example1 {
  function A() {
    print msg.sender; // just fake code to imagine it prints out the result
    example3.C();
  }
}

contract example2 {
  function B() internal {
    print msg.sender; 
  }
}

contract example3 is example2 {
  function C() {
    print msg.sender;
    B();
  }
}

If I call function A(), then A() will print my address, and C() will print contract example1's address. Then is B() will print contract example1's address or contract example2's address?



Answers 1


As per the Solidity docs:

When a contract inherits from other contracts, only a single contract is created on the blockchain, and the code from all the base contracts is compiled into the created contract.

which means code of example2 will be added in example3 and example3 will look like:

contract example3 {
  function C() {
    print msg.sender;
    B();
  }
function B() internal {
    print msg.sender; 
  }
}

So as mentioned here, c() will print example1's address twice.

A.K.
A.K.
December 06, 2018 13:19 PM

Related Questions


Updated July 13, 2018 12:28 PM

Updated July 12, 2017 11:28 AM

Updated July 17, 2017 21:28 PM

Updated November 15, 2017 20:28 PM