0

First, you see this code.

pragma solidity 0.8.7;

contract SendAndTransferEther {
    
    event payableMessage(address _from, address _to, uint256 amount);

    receive() external payable {}

    function checkBalance() public payable returns(uint) {
        return  address(this).balance;
    }

    function checkBalanceMsg() external payable returns(uint) {
        return msg.value;
    }
}

When I send 5 Ether, after that, I call the checkBalance() function it shows 5 Ether, but when I call checkBalanceMsg() function it shows 0 ether. I'm a little bit confused, What am I doing wrong.?

Look at the output.

Output of above code

1 Answer 1

0

I feel you forgot to set the value from the remix menu. That's why the value of msg.value is showing zero. As the checkBalanceMsgfunction has been marked payable, you need to send the value from the remix menu.

Lmk if doesn't get resolved by this.

enter image description here

8
  • If I don't set any value then how do I get the value this checkBalance(). Please see I'm output image.
    – Ng Sharma
    Commented Apr 24, 2022 at 10:19
  • The CheckBalance() function checks the balance of the smart contract, it returns whatever smart contract holds. Whereas the checkBalanceMsg() function returns the value that has been passed with the transaction and not the smart contract balance. Both functionalities are different. Commented Apr 24, 2022 at 10:22
  • @NgSharma I hope the above comment clears your confusion. Commented Apr 24, 2022 at 10:24
  • Okay, Its mean address(this).balance and msg.value are two different things.
    – Ng Sharma
    Commented Apr 24, 2022 at 11:09
  • 1
    Yeah! address(this).balance returns the ether balance of the contract while msg.value is the amount sent along with the transaction. Commented Apr 24, 2022 at 11:55

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.