Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

using infura in truffle and get Error: Invalid JSON RPC response: "" #852

Open
dc1996226 opened this issue Mar 17, 2018 · 56 comments · Fixed by trufflesuite/truffle-core#135

Comments

@dc1996226
Copy link

dc1996226 commented Mar 17, 2018

it is my first time to ask questions there ,please forgive my rudeness.

I using metamask, infura, and truffle to try to deploy the contract (the pet-shop-tutorial) to the ropsten testnet, all gone well but this.

that is the error information:

duchendeMacBook-Air:pet-shop-tutorial duchen$ truffle migrate --network ropsten --reset
Using network 'ropsten'.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0x8bf66529dd4eb0c36a89ecec783808effad37ec4bc6dda9f9fe1a7f7cc219f3d
  Migrations: 0xb2ea93671103da9da0cc8018e4a262280e4944f0
Saving successful migration to network...
  ... 0xef35a32e4e13e9615c597527c082bb0f5cc32018bfa7a8ad4a03b9f8af9c59ce
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Replacing Adoption...
  ... 0x4340c9d747dd134da705a6cc192432b0b4b75d0138a01ef21f5c955958a2bb05
  Adoption: 0x83544aff3420b9a1639fb9944449575f6135d915
Saving successful migration to network...
  ... 0x4774ad3d756e26aeaf047b5e19ae449ace957e3baf3578478c70c0aa951fa8e1
Saving artifacts...
/usr/local/lib/node_modules/truffle/build/cli.bundled.js:128857
var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=false;var ENVIRONMENT_IS_WORKER=false;var ENVIRONMENT_IS_NODE=false;var ENVIRONMENT_IS_SHELL=false;if(Module["ENVIRONMENT"]){if(Module["ENVIRONMENT"]==="WEB"){ENVIRONMENT_IS_WEB=true}else if(Module["ENVIRONMENT"]==="WORKER"){ENVIRONMENT_IS_WORKER=true}else if(Module["ENVIRONMENT"]==="NODE"){ENVIRONMENT_IS_NODE=true}else if(Module["ENVIRONMENT"]==="SHELL"){ENVIRONMENT_IS_SHELL=true}else{throw new Error("The provided Module['ENVIRONMENT'] value is not valid. It must be one of: WEB|WORKER|NODE|SHELL.")}}else{ENVIRONMENT_IS_WEB=typeof window==="object";ENVIRONMENT_IS_WORKER=typeof importScripts==="function";ENVIRONMENT_IS_NODE=typeof process==="object"&&"function"==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;ENVIRONMENT_IS_SHELL=!

Error: Invalid JSON RPC response: ""
 at Object.InvalidResponse (/Users/duchen/node_modules/truffle-hdwallet-provider/node_modules/web3/lib/web3/errors.js:35:16)
    at XMLHttpRequest.request.onreadystatechange (/Users/duchen/node_modules/truffle-hdwallet-provider/node_modules/web3/lib/web3/httpprovider.js:115:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/duchen/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/Users/duchen/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (/Users/duchen/node_modules/xhr2/lib/xhr2.js:544:12)
    at ClientRequest.<anonymous> (/Users/duchen/node_modules/xhr2/lib/xhr2.js:414:24)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:387:9)
    at emitOne (events.js:116:13)

and this is my code in truffle.js:

var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat";
module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // for more about customizing your Truffle configuration!
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    },
      ropsten: {
      provider:function(){
      return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/epDGgTgqhFlsPTwmmVC0")
      },
      network_id: 3,
      gas:499000,
      gasPrice:1000000000000
    }
  }
};

I followed the steps from http://truffleframework.com/tutorials/using-infura-custom-provider
but it seemed it has some problems, thank you for your answers.

@cgewecke
Copy link
Contributor

@dc1996226 Thanks for opening. It looks very similar to this issue at truffle-migrate and I think we'll have to investigate further to understand exactly why this is happening.

For what it's worth, I see the final transaction in the migration sequence executed successfully on Ropsten here. Will try to reproduce this this week.

@cgewecke cgewecke self-assigned this Mar 17, 2018
@dstreppa
Copy link

+1

2 similar comments
@draugaslt
Copy link

+1

@atygr
Copy link

atygr commented Apr 17, 2018

+1

@nklipa13
Copy link

+1

@cgewecke
Copy link
Contributor

