Blockchain 以太坊Web3.js无效的JSON RPC响应:“&引用;
我正在为以太坊使用web3.js模块。在执行事务时,我得到错误响应 错误:Blockchain 以太坊Web3.js无效的JSON RPC响应:“&引用;,blockchain,ethereum,solidity,Blockchain,Ethereum,Solidity,我正在为以太坊使用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/
"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)"
当我调用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”
使用web3.js时,您应该签署交易记录。当您调用非常量函数(如transfer)时,您应该对事务进行签名,然后发送已签名的事务(有一个名为sendSignedTransaction的方法)。使用web3js很难做到这一点,我建议使用ehtersjs,使用它一切都会简单得多。要补充maptuhec所说的,在Web3中调用“状态更改”函数或状态更改事务时,必须对其进行签名
下面是一个示例,当您试图调用一个公共函数(甚至是一个公共契约变量)时,它只读取(或“查看”并从智能契约返回一个值,而不更改其状态,在这种情况下,我们不必指定一个事务主体,然后将其作为事务进行签名,因为它不会改变我们合同的状态
constructance.methods.aPublicFunctionOrVariableName().call()。然后((结果)=>{console.log(结果);})
@Anchal您是对的。更改状态的函数需要签名。谢谢像web3js这样的以太坊的ethersjs官方模块?是的,这里有一个指向的链接,这是一个非常好的库。还有一个我听说也不错,但从未使用过的,叫做ethereal js。
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)
})
})