1

Take this scenario:

Contract A {
   function whoIsSender() public returns (address){
      return (msg.sender);
   }
}

Contract B is A {
   function withThis() public returns (address){
      return (this.whoIsSender());
   }

   function withoutThis() public returns (address){
      return (whoIsSender());
   }
}

I was expecting both functions to return the same. Apparently it is not. It seems that using the this keyword breaks it to a contract-to-contract invocation and then msg.sender becomes the contract address instead of the wallet? Is this by design and can someone explain why?

3

This is touched on a bit here: https://solidity.readthedocs.io/en/v0.4.21/contracts.html#visibility-and-getters, but essentially what you're saying is correct.

<address>.<function>() (e.g. this.whoIsSender()) sends a message to the contract at that address. This is an external call, and msg.sender is the address of the account that made the call.

<function>() (e.g. whoIsSender()) does an internal function call (just a JUMP).

  • How about checking own balance using this.balance? Is it an external call? Is there any other way to get contract's own balance other than this.balance? – shaharsol Jun 21 '18 at 17:44
  • 2
    No, this.balance is not any sort of call. That syntax is also deprecated: use address(this).balance instead. – smarx Jun 21 '18 at 17:51

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.