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

when I run the code the output

msg.sender.balance = 99

address(this).balance = 0

    // SPDX-License-Identifier: GPL-3.0
    pragma solidity >=0.7.1 <0.9.0;
    contract test{
        uint public myBalance1=msg.sender.balance;
        uint public myBalance2=address(this).balance;

1 Answer 1


address(this).balance is the balance of the smart contract itself.

msg.sender.balance is the balance of the user that made the transaction, and if msg.sender is another contract, then it's the balance of that other contract.

this refers to the contract instance that is executing. msg.sender is the user or contract that just called the other contract.

I you call the contract, then msg.sender is you and msg.sender.balance is the balance of your address, the one that you used to send the transaction. this, inside a contract, is the contract that you just called, not you.

If you call a A and that contract calls another contract B, then msg.sender inside B will not be you but will be A's contract address. Inside A, msg.sender is you, because you are the one that called it, but inside B, msg.sender is A, because a called B.

  • 1
    im who call and deploy contract with the same address, why not the same balance output Commented Aug 23, 2022 at 18:06
  • Because your account and the smart contract you deploy are two different entities, with their own balance. If you want to see a balance on your smart contract, then you need to implement the receive() external payable or fallback() external payable or any payable function to accept ether on your contract, which, by default, does not accept ether unless you define a payable function. Commented Aug 23, 2022 at 18:25

Your Answer

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

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