0

I'm trying to send Custom ERC20 Token to Owner Wallet Address using transferFrom() function using Web3.js

However all the Transactions are failed. Which is the same problem occur on Remix IDE. Some answers on stackOverflow here says that the approve() is needed to call first before the transferFrom() function. so, I tried on Remix frist but I got the same problem. And then tried with using Web3.js like below.

const myContract = new web3.eth.Contract(abi);
const amount = sendAmount;
let address = myAddress;
myContract.options.address = contractAddress;
myContract.options.from = TokenOwner;
let options = myContract.options;
let data1 = myContract.methods.approve(address, amount).encodeABI();
let data2 = myContract.methods.transferFrom(address, TokenOwner, amount).encodeABI();

const ethAccount = fromPrivateKey(toBuffer("0x..."));
const fromPrivateKeyBuffer = ethAccount.getPrivateKey();

web3.eth.getTransactionCount(TokenOwner, (err, count) => {
  if (err) return;

  const txData = {
    chainId: 0x03,
    gasPrice: web3.utils.toHex(42000000000),
    gasLimit: web3.utils.toHex(90000),
    to: contractAddress,
    from: TokenOwner,
    value: 0x0,
    nonce: web3.utils.toHex(count),
    data: data1
  };
  const tx = new ethTx(txData);
  tx.sign(fromPrivateKeyBuffer);
  const serializedTx = tx.serialize().toString("hex");
  if (!serializedTx) {
    return;
  } else {
    web3.eth.sendSignedTransaction(`0x${serializedTx}`, (err, MuiTXHash) => {
      console.log("err : ", err, "Data1-MuiTXHash : ", MuiTXHash);
      // START DATA2
      web3.eth.getTransactionCount(TokenOwner, (err, count) => {
        if (err) return;

        const txData2 = {
          chainId: 0x03,
          gasPrice: web3.utils.toHex(42000000000),
          gasLimit: web3.utils.toHex(90000),
          to: contarctAddress,
          from: TokenOwner,
          value: 0x0,
          nonce: web3.utils.toHex(count + 1),
          data: data2
        };
        const tx2 = new ethTx(txData2);
        tx2.sign(fromPrivateKeyBuffer);
        const serializedTx2 = tx2.serialize().toString("hex");
        if (!serializedTx2) {
          return;
        } else {
          web3.eth.sendSignedTransaction(`0x${serializedTx2}`, (err, MuiTXHash2) => {
            console.log("err : ", err, "Data2-MuiTXHash : ", MuiTXHash2);
          });
        }
      });
      // END DATA2
    });
  }
});

};

I got the two Transaction Hash return data and the TransferFrom() transaction is failed again. What is the problem? How can I make it success? I have to withdraw the custom ERC20 token from specific address to owner. so I have to use transferFrom() transaction. Please let me know how to do. Thanks.

0
  • txData: TokenOwner approve myAddress spend amount token

=> txData2: myAddress now can transfer from TokenOwner address amount token to any address

let data2 = myContract.methods.transferFrom(TokenOwner, toAddress, amount).encodeABI();

and

txData2 = {
      chainId: 0x03,
      gasPrice: web3.utils.toHex(42000000000),
      gasLimit: web3.utils.toHex(90000),
      to: contarctAddress,
      from: address,
      value: 0x0,
      nonce: web3.utils.toHex(count + 1),
      data: data2
    };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.