MetaMask/metamask-extension

Contract variables do not update immediately after state-changing transaction mined (Ganache-CLI + Metamask)

Closed this issue · 2 comments

Using Ganache-CLI as my testnet. Using Ganache-CLI as the Web3 provider, my code runs fine. Using Metamask as the Web3 Provider, my code does not run as expected.

What I'm doing:
(1) Using Ganache-CLI testnet
(2) Call value of public variable
(3) Increment public variable by 1
(4) Wait for transaction to be mined
(5) Call value of public variable

Expected behavior:
The value of the variable should have increased by 1 when calling it a second time.

Actual behavior:
It does not increase by 1. I need to manually call for the value of this variable again 5-10 seconds later to see the increased value.

Browser: Google Chrome
OS: Mac OSX

Here is the code I'm using to test with.

var Web3 = require('web3');

//// Here is my contract
//// Depoyed to Ganache testnet at 0xb53e0264f951C0ee4cA20Cfd5a082B8251844BD5
//
// pragma solidity ^0.4.16;
//
// contract Test {
//
//   uint256 public x;
//
//   function inc() public {
//     x += 1;
//   }
//
// }

let abi = [{"constant": true, "inputs": [], "name": "x", "outputs": [{"name": "", "type": "uint256"}], "payable": false, "stateMutability": "view", "type": "function"}, {"constant": false, "inputs": [], "name": "inc", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function"}]
let address = '0xb53e0264f951C0ee4cA20Cfd5a082B8251844BD5';

let txOptions = {
    from: '0x4dBa461cA9342F4A6Cf942aBd7eacf8AE259108C', // Unlocked account
    gas: 1500000,
    gasPrice: '30000000000000'
};

// const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); // Works as expected
const web3 = new Web3(Web3.givenProvider); // Does not work as expected

async function transactGanache() {
    let contract = new web3.eth.Contract(abi, address);
    let x = await contract.methods.x().call();
    console.log(x)
    let receipt = await contract.methods.inc().send(txOptions);
    x = await contract.methods.x().call();
    console.log(x)
}

transactGanache()

Using Ganache-CLI as Web3 Provider outputs 0 and then 1. Using Metamask outputs 0 and then 0

Same problem here. @jstoxrocky have you found a workarround?

Thanks for the report!

This may be a problem with ganache or web3 or the test code, rather than with MetaMask itself. I'd encourage you to post this in our community forum, or on the Ethereum stack exchange, if you're still encountering this issue.