0

I'm working with an standard ERC20 token, I have a transfer function:

 function transfer(address _to, uint256 _value)  public returns (bool) {
        require(_to != address(0));
        require(_value <= balances[msg.sender]);

        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

I have an address A that has 20 of my ERC20 tokens, and I'm using web3.js to send transactions to a smart contract that is deployed on a Geth private node. MetaMask is intercepting those invocations.

I'm curious about the behaviour of MetaMask in these two scenarios:

  1. Send 19 tokens from address A to adddress B, the transaction succeeds, now A has 1 token

  2. Try to send 21 tokens from address A to address B, metamask shows this message:

MetaMask notification

It is as if MetaMask knows that the precondition of the contract will fail:

    require(_value <= balances[msg.sender]);

I'm curious to know if MetaMask has its own state of the contract and can determine if a transaction will fail or not, previous to invoking the contract, or while it is invoking the contract.

Greenonline
  • 250
  • 1
  • 4
  • 11

1 Answers1

0

It steps through the transaction in a read-only mode as part of the gas estimation process. It can see when the transaction is likely to fail.

Hope it helps.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
  • Thanks, is there any place where i can read about that? Im curious about how Metamask does the read-only step through – Marcos Martínez Jun 27 '18 at 15:13
  • 1
    MetaMask doesn't do it. It calls eth_estimateGas on the Ethereum node it's talking to. This does much the same thing as eth_call... it executes the transaction locally to measure the gas usage and then just throws away the side effects. – user19510 Jun 27 '18 at 15:25
  • I agree with Steve about the detailed sequence. Gas estimation works by stepping over the code. You can also invoke it any time you like to explore function responses. Have a look over here: https://ethereum.stackexchange.com/questions/765/what-is-the-difference-between-a-transaction-and-a-call – Rob Hitchens Jun 27 '18 at 15:31
  • Ok, so metamask calls eth_estimateGas, the Geth node invoke the transfer function of my contract but discards any modifications on the blockchain, so the eth_estimateGas doesnt affect the blockchain state. Thats make sense. Thank u guys – Marcos Martínez Jun 27 '18 at 15:40