0

Why am I getting an error when doing a gas estimation on infura. I am using web3 v1.0 and here is my code:

let Web3 = require('web3'); 
var web3 = new Web3(new Web3.providers.HttpProvider('http://ropsten.infura.io/key'));

const account = '0x5561E1e11199317E2186.....';
const contractAddress = '0x72A0411ee2f3.....'; 

var tokenizeContract = new web3.eth.Contract( [ ...abi... ], '0x72A0411ee2f328E34AB8DE74e51820bC257ccDE5' ); 

const contractFunction = tokenizeContract.methods.setOrder(web3.utils.toHex('location'), web3.utils.toHex('productName'), web3.utils.toHex('customer'), 3000, 2, 6000, web3.utils.toHex('timestamp'));

contractFunction.estimateGas(function(error, gasAmount) {
    if(!error) {
        console.log(gasAmount);
    } 
    else {
        console.log(error);
    }
});

This is the error:

Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/home/user/code/project_poc/node_modules/web3-core-helpers/src/errors.js:42:16)
    at XMLHttpRequest.request.onreadystatechange (/home/user/code/project_poc/node_modules/web3-providers-http/src/index.js:73:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/user/code/project_poc/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/user/code/project_poc/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (/home/user/code/project_poc/node_modules/xhr2/lib/xhr2.js:544:12)
    at ClientRequest.<anonymous> (/home/user/code/project_poc/node_modules/xhr2/lib/xhr2.js:414:24)
    at ClientRequest.emit (events.js:180:13)
    at Socket.socketErrorListener (_http_client.js:395:9)
    at Socket.emit (events.js:180:13)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at process._tickCallback (internal/process/next_tick.js:178:19)
  • Why do you convert to hex all your param? You don't have to do this. – Florian Castelain Jun 13 '18 at 15:31
  • Because the variables are defined as bytes32 in the smart contract. There is no way to use string type in solidity or is there a way? – saitam Jun 13 '18 at 15:53
  • You of course can use strings. Why not? You will just have limitations, be you can Oo – Florian Castelain Jun 13 '18 at 17:35
0

The solution in my case is using https instead of http:

var web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/key'));

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.