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 =&gt; 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 Answers1

0

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

function withdraw() public fundingIsOpen payable {
    payable(msg.sender).transfer(amount);
}
Foxxxey
  • 4,307
  • 1
  • 6
  • 22