0

I am coding a crowdfunding contract where senders have two options. First, payIn which invokes a transfer to the contract's address. Second, withdraw which invokes a transfer from the contract's address to the sender. However, I cannot transfer to the contract's address because it is not payable. How can I make the contract payable?

pragma solidity >=0.7.0 <0.9.0;

contract Crowdfunding{
    // Crowdfunding settings.
    address payable owner;
    uint256 goal;
    bool isOpen;
    
    // Store the crowd funding values
    mapping(address => uint256) public crowd;

    constructor(uint256 _goal) {
        owner = payable(msg.sender);
        isOpen = true;
        goal = _goal;
    }
    
    function payIn() public fundingIsOpen payable {
        address(this).transfer(msg.value);
    }
    
    function withdraw() public fundingIsOpen payable {
        address(msg.sender).transfer(crowd[msg.sender]);
    }

...

1 Answer 1

0

Lots of ways to go about this, one that would work is

function withdraw() public fundingIsOpen payable {
    payable(msg.sender).transfer(amount);
}
0

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.