Just leaving a note here - @draugaslt @nklipa13 @atygr - anyone else adding to this thread. This bug is difficult to reproduce and there's some evidence that it's intermittent (see comment in #894). We'd really like to figure out if this is an infura connectivity issue or a problem with migrations logic. Anything you can add here might be helpful. Examples:

  • it was broken and then somehow started working (plus details!!)
  • it's always broken and never works

@roderik
Copy link
Contributor

roderik commented May 3, 2018

I've been hit by the same issue, but not on Infura.

A project with 4 migration files, all "saving artifacts" work but the last one ends like this:

Saving successful migration to network...
  ... 0x7208b4903b144324a1e14a875f7b63507ff69f9d5c689c9c029b19cac4d9ffe0
Saving artifacts...

.. long time nothing ..

Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/Users/roderik/Development/socialcurrency-dapi/node_modules/web3/lib/web3/errors.js:35:16)
    at XMLHttpRequest.request.onreadystatechange (/Users/roderik/Development/socialcurrency-dapi/node_modules/web3/lib/web3/httpprovider.js:115:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:544:12)
    at ClientRequest.<anonymous> (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:414:24)
    at ClientRequest.emit (events.js:180:13)
    at TLSSocket.socketErrorListener (_http_client.js:395:9)
    at TLSSocket.emit (events.js:180:13)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at process._tickCallback (internal/process/next_tick.js:178:19)
Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/Users/roderik/Development/socialcurrency-dapi/node_modules/web3/lib/web3/errors.js:35:16)
    at XMLHttpRequest.request.onreadystatechange (/Users/roderik/Development/socialcurrency-dapi/node_modules/web3/lib/web3/httpprovider.js:115:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:544:12)
    at ClientRequest.<anonymous> (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:414:24)
    at ClientRequest.emit (events.js:180:13)
    at TLSSocket.socketErrorListener (_http_client.js:395:9)
    at TLSSocket.emit (events.js:180:13)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at process._tickCallback (internal/process/next_tick.js:178:19)
Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/Users/roderik/Development/socialcurrency-dapi/node_modules/web3/lib/web3/errors.js:35:16)
    at XMLHttpRequest.request.onreadystatechange (/Users/roderik/Development/socialcurrency-dapi/node_modules/web3/lib/web3/httpprovider.js:115:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:544:12)
    at ClientRequest.<anonymous> (/Users/roderik/Development/socialcurrency-dapi/node_modules/xhr2/lib/xhr2.js:414:24)
    at ClientRequest.emit (events.js:180:13)
    at TLSSocket.socketErrorListener (_http_client.js:395:9)
    at TLSSocket.emit (events.js:180:13)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at process._tickCallback (internal/process/next_tick.js:178:19)

What is similar to infura is that the node I target is a load balancer in front of 2 parity nodes running a PoA network. (actually, there are 2 networks configured exactly the same, both behave the same way)

In this project it is an "always" error.

@cgewecke
Copy link
Contributor

cgewecke commented May 3, 2018

@roderik It looks like this might be caused by the HDWallet provider continuing to poll the remote client for blocks after the client disconnects. Does that seem like a plausible description for your case?

If so, we now have a simple way of reproducing this behavior and should be able to detect whether or not hdWallet is being used in migrations / close the provider appropriately.

@roderik
Copy link
Contributor

roderik commented May 4, 2018

I am indeed using HDWalletProvider configured like:

    mintnet: {
      provider: () => {
        return new HDWalletProvider(
          process.env.ETHEREUM_DEPLOYER_SEED,
          'https://mintnet.settlemint.com'
        )
      },
      gasPrice: 0x00,
      network_id: '8995',
    },

You are mentioning a client but I'm a bit confused about what you mean by that. HDWalletProvider polls, so it is an HTTP request that gets a response and then closes the connection. The Parity node and/or the loadbalancer do not stop answering, so polling at any time for new blocks should just work.

Going by the error message, usually means that I forgot to turn on ganache when developing, makes me think that in that last phase, it tries to connect to a different network / endpoint.

I also tried the network config without the function, similar result. It still hangs, but I do not get the error messages.

Digging deeper!

@roderik
Copy link
Contributor

roderik commented May 4, 2018

Already a small observation:

I added a console log in HDWalletProvider to log the rpc url used in the constructor.

using provider: new HDWalletProvider(), it cleanly logs the URL once
using provider: () => return new HDWalletProvider(), it logs the url multiple times per migration (2x per artifact.require)

Seems like the () => form is not such a good idea

