gpt4 book ai didi

blockchain - Ethereum Web3.js 无效的 JSON RPC 响应 : ""

转载 作者:行者123 更新时间:2023-12-01 11:15:54 24 4
gpt4 key购买 nike

我正在为以太坊使用 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"

最佳答案

补充一下 maptuhec 所说的,在 Web3 中调用“状态更改”函数或状态更改事务时,必须签名!

以下是您尝试调用公共(public)函数(甚至是公共(public)合约变量)时的示例,该函数仅读取(或“查看”)并从您的智能合约返回一个值而不更改其状态,在我们不需要指定一个交易主体然后将其作为交易签名,因为它不会改变我们合约的状态。

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


**

State-Changing Transactions



**

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

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 , getTransactionCountestimateGas

关于blockchain - Ethereum Web3.js 无效的 JSON RPC 响应 : "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51355259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com