0

I am experiencing a weird issue. I am building a DApp where I want the new season of the game to start every 5 minutes (for testing purposes). However, web3 call returns old data, seems that it cached it and didn't refresh unless I send a tx. Here is a code snippet, smart contract:

uint public constant SEASON_DURATION = 5 minutes;

uint public start;

constructor() public {
    start = now;
}

function getCurrentSeason() public view returns (uint) {
    return (uint) ((now - start) / SEASON_DURATION);
}

React frontend:

const season = await myContract.methods.getCurrentSeason().call({from: activeAccount});

After 5 minutes pass, it still returns 0 even if I refresh the page. If I do a transaction (invoke any function in a smart contract), then getCurrentSeason() returns the correct value. What could be the solution?

I am using:

  • web3 ^1.2.6
  • Solidity v0.5.16
  • Ganache (GUI version) v2.1.2
  • Truffle v5.1.9
  • Node v10.15.3
  • Metamask wallet
1

Your problem is the fact that you're using a private network (emulated via Ganache), with no other miner participating. Therefore, now (aka block.timestamp) is never incremented.

Luckily for you, on Ganache you can emulate time via evm_increaseTime, for example:

web3.currentProvider.send({method: "evm_increaseTime", params: [300]});

See the official documentation for more details.

| 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.