1

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 ?

1 Answer 1

2

Keyword payable in the function is needed to make the function able to receive ether. Sending ether is possible from any function, as long as the address sending is payable.

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.