Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with time using hardhat returns Error: Invalid JSON RPC response: "" #179

Open
invissiblecat opened this issue Jan 11, 2022 · 6 comments

Comments

@invissiblecat
Copy link

I asked about this issue at stackoverflow, but didn't get an answer. As I am not sure, if there is a problem from my side or a real issue, I guess it is okay to ask here (tell me if it is wrong).

Following the examples, I try to increase time in my test and keep getting an error: Invalid JSON RPC response: ""

Test with given error:

it("should revert claim drawing with 'Android: bad state'", async () => {

        const [owner, signer1] = await ethers.getSigners();

        let duration = time.duration.seconds(3);

        await time.increase(duration);

        await android.claimPainting(1);

        await truffleAssert.reverts(
		android.claimPainting(1),
		'Android: bad state'
	 );
    });

Error message:

11) Start drawing
       should revert claim drawing with 'Android: bad state':
     Error: Invalid JSON RPC response: ""
      at Object.InvalidResponse (node_modules/web3-core-helpers/lib/errors.js:43:16)
      at XMLHttpRequest.request.onreadystatechange (node_modules/web3-providers-http/lib/index.js:95:32)
      at XMLHttpRequestEventTarget.dispatchEvent (node_modules/xhr2-cookies/xml-http-request-event-target.ts:44:13)
      at XMLHttpRequest._setReadyState (node_modules/xhr2-cookies/xml-http-request.ts:219:8)
      at XMLHttpRequest._onHttpRequestError (node_modules/xhr2-cookies/xml-http-request.ts:379:8)
      at ClientRequest.<anonymous> (node_modules/xhr2-cookies/xml-http-request.ts:266:37)
      at ClientRequest.emit (node:events:390:28)
      at ClientRequest.emit (node:domain:475:12)
      at Socket.socketErrorListener (node:_http_client:447:9)
      at Socket.emit (node:events:390:28)
      at Socket.emit (node:domain:475:12)
      at emitErrorNT (node:internal/streams/destroy:157:8)
      at emitErrorCloseNT (node:internal/streams/destroy:122:3)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)

Hardhat config file:

import * as dotenv from "dotenv";

import { HardhatUserConfig, task } from "hardhat/config";
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";
import "@openzeppelin/test-helpers";
const fs = require("fs");

dotenv.config();

const { apiKey, privateKey } = JSON.parse(fs.readFileSync("./keys.json", 'utf8'));

task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

const config: HardhatUserConfig = {
  solidity: {
    version: "0.8.4",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
  networks: {
    ropsten: {
      url: process.env.ROPSTEN_URL || "",
      accounts:
        process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
    },
    rinkeby: {
      url: `https://eth-rinkeby.alchemyapi.io/v2/${apiKey}`,
      // gasPrice: 225000000000,
      accounts: [
        `${privateKey}`
      ],
    },
  },
  gasReporter: {
    enabled: process.env.REPORT_GAS !== undefined,
    currency: "USD",
  },
  etherscan: {
    apiKey: process.env.ETHERSCAN_API_KEY,
  },
};

export default config;

@dalechyn
Copy link

dalechyn commented Feb 8, 2022

same with truffle

@zikyfranky
Copy link

Same issue with truffle

Mine was working perfectly, then I moved the file (deleting the node_modules) folder, and after installing dependencies with yarn and npm, I get the invalid JSON Response.

Nothing's changed except the node_modules

@zikyfranky
Copy link

zikyfranky commented Feb 17, 2022

After digging through the libraries, I realized the error is raised when a wrong HTTP provider rpc URL is passed, so I dug deeper, and in @openzeppelin/test-helpers/src/config/web3.js there's a setDefaultWeb3Provider and apparently, this function isn't detecting the truffle injected web3, thus making use of the default http://localhost:8545 rpc URL which isn't running.

I'll update this if I figure a way to pass down the injected web3 object to @OpenZeppelin

[UPDATE]
I couldn't find a fix for this, I had to switch from truffle to hardhat.

Make sure to install @nomiclabs/hardhat-web3 and import it in your hardhat config, details here

@marc-aurele-besner
Copy link

The issue seems to be because we are using ethers.js instead of web3.js

@russanto
Copy link

Same issue with Truffle+Web3js+Ganche and setting the provider with

require('@openzeppelin/test-helpers/configure')({
    provider: 'http://127.0.0.1:7545',
});

solved the problem for me, if it can help.

@ferdasonmez
Copy link

I am having the same problem for hardhat testing environment without Ganache. How should I fix this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants