Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ethereum Web3.js Invalid JSON RPC response: ""

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

Error:

"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 ropsten test network url for testing my smart contract:

https://ropsten.infura.io/API_KEY_HERE

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:

router.post('/transfer', (req, res, next)=>{
  contractInstance.methods.transfer(req.body.address, req.body.amount).send({from:ownerAccountAddress})
  .on('transactionHash',(hash)=>{
console.log(hash)
  }).on('confirmation',(confirmationNumber, receipt)=>{
    console.log(confirmationNumber)
    console.log(receipt)
  }).on('receipt', (receipt)=>{
    console.log(receipt)
  }).on('error',(err)=>{
    console.log(err)
  })
})

Please let me know where I am wrong.

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

like image 907
Akshay Sood Avatar asked Oct 18 '25 18:10

Akshay Sood


1 Answers

To add to what maptuhec said, 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 (or even a public contract variable), which is only reading (or "view"ing) and returning a value from your smart contract and NOT changing its state, in this we don't need to necessarily specify a transaction body and then sign it as a transaction, because it doesn't change the state of our contract.

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

**

State-Changing Transactions

**

Now, consider the example below, 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);});

For more on signTransaction, sendSignedTransaction, getTransactionCount and estimateGas

like image 139
Achal Singh Avatar answered Oct 20 '25 20:10

Achal Singh