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).

6 Answers 6

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.

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

2
  • this, not this.address
    – user19510
    Feb 17, 2018 at 20:46
  • 4
    Also, this has been deprecated in favor of address(this). May 29, 2018 at 22:59
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?

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.

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.

3
  • 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, 2022 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? Jul 25, 2022 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, 2022 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.