0
votes

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.

3

3 Answers

0
votes

I guess the compiling on Remix had some errors so I created new solidity file with same code on it and deployed it. The new smart contract worked well and no errors occurred. The errors was not about my code. If someone's suffering like this problems, then create a new contract address and I don't recommend using 'At Address' on Remix. This is not work properly at all.

0
votes

I was tried appove() + transferFrom () , allowance() on Solidity working well but on web3 not working error same as you

0
votes

I guess the transferFrom function should be called by the spender (i.e the address to which the TokenOwner has approved to spend). So,

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

And don't forget to use spender's privateKey to sign the txn and spender's address in getTransactionCount Read Here : Ethereum Wiki Hope it helps. :)