@cgewecke
Copy link
Contributor

cgewecke commented May 4, 2018

@roderik Ah that's interesting - I was thinking it's possible that Infura occasionally times out or fails to respond because it's under heavy load.

Would you be open to testing a possible fix for this? Have published an experimental build that forces truffle to exit after a command is run and would like to see if that short-circuits this problem. It's available by running:

npm install -g darq-truffle@roderik

darq-truffle migrate  # <-- Example command

NB: truffle develop and truffle console are likely broken by this change - just trying to see if there's a way out the hang. A little more info on darq-truffle at npm here.

@Warrantoken
Copy link

@cgewecke for us the error was removed, but the outcome is the same.

@cgewecke
Copy link
Contributor

cgewecke commented May 5, 2018

@Warrantoken Could you describe the outcome?

Also if you have a second, could you uninstall the roderik build and re-install it? Just published another version of that which replaces the hard exit with timer clearing and might be more viable as a PR.

@roderik
Copy link
Contributor

roderik commented May 5, 2018

@cgewecke as @Warrantoken mentioned, it does not solve the hanging issue.

I've found a way for you to trigger this issue, that will make it a lot easier to try and find the root cause. This repo (a token sale) triggers the bug: https://github.com/DataBrokerDAO/dtx-crowdsale-contracts

You can trigger it by running: (0 gas, 5s blocktime, Parity-based PoA network)

ETHEREUM_DEPLOYER_SEED="help help help help help help help help help help help help" darq-truffle migrate --reset --network mintnet

Explorer: https://explorer.mintnet.settlemint.com/
Netstats: https://stats.mintnet.settlemint.com/

An observation already, if I enable migration 1_ and 2_ it does not hang. If I enable another one (i did 14_ now) it hangs.

@Warrantoken
Copy link

@cgewecke
Well, we are only having this issue deploying to the main net. so its going to get kinda expensive to keep trying it.

here is the contract we tried to deploy, its a slightly modified openzepplin erc20 token with crowdsale:
https://etherscan.io/address/0xcb3b07feb71b10ec70c999a8436ff25ee6ea0a5f

@cgewecke
Copy link
Contributor

cgewecke commented May 5, 2018

@Warrantoken That contract looks like it might be Migrations.sol? Have you successfully deployed to Rinkeby or Ropsten?

@roderik - I will try out Mintnet, looks amazing. One thing that occurs to me is that Parity has a known issue interacting with web3 (see truffle migrate 15) that can be resolved by running the nodes in geth mode. Do you know if that might be relevant?

@cgewecke
Copy link
Contributor

cgewecke commented May 5, 2018

@roderik May have gotten it to work. Have published (yet another) @roderik build that closes sockets and timers on exit. So:

npm uninstall -g darq-truffle
npm install -g darq-truffle@roderik

Then fix the missing await in 2_deploy_contracts.js here. Super impressed with your block explorer and setup btw - this was a joy to debug.

Output

cgewecke$ darq-truffle migrate --reset --network mintnet
Using network 'mintnet'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x51500047ae18688efca1c7ef82887aa6868094ab48c91358861399ce85e8ea8f
  Migrations: 0x0a70c9ab017117be5fa6b6d7b587b89ba548eddd
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Running step...
  Replacing MiniMeTokenFactory...
  ... 0x5314464d47f70aef8ff7a1e08fc345060a5706e1f1f8e9af6a4bf3a45ee9e1d5
  MiniMeTokenFactory: 0x122a0bd157ecf23406ce4851c93b3c23ffa4eeb6
  Replacing MultiSigWalletWithDailyLimit...
  ... 0x25617048235eb8b95769ed2d46c5f2691a0367b7f4a161be32f59bb1c0bf1d95
  MultiSigWalletWithDailyLimit: 0x8bc790a583789367f72c9c59678ff85a00a5e5d0
  Replacing DTXToken...
  ... 0x1dec0ea86f06785e4ad540bac931d6fdb8a7dd732f45c3f02028c5fa5fb7e6c4
  DTXToken: 0x57a474713701d60a1e3c22e1827bf421f4948006
  Replacing TokenSale...
  ... 0xc66a183e36369be5680b47fc90b17fa83510af1a0084c15b1682f5e1cf002773
  TokenSale: 0x026950f443feb8be15bf0a3b878ba1fe23e0ab61
  ... 0xe7d853bb1302f0d71fbef36c41f55d41f078e72dee512fcc8589f8851aa39ec0
