I wanted to withdraw an amount from a smartcontract. I thought I need to use the withdraw function.
eventually I found this
/ SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Contract {
address public owner;
address public charity;
constructor (address _charity){
owner = msg.sender;
charity = _charity;
}
receive() external payable {}
function tip() public payable{
(bool s, ) = owner.call{value:msg.value}("");
require(s);
}
function donate() public payable{
(bool s, ) = charity.call{value:address(this).balance}("");
require(s);
}
}
Later
I found out that donate does not need to be payable and still I do not know why it is not better to make a withdraw function and the boolean here.
I wanted to withdraw an amount from a smartcontract. I thought I need to use the withdraw function.
eventually I found this
/ SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Contract {
address public owner;
address public charity;
constructor (address _charity){
owner = msg.sender;
charity = _charity;
}
receive() external payable {}
function tip() public payable{
(bool s, ) = owner.call{value:msg.value}("");
require(s);
}
function donate() public payable{
(bool s, ) = charity.call{value:address(this).balance}("");
require(s);
}
}
Later
I found out that donate does not need to be payable and still I do not know why it is not better to make a withdraw function and the boolean here.