Explore Courses Blog Tutorials Interview Questions
0 votes
in Blockchain by (4.1k points)

I am using web3.js module for Ethereum. While executing a transaction I am getting error response.


"Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/home/akshay/WS/ethereum/node_modules/web3-core-helpers/src/errors.js:42:16)
    at XMLHttpRequest.request.onreadystatechange (/home/akshay/WS/ethereum/node_modules/web3-providers-http/src/index.js:73:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/akshay/WS/ethereum/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/akshay/WS/ethereum/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/home/akshay/WS/ethereum/node_modules/xhr2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/home/akshay/WS/ethereum/node_modules/xhr2/lib/xhr2.js:469:24)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)"

I am using the Ropsten test network URL for testing my smart contract:

When I call the balanceOf function, it works fine but when I try to call function transfer it send me this error. The code is mentioned below:'/transfer', (req, res, next)=>{
  contractInstance.methods.transfer(req.body.address, req.body.amount).send({from:ownerAccountAddress})
  }).on('confirmation',(confirmationNumber, receipt)=>{
  }).on('receipt', (receipt)=>{

Please let me know where I am wrong.

EDIT: I am using web3js version "web3": "^1.0.0-beta.34"

1 Answer

0 votes
by (14.4k points)

While calling a "state-changing" function in Web3 or a state-changing transaction, it must be signed!

Below is an example of when you're trying to call a public function which is only reading and returning a value from the smart contract. The function is not changing the state of the smart contract. In this case, we don't need to necessarily specify a transaction body and then sign it as a transaction.

contractInstance.methods.aPublicFunctionOrVariableName().call().then( (result) => {console.log(result);})

Consider the example below. Over here, we're trying to invoke a "state-changing" function and hence we'll be specifying a proper transaction structure for it.

web3.eth.getTransactionCount(functioncalleraddress).then( (nonce) => {

        let encodedABI = contractInstance.methods.statechangingfunction().encodeABI();

 contractInstance.methods.statechangingfunction().estimateGas({ from: calleraddress }, (error, gasEstimate) => {

          let tx = {

            to: contractAddress,

            gas: gasEstimate,

            data: encodedABI,

            nonce: nonce


          web3.eth.accounts.signTransaction(tx, privateKey, (err, resp) => {

            if (resp == null) {console.log("Error!");

            } else {

              let tran = web3.eth.sendSignedTransaction(resp.rawTransaction);

              tran.on('transactionHash', (txhash) => {console.log("Tx Hash: "+ txhash);});

Browse Categories