Saving artifacts...
Running migration: 3_early_sale_participants.js
  Running step...
Saving artifacts...
Running migration: 4_private_sale_syndicates.js
  Running step...
Saving artifacts...
Running migration: 5_lastcall_private_sales.js
  Running step...
Saving artifacts...
Running migration: 6_initial_private_sales.js
  Running step...
Saving artifacts...
Running migration: 7_cc_private_sales.js
  Running step...
Saving artifacts...
Running migration: 8_coinfalcon.js
  Running step...
Saving artifacts...
Running migration: 9_d10.js
  Running step...
Saving artifacts...
Running migration: 10_ko_private_sale.js
  Running step...
Saving artifacts...
Running migration: 11_fix_dates.js
  Running step...
Saving artifacts...
Running migration: 12_test.js
  Running step...
Saving artifacts...
Running migration: 13_fix_dates.js
  Running step...
Saving artifacts...
Running migration: 14_fixdates2.js
  Running step...
  ... 0xebf6e4ff706373fce43002e04b08d278c037fae99917e46ae8615fa0d2d375c3
1521471600 BigNumber { s: 1, e: 9, c: [ 1521471600 ] }
1524754799 BigNumber { s: 1, e: 9, c: [ 1524754799 ] }
1524783600 BigNumber { s: 1, e: 9, c: [ 1524783600 ] }
1524869999 BigNumber { s: 1, e: 9, c: [ 1524869999 ] }
1524870000 BigNumber { s: 1, e: 9, c: [ 1524870000 ] }
1527375600 BigNumber { s: 1, e: 9, c: [ 1527375600 ] }
Saving artifacts...
Running migration: 15_newdates3.js
  Running step...
Saving artifacts...
Running migration: 16_new_dates.js
  Running step...
Saving artifacts...
Users-MacBook-Air:dtx-crowdsale-contracts cgewecke$

@roderik
Copy link
Contributor

roderik commented May 6, 2018

@cgewecke fixed in all my projects!

And feel free to use mintnet if you want, happy to give something back for all the work! If you really want to mess around, replace mintnet in all URL's to minttestnet, exact same setup, fewer production apps :)

@cgewecke
Copy link
Contributor

cgewecke commented May 6, 2018

@roderik Oh good, and thank you!

@Alonski
Copy link

Alonski commented May 20, 2018

Could this be somehow related? #729

@cgewecke
Copy link
Contributor

cgewecke commented May 20, 2018

@Alonski I don't think so. This is happening in a migrations context using HDWalletProvider and is caused by failing to close the provider correctly as Truffle exits a command. #729 looks like its caused by web3 / XHR2 consuming too many port resources in long running tests that make thousands of server requests.

@Alonski
Copy link

Alonski commented May 20, 2018

@cgewecke ok thanks. I've been following #729 and someone mentioned this one to me as well

@cgewecke
Copy link
Contributor

This should be fixed in 4.1.9. Please ping if you continue see this error.

@TimDaub
Copy link

TimDaub commented Nov 26, 2018

I'm getting this error with darq-truffle@next. After it deploys my smart contracts and wants to save the artifacts, I get the following error:

