openzeppelin-test-helpers
openzeppelin-test-helpers copied to clipboard
Working with time using hardhat returns Error: Invalid JSON RPC response: ""
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;
same with truffle
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
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
The issue seems to be because we are using ethers.js instead of web3.js
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.
I am having the same problem for hardhat testing environment without Ganache. How should I fix this ?