I understand that a function must be declared as payable
in order to send ETH to some address.
I am trying this piece of code using Solidity 0.7.1
pragma solidity ^0.7.1;
contract SendMoney {
uint public balanceReceived;
function sendETHToSmartContract() public payable {
balanceReceived += msg.value;
}
function getBalance() public view returns(uint){
return address(this).balance;
}
function sendETHOutToCaller() public {
msg.sender.transfer(getBalance());
}
}
I declared function sendETHOutToCaller()
without payable
. When I run it , I can call function sendETHOutToCaller()
to transfer the ETH from smart contract to the caller. Works fine on Solidity 0.7
Why in this case I am able to transfer the ETH from smart contract to the caller without payable
?