[test] Error: Invalid JSON RPC response: ""
webpack_1     | [1]     at Object.InvalidResponse (/usr/src/webpack/node_modules/truffle-hdwallet-provider/node_modules/web3/lib/web3/errors.js:35:16)
webpack_1     | [1]     at XMLHttpRequest.request.onreadystatechange (/usr/src/webpack/node_modules/truffle-hdwallet-provider/node_modules/web3/lib/web3/httpprovider.js:115:32)
webpack_1     | [1]     at XMLHttpRequestEventTarget.dispatchEvent (/usr/src/webpack/node_modules/xhr2/lib/xhr2.js:64:18)
webpack_1     | [1]     at XMLHttpRequest._setReadyState (/usr/src/webpack/node_modules/xhr2/lib/xhr2.js:354:12)
webpack_1     | [1]     at XMLHttpRequest._onHttpRequestError (/usr/src/webpack/node_modules/xhr2/lib/xhr2.js:544:12)
webpack_1     | [1]     at ClientRequest.<anonymous> (/usr/src/webpack/node_modules/xhr2/lib/xhr2.js:414:24)
webpack_1     | [1]     at ClientRequest.emit (events.js:182:13)
webpack_1     | [1]     at TLSSocket.socketErrorListener (_http_client.js:399:9)
webpack_1     | [1]     at TLSSocket.emit (events.js:182:13)
webpack_1     | [1]     at emitErrorNT (internal/streams/destroy.js:82:8)
webpack_1     | [1]     at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
webpack_1     | [1]     at process.internalTickCallback (internal/process/next_tick.js:72:19)
webpack_1     | [1] [test] Error: Invalid JSON RPC response: ""
webpack_1     | [1]     at Object.InvalidResponse (/usr/src/webpack/node_modules/truffle-hdwallet-provider/node_modules/web3/lib/web3/errors.js:35:16)
webpack_1     | [1]     at XMLHttpRequest.request.onreadystatechange (/usr/src/webpack/node_modules/truffle-hdwallet-provider/node_modules/web3/lib/web3/httpprovider.js:115:32)
webpack_1     | [1]     at XMLHttpRequestEventTarget.dispatchEvent (/usr/src/webpack/node_modules/xhr2/lib/xhr2.js:64:18)
webpack_1     | [1]     at XMLHttpRequest._setReadyState (/usr/src/webpack/node_modules/xhr2/lib/xhr2.js:354:12)
webpack_1     | [1]     at XMLHttpRequest._onHttpRequestError (/usr/src/webpack/node_modules/xhr2/lib/xhr2.js:544:12)
webpack_1     | [1]     at ClientRequest.<anonymous> (/usr/src/webpack/node_modules/xhr2/lib/xhr2.js:414:24)
webpack_1     | [1]     at ClientRequest.emit (events.js:182:13)
webpack_1     | [1]     at TLSSocket.socketErrorListener (_http_client.js:399:9)
webpack_1     | [1]     at TLSSocket.emit (events.js:182:13)
webpack_1     | [1]     at emitErrorNT (internal/streams/destroy.js:82:8)
webpack_1     | [1]     at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
webpack_1     | [1]     at process.internalTickCallback (internal/process/next_tick.js:72:19)

I tried with truffle-hdwallet-provider 0.0.7-beta.0. Doesn't work.

@gnidan
Copy link
Contributor

gnidan commented Jan 16, 2019

Is this still a problem in the latest version of Truffle?

@t4sk
Copy link

t4sk commented Jan 21, 2019

@gnidan
I had this issue on

Truffle v5.0.2 (core: 5.0.2)
Solidity - 0.4.24 (solc-js)
Node v11.7.0

I was able to fix this problem by changing provider in truffle-confing.js from

provider: function () {
   return new HDWalletProvider(...)
}

to

provider: new HDWalletProvider(....)

Don't know why this works...

feamcor added a commit to dev-bootcamp-2019/final-project-feamcor that referenced this issue Jan 23, 2019
feamcor added a commit to dev-bootcamp-2019/final-project-feamcor that referenced this issue Jan 23, 2019
@EvanTedesco
Copy link

EvanTedesco commented Feb 11, 2019

I am also seeing this with the following versions:
Truffle v5.0.2 (core: 5.0.2)
Solidity v0.5.0 (solc-js)
Node v10.14.1

I tried @t4sk 's solution which allowed me to push to Ropesten but My test suite still won't run.

I am still getting:

Invalid JSON RPC response: "WebSockets request was expected\r\n"

I was able to run tests locally by adding the following network configuration in truffle.js:

test: { host: "127.0.0.1", port: 9545, network_id: '*', }

then running my tests using

truffle test --network test

@eggplantzzz
Copy link
Contributor

@EvanTedesco Is your code available anywhere to look at?

@sushanth3366
Copy link

#package.json

"name": "inbox",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC",
"dependencies": {
"ganache-cli": "^6.3.0",
"mocha": "^4.1.0",
"solc": "^0.4.17",
"truffle-hdwallet-provider": "^1.0.4",
"web3": "^1.0.0-beta.36"
}
}

#deploy.js

const HDWalletProvider = require('truffle-hdwallet-provider');//HDWallet says which account we wnat to unlock and which account to use as source of ether and which account to be unlockedfor deploying our constract and what outside node we are gng to connect to
const Web3=require('web3');
const{interface,bytecode}=require('./compile');
const provider=new HDWalletProvider(
'uniform parrot race entry worry saddle during buyer source wait invest slight','https://rinkeby.infura.io/v3/de97edd91145471199555841549fb3bc'
);
const web3=new Web3(provider);//we can use web3 instance to interact with test network we can send eth,deploy contract etcv

