1

I was thinking in add a function to my contract in order to avoid locking ethers there (for example, someone can transfer ether to the contract in a mistake)

It is important to note that this contract is not supposed to store any ether in normal conditions. However, there are some payable functions in the contract used to transfer money between parts.

What do you think? Is it a good idea?

function withdrawETH () public onlyOwner {

    owner.transfer((address(this)).balance); 
}
1

This is a valid option, but the issue is that you must call this function in order to receive the funds. If the contract is not supposed to store funds, as you mentioned, you can simply forward the funds each time ETH may be sent through a payable function. See below for an example:

function payableFunction()
    public
    payable
{
    ...  // Function logic goes here
    forwardFunds()
}

function forwardFunds()
    internal
{
    owner.transfer((address(this)).balance); 
}

There are a few advantages to doing it this way. The first is that you, the owner, do not need to call the contract periodically, but rather the contributor is calling it each time. This also saves you a bit of gas, depending on the frequency. Additionally, this satisfies the condition of not wanting ETH stored in the contract itself.

2
  • 1
    Using this pattern, you could actually simply return the ETH to msg.sender which may be more wholesome :) Aug 17 '18 at 17:42
  • 1
    Note that it is possible that smart contracts receive funds as a result of a selfdestruct or of minin without the possibility to execute code, so the OP's solution would deal also with this cases.
    – Briomkez
    Mar 25 '19 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.