当我使用web3js接口通过infura节点将交易发送到ethreum testnet rinkeby时,遇到了错误“错误:无效的JSON RPC响应:””。这是我的源代码:
let Web3 = require("web3");
if(typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/2f85c22a29994320b52da33bec96968d"));
}
let abi = "/abi/"
let address = "";
let contract = new web3.eth.Contract(abi, address);
contract.methods.set("JJJJJJJJJ").send({from: '0x1062024529684b1890b2fa5964334d8db7da2512'},function(err, txHash) {
if(err) {
console.log("err: " + err);
} else {
console.log(txHash);
}
});
答案 0 :(得分:0)
通常,web3初始化需要花费几分钟,因此请使用await,async方法进行web3初始化。
例如:
async function abc() {
web3 = await new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/2f85c22a29994320b52da33bec96968d"));
}