0

I have ERC20 contract with payable function.

1

function () external payable {
    uint256 amount = msg.value * unitsOneEthCanBuy / 1000000000000;
    balances[msg.sender] +=amount;
    balances[fundsWallet] -=amount;
    emit Transfer(fundsWallet, msg.sender, amount);
    fundsWallet.transfer(msg.value);
    }

2

function () external payable {
   
    create(msg.sender);
   
    }
    
function create(address _beneficiary) public payable {
    uint256 amount = msg.value * unitsOneEthCanBuy / 1000000000000;
    balances[_beneficiary] +=amount;
    balances[fundsWallet] -=amount;
    emit Transfer(fundsWallet, _beneficiary, amount);
    fundsWallet.transfer(msg.value);
    }

What better 1 or 2?

1

What do you mean "better"?

The 2nd option might be slightly more expensive than the 1st option, but in a negligible manner.

I am using the word "might", because compiler optimization may even make both options identical.

However, if you wish to expose your payment method to other contracts (and not only to externally-owned accounts), then you pretty much have to implement the second option, because the fallback function by itself has a stipend of 2300 gas and is therefore essentially non-usable by other contracts.

So the real question here is whether you need to expose this functionality only to externally-owned accounts, or also to other contracts.

  • "fallback function by itself has a stipend of 2300 gas" - as I understand 1st and 2nd are equal gas cost? – Alex May 7 '19 at 16:39
  • @Alex: Sorry, but I don't understand your question in the context of the statement preceding it. – goodvibration May 7 '19 at 16:51
  • 2
    "because the fallback function by itself has a stipend of 2300 gas and is therefore essentially non-usable by other contracts." <-- This is untrue. A contract calling the fallback function can specify how much gas is provided, just like calling any other function. The 2300 stipend you're thinking of is probably the amount of gas forwarded when you use <address>.send or <address>.transfer. – user19510 May 7 '19 at 16:52
  • 1
    @smarx: AFAIK, a contract can call the fallback function via either one of the two options that you've mentioned (and perhaps also via call). And when doing so, an additional amount of 2300 gas is provided, restricting the execution to a very small range of options (e.g., even a single storage write requires more than that stipend). So afaik, it is essentially impossible to invoke the fallback function of a contract from the fallback function of another contract (unless the former spends almost nothing). Perhaps I did not explain that properly. – goodvibration May 7 '19 at 17:13
  • 1
    As you said, call is what you'd use to specify a different gas amount. E.g. address(otherContract).call.value(1 ether)(""). That would invoke otherContract's fallback function while forwarding all available gas. – user19510 May 7 '19 at 17:21

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.