2

I am writing a smart contract which receives Ether.

How can I transfer Ether from the contract to my personal purse (non programmatically using such a program as Ethereum Wallet)?

Can I send Ether from the contract like as I can do with regular wallet?


In the "possible duplicate" question it is not said how to withdraw from the contract non-programmatically. It is should be easy for a user to withdraw, without him writing a code.

2

4 Answers 4

6

Basically it all depends on your contract.

Let's have an example contract:

contract test {
    function payme() payable public {
    }

    function getBal() public returns (uint) {
        return this.balance;
    }
}

You can save Ether in this contract by calling the payme function. Also you can query how much Ether the contract has. But the Ether can't be transferred away, so it's stuck in the contract forever.

@Victory's answer has a good idea about how to enable withdrawals without any coding. But that of course requires the contract to support such functionality.

2

You can do this instead :

import "@openzeppelin/contracts/access/Ownable.sol";


function withdraw() public onlyOwner {
    uint256 amount = address(this).balance;
    require(amount > 0, "Nothing to withdraw; contract balance empty");
    
    address _owner = owner();
    (bool sent, ) = _owner.call{value: amount}("");
    require(sent, "Failed to send Ether");
  }

  // Function to receive Ether. msg.data must be empty
  receive() external payable {}

  // Fallback function is called when msg.data is not empty
  fallback() external payable {}

}

0

If you are not writing a web3 interface you only have one real option and that is the default function of your contract. You would tell the user to send 0 ETH (sending 1 wei might also make sense) to the contract which would then trigger the withdraw function.

contract MyContract {
   uint allTheEth;

   function withdraw()
   public
   {
      msg.sender.transfer(allTheEth);
   }

   function()
   public
   payable
   {
      // user is sending nothing so they must want to withdraw
      if (msg.value == 0) {
         withdraw();
      // sender is sending something so must want to depost
      } else {
         allTheEth += msg.value;
      }
   }
}
2
  • 1
    Per Lauri's answer above, you don't need to track your balance in allTheEth, right? You can just use address(this).balance ?
    – Rich
    Commented Apr 2, 2018 at 0:39
  • 1
    @Rich correct, the balance of the contract is just this.balance.
    – Victory
    Commented Apr 2, 2018 at 22:44
0

Receiving Ether on smart contract is not a good idea. but if you want to do that you can make payable function to transfer the ETH. you can find the TransferHelper.sol on net

function transferETH(address _receiverAddress) public payable {
    TransferHelper.safeTransferETH(_receiverAddress, msg.value);
}
2
  • Could you elaborate on why 'receiving Ether on smart contract is not a good idea'? Commented Dec 5, 2022 at 7:02
  • Because of attacks that hackers can made on smart contract and stole all your the ETH Commented Dec 6, 2022 at 9:26

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.