Below is my fallback function for ethereum. It currently require 91000 gaslimit which is way more than 21000. Metamask always starts the transaction with 21000 gas stipend due to which transaction fails always. Is there anyway Metamask calculates the actual gas needed because I cannot tell each user individually to send 91000 gas along they will believe in gas whatever Metamask will suggest OR if we can reduce the gas price from 91000 to 21000 somehow.

function deposit() external payable {                                                   
    makeDeposit(msg.sender, msg.value);

function makeDeposit(address sender, uint256 amount) internal {
    require(balances[sender] == 0);
    require(amount > 0);
    balances[sender] = balances[sender].add(amount.mul(1000));                      // mint new tokens
    _totalSupply = _totalSupply.add(amount.mul(1000));                              // track the supply
    emit Transfer(address(0), sender, amount.mul(1000));                            // notify of the transfer event
    depositor[sender].time = now;
    depositor[sender].amount = amount;
    emit Deposited(sender, amount);

Metamask is not correctly estimating the gas because this is not your fallback function. A fallback function is of the format (notice there is no function name):

function() external payable {}

If you are not providing data to the transaction in Metamask, it will assume you are sending a normal transaction and will estimate 21000 gas.

In order to correctly estimate the gas cost, you must accurately call the the function, which can be done through the fallback function. Your code should look as follows:

function() external payable {

Or, more simply:

function() external payable {
    makeDeposit(msg.sender, msg.value);
| improve this answer | |

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.