I found this in the official solidity docs.
The values of all members of msg, including msg.sender and msg.value can change for every external function call. This includes calls to library functions.
The "can change for every external function call" is too vague for me.
Does anyone know when it does change for an external function call?
My concrete example:
pragma solidity ^0.5.10;
contract A {
function foo() external view returns(address payable) {return msg.sender;}
}
contract B is A {
address payable public msgSender;
function bar() external {
// 3 ways of doing the same thing
msgSender = A.foo(); // does this work and who will be msgSender?
msgSender = this.foo(); // does this work and who will be msgSender?
msgSender = super.foo(); // does this work and who will be msgSender?
}
}
Please take all the code as is => in the 3 notations A.foo()
this.foo()
super.foo()
I am NOT talking to another deployed contract instance. With all 3 ways I want to showcase 3 (different but same) ways of calling external functions
(inherited
ones in this case) on a contract
from inside
(internally
) that same contract
. The 3 ways basically just vary in their scope
or explicitness I believe.
Back to my main question:
Who is the msg.sender
emitted in the EmitMsgSender
event
emitted by the call
to A/this/super.foo()
in my example?
Is it contract B
's address payable
or is it the address
that sent the tx
to B.bar()
?
By the way, it would make my life easier, if it were the original caller of the B.bar()
function, and not B
's address.
Thanks for your help!
foo
is external, and you cannot call external functions from the same contract (and sinceB is A
, we're talking about the same contract here). So you probably wanna change it topublic
orinternal
and then simply give it a try and see what you get in the emitted event. BTW, you might find it easier to test the whole thing it you declare the function asview returns (address)
and simply returnmsg.sender
.external
functions inside the samecontract
: " An external function f cannot be called internally (i.e. f() does not work, but this.f() works)." (see the docs: solidity.readthedocs.io/en/latest/…)