get Error: Returned Error: Must be authenticated!,同时使用web3.j


const fs = require('fs');
let web3 = new Web3(new Web3.providers.HttpProvider('http:127.0.0.1'))
const abi = fs.readFileSync('erc20_abi.json', 'utf-8')
const contractAddress = '0xB4...'
const privateKey = '...'
let contract = new web3.eth.Contract(JSON.parse(abi), contractAddress);
let transfer = contract.methods.transfer("0xd...", 10);
let encodedABI = transfer.encodeABI();

var tx = {
from: "0xF...",
to: contractAddress,
gas: 2000000,
data: encodedABI
}; 
web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
var tran = web3.eth.sendSignedTransaction(signed.rawTransaction);
tran.on('confirmation', (confirmationNumber, receipt) => {
console.log('confirmation: ' + confirmationNumber);
});
tran.on('transactionHash', hash => {
console.log('hash');
console.log(hash);
});
tran.on('receipt', receipt => {
console.log('reciept');
console.log(receipt);
});
tran.on('error', console.error);
});

我正在使用上面的代码从帐户发送ERC20令牌到另一个帐户,它抛出这个错误

UnhandledPromiseRejectionWarning: Error: Returned Error: Must be authenticated

我已经弄清楚我使用错误的rpc提供程序,改变rpc提供程序修复此问题。

相关内容

最新更新