3

I completed the truffle framework pet shop tutorial using Metamask and Ganache. The blokchain (locally hosted on port 7545) stores my "pet adoptions" which are represented as ethereum transactions. I can view the past transactions either in Ganache or by clicking on the Metamask icon in Chrome.

The problem arises when I close Ganache and start it again. There are now no transactions shown in Ganache, as (according to my understanding) the blockchain is stored in memory, so after restarting Ganache it is blank.

However, Metamask still shows my past transactions, even though they should be gone. This leads to inconsistencies in my pet shop app. Furthermore, my adoptions now don't work: Upon trying to adopt a pet, I get the following error:

Error: the tx doesn't have the correct nonce. account has nonce of: 0 
tx has nonce of: 10

Does Metamask store the transactions somewhere, and that's why they're still displaying? How does Metamask actually interact with the blockchain?

Edit: Turns out the only solution I have found is to remove Metamask and install it again.

1 Answer 1

2

The problem is due to the fact that Ganache starts on the same network ID a new blockchain after every restart. While Metamask keeps the history of the transactions and use it to determine the nonce (index of each transaction for an account).

You have a few solutions here:

  1. As you said, install/uninstall Metamask to reinitialize the account history

  2. You can change the Network ID in Ganache every time you restart it

enter image description here

  1. Third solution will consist in providing the nonce yourself for every transaction. For that, you can use web3.eth.getTransactionCount which ill provide you the correct nonce for this address.

Example

setTimeout(function() { // Wait for metamask for being injected

    if (typeof web3 !== 'undefined') { // Check if metamask installed

        if(web3.eth.accounts.length > 0) { // Chek if metamask unlocked

            window.web3 = new Web3(web3.currentProvider);

            const from = web3.eth.accounts[0];
            const to   = "0xf17f52151EbEF6C7334FAD080c5704D77216b732";
            const value     = web3.toWei(1, 'ether');

            web3.eth.getTransactionCount(from, function(err, nonce) { // Get the nonce
                if(!err) {

                  const transaction = {
                     "nonce"        : nonce,
                      "to"          : to, 
                      "value"       : value
                    };

                    web3.eth.sendTransaction(transaction, function(err, tx) { // Sent the transacion
                        if(!err)
                           console.log(tx)
                    });
               }

            });
        } 
     }
}, 500);

/ !\ In the solution 2 and 3, you might need to switch network in Metamask to another network and go back to localhost to reset the network ID or the balance correctly after the ganache restart.

Your Answer

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

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