Friday, April 14, 2023

Don't Forget That "Withdraw" Function In Your Ethereum Contract

I've had this happen a couple times with some contracts I was creating early on where I did not have any way to retrieve funds. So if I was doing a simple contract to exchange tokens for Eth, all of that portion was working fine, but after doing some test transactions I realized I couldn't get the Eth out. 

Yup.

Nothing worse than seeing Eth build up and not being able to touch it, even if in test.

Yup.

Just make sure you don't forget this function

function withdraw() public onlyOwner {
  address payable receiver = payable(ownerDraw);
  receiver.transfer(address(this).balance);
}

And then import in the zepplin library in your sol file.

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