I am implementing crowdsale smart contract to accept ether and invoke ERC20 smart contract to assign tokens count calculated depending on the amount of ether invested.

I heard that just implementing default payable fallback is not right approach as that doesn't prevent customers from sending ether directly from exchange shared wallet accounts.

  function() payable {


What is the right approach then to implement fallback payable (apart from throw there) in crowdsale contracts so that customers could only send ether via MEW, Metamask and other private wallets?


To my knowledge, the fallback function has a stipend of 2300 gas when it comes to internal sends and transfers, from one smart contract to another. This happens because send and transfer haven't got a mechanism to set the gas spent so they are by default limited to 2300(on the other hand call consumes as much gas as needed). So they still trigger code execution, but the called contract is only given a stipend of 2,300 gas which is currently only enough to log an event.

If you receive ether from an external account I believe there is no gas limit for your send or transfer. So theoretically, if you accept ether only from external accounts you could use the fallback method. I believe the best practice is to revert any calls to the fallback and simply add a buy function that is payable.

Hope it helps!


If you want to enable those kind of transaction only you should check for tx.origin as well in order to exclude any possible different invocation of your fallback.

require(tx.origin == msg.sender, “EOA direct transfer not detected”); 

as first line of your fallback can be enough.

On the other hand this make your fallback not usable for manipulation by malicious contract as well.


If you are looking to implement a Crowdsale suggest you check the various types available in OpenZeppelin: https://docs.openzeppelin.org/v2.3.0/crowdsales

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.