Vishal Tyagi
Vishal Tyagi

Reputation: 187

Getting Error: Returned error: Must be authenticated! , while sending erc20 code using web3.js

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);
  });

I am using the above code to send ERC20 tokens from account to another account, Its throwing this error

UnhandledPromiseRejectionWarning: Error: Returned error: Must be authenticated

Upvotes: 0

Views: 401

Answers (1)

Vishal Tyagi
Vishal Tyagi

Reputation: 187

I have figured it out I am using wrong rpc provider, changing the Rpc provider fixes this issue.

Upvotes: 2

Related Questions