代码之家  ›  专栏  ›  技术社区  ›  Akshay Sood

以太坊Web3.js无效的JSON RPC响应:“”

  •  3
  • Akshay Sood  · 技术社区  · 7 年前

    我正在为以太坊使用web3.js模块。执行事务时,我收到错误响应。

    错误:

    "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)"
    

    我正在使用Ropsten测试网络URL测试我的智能合约:

    https://ropsten.infura.io/API_KEY_HERE
    

    当我打电话给 balanceOf 函数,它可以正常工作,但当我尝试调用函数时 transfer 它把这个错误发送给我。代码如下:

    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)
      })
    })
    

    请告诉我哪里错了。

    编辑:我正在使用Web3JS版本 "web3": "^1.0.0-beta.34"

    2 回复  |  直到 7 年前
        1
  •  4
  •   Pang Ajmal PraveeN    7 年前

    要添加Maptuhec所说的内容,在Web3中调用“状态更改”函数或状态更改事务时,必须对其进行签名!

    下面是一个例子,当你试图调用一个公共函数(甚至是一个公共契约变量),它只读取(或“查看”ing)并从你的智能合约返回一个值,而不改变它的状态,在这种情况下,我们不需要指定一个事务体,然后签名它。作为交易,因为它不会改变我们合同的状态。

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

    **

    状态更改事务

    **

    现在,考虑下面的示例,这里我们试图调用一个“状态更改”函数,因此我们将为它指定一个适当的事务结构。

    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);});

    关于更多信息 signTransaction , sendSignedTransaction , getTransactionCount estimateGas

        2
  •  1
  •   maptuhec    7 年前

    使用web3.js时,您应该签署事务。当调用非常量函数(如transfer)时,应在事务上签名,然后发送签名的事务(有一个方法称为sendsignedTransaction)。使用Web3JS非常困难,我建议使用ehtersjs,使用它,一切都会轻松很多。