msg.sender, msg.value and address(this)

Solidity Simplified

Hello readers.

In this article, I will be explaining all that we need to know about msg.sender, msg.value and address(this) in solidity.

Let us start by explaining what msg.sender is.

msg.sender

This is the address of the person that is interacting with the smart contract at that particular point in time. The code below explains better.

pragma solidity ^0.8.0;
contract Michael{
function sender() public view returns(address){
return msg.sender;
}
}

image.png

From the above image, we can see that the function named sender is showing the address of the msg.sender which is the person that called the sender function.

msg.value

msg.value is the amount of ether sent in a transaction. It's alway in the smallest unit which is wei.

address(this)

This is referring to the address of the instance of the contract where the call is been made.

Difference between msg.sender and address(this)

Many developers do confuse the address of msg.sender with address(this). The difference is clear. Address of msg.sender is the address of the person interacting with the contract also known as your wallet address. While address(this) is the address of the instance of the contract where the call is been made. So they are two different addresses.

See the image that explains the difference between the msg.sender and address(this) below.

image.png

See the code below.

pragma solidity ^0.8.0;
contract Michael{

function addressthis() public view returns(address){
return address(this);

}

function msgsender() public view returns(address){

return msg.sender;

}
}