gpt4 book ai didi

javascript - 未处理的拒绝错误 : Invalid JSON RPC response: ""

转载 作者:数据小太阳 更新时间:2023-10-29 04:21:09 25 4
gpt4 key购买 nike

我正在尝试在我的 ERC20 代币合约上调用一个方法。我正在连接到“https://rinkeby.infura.io/” ' http提供者。我可以 call() 常量方法,但是当我想通过调用 send() 函数更改契约(Contract)状态时,我得到了这个提到的错误。如果您认为发布 ABI JSON 或 Solidity 合约有帮助,我也可以提供。我认为我的问题纯粹与 web3 相关。我想我需要签署交易(逻辑上),但 web3 文档没有提及任何内容。 http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

这是我遇到的错误:

Unhandled rejection Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/opt/backend/node_modules/web3-core-helpers/src/errors.js:42:16)
at XMLHttpRequest.request.onreadystatechange (/opt/backend/node_modules/web3-providers-http/src/index.js:60:32)
at XMLHttpRequestEventTarget.dispatchEvent (/opt/backend/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/opt/backend/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (/opt/backend/node_modules/xhr2/lib/xhr2.js:509:12)
at IncomingMessage.<anonymous> (/opt/backend/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:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
From previous event:
at PromiEvent (/opt/backend/node_modules/web3-core-promievent/src/index.js:35:24)
at send (/opt/backend/node_modules/web3-core-method/src/index.js:446:21)
at Object._executeMethod (/opt/backend/node_modules/web3-eth-contract/src/index.js:890:24)
at Object.currencyToken.sendSignUpBonousTokens (/opt/backend/server/common/token/currency-token.js:86:56)
at <anonymous>

从调用这个方法:

const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const ABIjson = require('./ABI.json');

const web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/my_access_token_here'));
const contractAddress = '0x31cF32fa91286168B1A896Db4F99d106246932Bc';
const ownerAddress = '0x44Ba8c5a905D634c485dE6f8fD43df9682AfD342';

const token = new web3.eth.Contract(ABIjson, contractAddress);
try
{
const gasAmount = await token.methods.transfer(company.walletAddress, 10000).estimateGas({from: ownerAddress});

token.methods.transfer(company.walletAddress, 550).send(
{
from: ownerAddress,
gasPrice: '20000000000',
gas: gasAmount
})
.then(function(receipt)
{
console.log(receipt);
});
}
catch (error)
{
console.error(error);
}

这是该方法的 ABI 片段:

{  
"constant":false,
"inputs":[
{
"name":"_to",
"type":"address"
},
{
"name":"_value",
"type":"uint256"
}
],
"name":"transfer",
"outputs":[
{
"name":"success",
"type":"bool"
}
],
"payable":true,
"stateMutability":"payable",
"type":"function"
},

最佳答案

@sam k,这里有一个更好更全面的解决方案,可以解决您可能正在寻找的问题。我在给定的代码片段中注意到一件重要的事情,您没有在代码中包含 httpProvider 行,这可能是原因(可能还有其他原因还有原因)。所以请添加下面提到的行。

let contractABI = <your contract ABI>;
let contractInstance = new web3.eth.Contract(contractABI,
contractAddress);
/**The line below needs to be added, specifying the http provider*/
contractInstance.setProvider(new Web3.providers.HttpProvider(URL));

现在是您要“签署”交易以“状态”改变您的契约(Contract)的部分。因此,您可以按照以下代码结构来签署交易。

web3.eth.getTransactionCount(ownerAddress).then( (nonce) => {
let encodedABI =
contractInstance.methods.statechangingfunction(<arguments>).encodeABI();
/**Estimating the required gas automatically from the network itself*/
contractInstance.methods.statechangingfunction(<arguments>).estimateGas({ from: ownerAddress }, (error, gasEstimate) => {
let tx = {
to: contractAddress,
gas: gasEstimate,
data: encodedABI,
nonce: nonce
};
/** Signing the transaction with the "privateKey" of the owner*/
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);});

如果您的其余代码没有问题,则此解决方案应该可行。如果没有,那么我建议您评论指向项目存储库的 github 链接,以便我可以审查并提供解决方案。让我知道这是否有帮助。您可以引用这些官方 web3 链接:signTransaction , sendSignedTransaction , getTransactionCountestimateGas .

关于javascript - 未处理的拒绝错误 : Invalid JSON RPC response: "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48217795/

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