2

I'm creating a new smart contract project in which I'll be selling tokens. I need a function that allows me to harvest the funds in the contract into my own wallet. I have built this function:

function harvest() public onlyCreator {
    require(msg.sender == creator, "Action can only be performed by contract creator");
    payable(creator).transfer(address(this).balance);
}

It seems to be working ok on my tests. My concern is that all the documentation I've read about transfers state that using account.transfer always use the same gas and does not allow for a custom value. At first sight this seems ok to me, since I'll only be doing simple transfers to a single creator address, but my doubt is if not allowing for a custom gas fee could potentially lock funds in the contract if the network changes the minimum gas requirements.

Is this scenario likely to happen? Am I safe using account.transfer?

4

If you are sending Ethers to a wallet address, it's perfectly ok to use transfer.

The problem with the transfer function is that it forwards a constant amount of gas (2300) to the recipient. If the recipient is a wallet, that gas is not used nor needed. If the recipient is another contract, the contract has 2300 gas to do some processing (it's mostly enough just for some logging). If some gas costs were to change (as they sometimes do), the receiving contract may revert due out of gas, which probably also causes your sending contract to revert as well, and in that way your contract withdrawal may become useless.

You can read more about this general problem here: https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/

2
  • Nice answer, finally some simplification on the subject, thanks. Am I right to presume that by sending to a wallet address, I'll receive the transfer amount + 2300 gas in Wei?
    – sigmawf
    Jul 26 at 17:53
  • No you don't receive the gas - gas can't be transferred. The 2300 gas is simply not utilized. Jul 27 at 5:01

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.