// reason we are writing function so that we can avoid promises and use async await
const deploy=async ()=>{
const accounts= await web3.eth.getAccounts();
console.log('attempting to deploy from account',accounts[0]);
const result=await new web3.eth.Contract(JSON.parse(interface))
.deploy({data:'0x'+ bytecode,arguments:['hi! there']})
.send({gas:'1000000',from:accounts[0]});

console.log('contract deployed at',result.options.address);//to know where our contract is deployed

};
deploy();

#json rpc error

kuras-MBP:inbox kurasushanthkumarreddy$ node deploy.js
attempting to deploy from account 0xAe458357763F80a7aAa72F80339911D9Bf48320b
contract deployed at 0x6d31980F771424AaDE60CfAeB961FdD5CF333f78
Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:625357)
at e.i.onreadystatechange (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:775475)
at e.t.dispatchEvent (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:1:152983)
at e._setReadyState (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:780284)
at e._onHttpRequestError (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:783952)
at ClientRequest. (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:781449)
at ClientRequest.emit (events.js:189:13)
at TLSSocket.socketErrorListener (_http_client.js:392:9)
at TLSSocket.emit (events.js:189:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:625357)
at e.i.onreadystatechange (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:775475)
at e.t.dispatchEvent (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:1:152983)
at e._setReadyState (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:780284)
at e._onHttpRequestError (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:783952)
at ClientRequest. (/Users/kurasushanthkumarreddy/inbox/node_modules/truffle-hdwallet-provider/dist/index.js:15:781449)
at ClientRequest.emit (events.js:189:13)
at TLSSocket.socketErrorListener (_http_client.js:392:9)
at TLSSocket.emit (events.js:189:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)

@sushanth3366
Copy link

sushanth3366 commented Feb 21, 2019

can anyone help me resolving this issue please??

@eggplantzzz
Copy link
Contributor

eggplantzzz commented Feb 21, 2019

@sushanth3366 Is your code available somewhere on github so I can take a look? The above is very difficult to read. Also, I want to look at your truffle config.

@sushanth3366
Copy link

@eggplantzzz i am new to this stuff can u tell me where the truffle config is available??

@sushanth3366
Copy link

sushanth3366 commented Feb 21, 2019 via email

@eggplantzzz
Copy link
Contributor

@sushanth3366 It is the file in the root directory of a truffle project named truffle-config.js (it also could be named truffle.js). I'm not sure it looks like deploy.js is set up correctly here...are you using truffle or are you just usingtruffle-hdwallet-provider?

@CruzMolina
Copy link
Contributor

@sushanth3366 , hopefully you were able to figure this out, but if not can you try setting the provider?

Example:

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const {interface, bytecode} = require('./compile');
const provider = new HDWalletProvider('uniform parrot race entry worry saddle during buyer source wait invest slight', 'https://rinkeby.infura.io/v3/de97edd91145471199555841549fb3bc');
const web3 = new Web3(provider);
web3.setProvider(provider); // <=======================

const deploy = async () => {
const accounts = await web3.eth.getAccounts();
console.log('attempting to deploy from account',accounts[0]);
const result = await new web3.eth.Contract(JSON.parse(interface))
  .deploy({ data:'0x'+ bytecode,arguments:['hi! there'] })
  .send({ gas:'1000000',from:accounts[0]} );

console.log('contract deployed at', result.options.address);
};
deploy();

@princesinha19
Copy link
Contributor

Getting the same error. I am using infura.

truffle-config.js

const HDWalletProvider = require('truffle-hdwallet-provider');
// const infuraKey = "";

// const fs = require('fs');
// const mnemonic = fs.readFileSync(".secret").toString().trim();

const mnemonic = config.mnemonic;


ropsten: {
      provider: () => new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/" + config.infuraProjectId),
      network_id: 3,       // Ropsten's id
      gas: 1500000,        // Ropsten has a lower block limit than mainnet
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    }

Error Log

