This is my code snippet for fallback function, If I use it by explicity using "fallback" function on Remix, then equivalent no of tokens do come in my wallet but if I send ethers via metamask directly to contract address then it fails always. See transaction hash: https://ropsten.etherscan.io/tx/0x0324500811aa9ee4b6fe0293458eab7b13c10e0cc4bc45b8c892bc6b318730f4

 * Fallback function when sending ether to the contract
 * Gas use: 
function () external payable {
    uint256 amount = msg.value;                                                     // amount that was sent
    require(amount > 0);                                                            // need to send some ETH
    balances[msg.sender] = balances[msg.sender].add(amount.mul(1000));              // mint new tokens
    _totalSupply = _totalSupply.add(amount.mul(1000));                              // track the supply
    depositor[msg.sender].time = now;
    depositor[msg.sender].amount = msg.value;
    emit Transfer(address(0), msg.sender, amount.mul(1000));                        // notify of the transfer event
    emit Deposited(msg.sender, amount);                                             // notify deposit event
  • The transaction fee is too low. I tried to send some ether and the first time Metamask erroneously said 21k gas, when I tried a second time it show 136k perhaps it's a bug in Metamask.
    – Ismael
    Aug 10, 2019 at 15:17
  • @Ismael yes, I observed that once I try to send some ethers it give me 110k fee and it went successful then I tried again it showed 21k fee and transaction failed, what shall I do currently? Can I somehow fix the gas fee? Aug 10, 2019 at 16:06
  • In Metamask there's an advanced option that allows to set the gas amount and gas price.
    – Ismael
    Aug 10, 2019 at 16:54
  • @Ismael, is there a way I can make it default in contract so metamask always require that amount of gas? because all users will not know what optimal gas to use Aug 10, 2019 at 18:16
  • No, it is not possible for a contract to set the gas price.
    – Ismael
    Aug 10, 2019 at 20:52

1 Answer 1


Your fallback function does too much for the 2,300 gas stipend. As a general heuristic, avoid setting state variables in fallback functions.

You need something like:

function deposit() external payable { ...

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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