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

Error: Invalid JSON RPC response: "" (create address locally and deploy contract) #164

Closed
justcodify opened this issue Nov 24, 2018 · 3 comments

Comments

@justcodify
Copy link

justcodify commented Nov 24, 2018

I have use case where when user sign up to my app, i create address and assign to him then need to store some info in contract & deploy to public ethereum. one user has single address but multiple contracts to be deployed.

this is my code: I am testing through command prompt "node test.js"

var Web3 = require("web3");
var url = 'https://ropsten.infura.io/v3/{key}';
var web3 = new Web3(new Web3.providers.HttpProvider(url));
var address = '0xxxxxxxxxxxxxxxxxxxxxxxx';
web3.eth.defaultAccount = address;
const fs = require("fs");
const solc = require("solc");
const input = fs.readFileSync("./CertContract.sol");
var output  = solc.compile(input.toString(),1);

var contract = output.contracts[":CertContract"];
var byteCode = contract.bytecode;
var abi = JSON.parse(contract.interface);
const certContract = new web3.eth.Contract(abi, address);
var auth = new web3.eth.Contract(abi, address);
auth.deploy({data: "0x"+byteCode, arguments: [1,'1']})
    .send({
        from: address,
        gas:1000000
    }, function(error, transactionHash){ 
            console.log(error);
        console.log(transactionHash);
    });

I getting an error:

Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (E:\Projects\ethereum\node_modules\web3-core-helpers\src\errors.js:42:16)
at XMLHttpRequest.request.onreadystatechange (E:\Projects\ethereum\node_modules\web3-providers-http\src\index.js:87:32)
at XMLHttpRequestEventTarget.dispatchEvent (E:\Projects\ethereum\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
at XMLHttpRequest._setReadyState (E:\Projects\ethereum\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
at XMLHttpRequest._onHttpResponseEnd (E:\Projects\ethereum\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
at IncomingMessage. (E:\Projects\ethereum\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
at IncomingMessage.emit (events.js:185:15)
at endReadableNT (_stream_readable.js:1101:12)
at process._tickCallback (internal/process/next_tick.js:114:19)

@JoshuaQYH
Copy link

    .send({
        from: address,
        gas: '1000000'  ------ here try to use the string.
    }, function(error, transactionHash){ 
            console.log(error);
        console.log(transactionHash);
    });

Getting the "" response means the server probably dropped the connection unexpectedly. Sometimes, you should check if your network is ok, or check the format of your data.

@roschler
Copy link

@justcodify See this related thread:

#78

The suggestion @JoshuaQYH is one that affected me in a previous run-in with this error. Make sure that all the gas related values you pass to any Web3JS calls are converted to BigNumbers using the web-utils toBN() method.

Unfortunately I have this error once again but the above suggestion did solve one occurrence of that erro for me about a month ago.

@justcodify
Copy link
Author

justcodify commented Dec 28, 2018

I found the problem and solved by adding my HD Wallet account using private key. infura only provides interface.

const path = require('path');
var Web3 = require("web3");
const solc = require("solc");
var provider = new Web3.providers.HttpProvider("infura url");
var web3 = new Web3(provider);
const input = fs.readFileSync(__dirname + path.sep + "CertitudeContract.sol");
var output = solc.compile(input.toString(), 1);
var contract = output.contracts[":NameOfContract"];
var byteCode = contract.bytecode;
var abi = JSON.parse(contract.interface);
var acct = web3.eth.accounts.privateKeyToAccount("private key of your wallet account");
web3.eth.accounts.wallet.add(acct);
web3.eth.defaultAccount = acct.address;
var auth = new web3.eth.Contract(abi, acct.address);
auth.deploy({
        data: "0x" + byteCode, arguments: [  ] // contract's constructor arguments
    }).send({
            from: acct.address,
            gas: 1000000 // it should be lil high
        }, (e, transactionHash) => {
            if (e) {
                console.log("In Error e");
                console.log(e);
                // here u can reutrn error.
            }
            else {
                console.log(transactionHash);
            }
        }).then(async function (obj) {
            console.log(obj.options.address); //this will be address of created smart contract
     });

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

3 participants