> truffle migrate --reset all --network ropsten

Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/home/prince/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/cruzmolina/Code/truffle-projects/truffle/node_modules/web3/node_modules/web3-core-helpers/src/errors.js:42:1)
    at e.InvalidResponse [as onreadystatechange] (/home/prince/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/cruzmolina/Code/truffle-projects/truffle/node_modules/web3/node_modules/web3-providers-http/src/index.js:92:1)
    at e.call [as dispatchEvent] (/home/prince/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/cruzmolina/Code/truffle-projects/truffle/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)
    at e.dispatchEvent [as _setReadyState] (/home/prince/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/cruzmolina/Code/truffle-projects/truffle/node_modules/xhr2-cookies/dist/xml-http-request.js:208:1)
    at e._setReadyState [as _onHttpRequestError] (/home/prince/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/cruzmolina/Code/truffle-projects/truffle/node_modules/xhr2-cookies/dist/xml-http-request.js:349:1)
    at ClientRequest._onHttpRequestError (/home/prince/node_modules/truffle-hdwallet-provider/dist/webpack:/truffle-hdwallet-provider/Users/cruzmolina/Code/truffle-projects/truffle/node_modules/xhr2-cookies/dist/xml-http-request.js:252:47)
    at ClientRequest.emit (events.js:182:13)
    at TLSSocket.socketErrorListener (_http_client.js:391:9)
    at TLSSocket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Truffle v5.0.0 (core: 5.0.0)
Node v10.13.0

@eggplantzzz
Copy link
Contributor

@princesinha19 Is config defined in the case above? I don't see it imported or defined anywhere. That would mess up your url.

@princesinha19
Copy link
Contributor

Thanks @eggplantzzz.
Yes, It's declared. I didn't included it here. I have imported a file where configuration are there.

@eggplantzzz
Copy link
Contributor

@princesinha19 Can you also upgrade to the latest version of Truffle to ensure that this is not some problem that has been patched already?

Let me know how it goes. If the problem persists I will try and duplicate your setup to see if I can replicate your problem.

@t4sk
Copy link

t4sk commented Jul 28, 2019

@princesinha19 I was getting the same error. My previous solution still worked on Ropsten. Here is my truffle-config.js

require("dotenv").config()
const HDWalletProvider = require("truffle-hdwallet-provider")

module.exports = {
  networks: {
    ropsten: {
      provider: new HDWalletProvider(
        process.env.HD_WALLET_MNEMONIC,
        `https://ropsten.infura.io/v3/${process.env.INFURA_API_KEY}`
      ),
      network_id: 3, // Ropsten's id
      gas: 5500000, // Ropsten has a lower block limit than mainnet
      confirmations: 2, // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 50, // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true, // Skip dry run before migrations? (default: false for public nets )
    },
  },

  mocha: {
    // timeout: 100000
  },

  compilers: {
    solc: {
      version: "0.5.10", // Fetch exact version from solc-bin (default: truffle's version)
      docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
      settings: {
        // See the solidity docs for advice about optimization and evmVersion
        optimizer: {
          enabled: false,
          runs: 200,
        },
        evmVersion: "byzantium",
      },
    },
  },
}

@levilliard
Copy link

@gnidan
I had this issue on

Truffle v5.0.2 (core: 5.0.2)
Solidity - 0.4.24 (solc-js)
Node v11.7.0

I was able to fix this problem by changing provider in truffle-confing.js from

provider: function () {
   return new HDWalletProvider(...)
}

to

provider: new HDWalletProvider(....)

Don't know why this works...

Works for me. Thanks !

@Leon-Africa
Copy link

Leon-Africa commented Dec 14, 2019

rm node_modules/
rm package.json
rm package-lock.json

Create an empty package.json file like this:

{
  "dependencies": { 
  }
}

Update node and npm
Ensure latest stable version node installed - currently 12.13.1

The update npm:

npm install -g npm

(restart terminal)

truffle-hdwallet-provider is deprecated:
https://www.npmjs.com/package/truffle-hdwallet-provider

Instead use @truffle/hdwallet-provider:
https://www.npmjs.com/package/@truffle/hdwallet-provider

(while in your project dir containing the package.json):

npm install @truffle/hdwallet-provider --save

In your truffle-config.js:

var HDWalletProvider = require("@truffle/hdwallet-provider");

Then:

truffle migrate --network rinkeby

@robertmagier
Copy link
Contributor

robertmagier commented Mar 17, 2020

g provider: new HDWalletProvider(), it cleanly logs the URL once
using provider: () => return new HDWalletProvider(), it logs the url multiple times per migration (2x per artifact.require)

Seems like the () => form is not such a good idea

In my case @roderik sollution worked perfectly. It solved my problem.
I had an issue on ropsten via Infura.

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

Successfully merging a pull request may close this issue.