Skip to content

Withdraw in solidity #1

Description

@S23Web3

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions