26

As per my understanding contract`s own address is the address which we assign to owner of contract using msg.sender. But I saw this question on SE where it was described that a contract can access its own address using address(this) , but when i compiled following script

 address owner; 
function test (string _name)public view returns(bool){
owner = msg.sender; 
return owner == address(this);

}

i get boolean result as false.

So What is the difference between msg.sender and address(this).

Anam Nizami
  • 885
  • 1
  • 8
  • 21

6 Answers6

36

this refers to the instance of the contract where the call is made (you can have multiple instances of the same contract).

address(this) refers to the address of the instance of the contract where the call is being made.

msg.sender refers to the address where the contract is being called from.

Therefore, address(this) and msg.sender are two unique addresses, the first referring to the address of the contract instance and the second referring to the address where the contract call originated from.

brianbhsu
  • 536
  • 4
  • 5
4

this is refers contract address. Contracts are non managable accounts which will not have any private key. That means if you created contract you don't have private key for contract A. msg.sender = Contract caller (Who is calling your contract) tx.origin = Transaction initiator

Refer bellow link for more details:

http://solidity.readthedocs.io/en/develop/units-and-global-variables.html

Jitendra Kumar. Balla
  • 2,154
  • 1
  • 8
  • 15
2

msg.sender is the address of the transaction invoker(address calling contract) whereas address(this) is the address of the contract itself. this keyword refers to the instance of a Contract.

1

Every account in Ethereum has an address. Contracts are special kinds of accounts and have their own addresses different from msg.sender and this.owner.

The contract's address is determined by the account that created it and its nonce. So if you create a new contract from address1 you can know in advance what’s the address of your new contract e.g. with this Solidity code: address(keccak256(0xd6, 0x94, address1, currentNonce)). You can find more details in this answer How is the address of an Ethereum contract computed?

medvedev1088
  • 10,996
  • 5
  • 35
  • 63
0

'this' is very common construct in most of the programming languages. It basically refers to the object/instance itself which is under execution. So in solidity too it means same.

Mandroid
  • 113
  • 4
0

I just want to add additional context to brianbhsu's answer (The most upvoted answer).

As I understand it, it could be the same in certain cases.

What I guess happened in your case was that you created the contract and then called the function from another account. Because you called it from another account, that account's address will be the msg.sender.

If however the function was a function that your contract call on its own, like a private function, then your contract address will be the msg.sender. In this case the Address(this) and msg.sender will be the same.

  • This is slightly incorrect, a private function by themselves will not change msg.sender. A contract calling its own an external function will change msg.sender. – Ismael Jul 24 '22 at 15:03
  • Thanks Ismael!

    Is it not the case that a private function can only be called by the contract since it is not publicly available to interact with?

    Is it possible for any other address that is somehow specified to interact with the private function?

    – Juan Jacques Pretorius Jul 25 '22 at 18:12
  • Private functions cannot be called from the outside the contract, and if they are called from within the same contract it will not change msg.sender. – Ismael Jul 25 '22 at 20:40