0

I want to implement a send ERC20 token function in my crypto wallet, and this is the function that I found on the internet shown as below.

 sendToken()
  {
    let toAddress = "0x5077174D79d9491AF15Dcf7D1496638D6062A011";
    this.tokenContractInstance.transfer(toAddress,123, (error, result) => {
      if (error == null) {
        console.log(result)

      } else {
        console.log('Transfer error' + error)
      }
    })
  }

Unfortunately, no luck for me with error message

"Invalid Message"

May I know why is failed? Also, I have seen others example as well to send ERC20 token by generating the rawTransaction with the privateKey sign function. Which approach should I go with?

TylerH
  • 20,799
  • 66
  • 75
  • 101
David
  • 607
  • 1
  • 6
  • 19

1 Answers1

0

Assuming web3 1.0, this is the correct way:

this.tokenContractInstance.methods.transfer(toAddress, 123).send({from: txSenderAddress}, (error, transactionHash) => {
    ...
});
TylerH
  • 20,799
  • 66
  • 75
  • 101
Ferit
  • 8,692
  • 8
  • 34
  • 59
  • I use the method above and I got an error message ("Invalid Address") – David Jul 24 '19 at 01:29
  • @ferit- Instead, of this.tokenContractInstance.methods.transfer, I changed to this this.tokenContractInstance.transfer. The first method doesn't works for me, it says, the transfer function not found. – David Jul 24 '19 at 01:30
  • @ferit- Why is that your code above shown as methods.transfer? Isn't it the transfer function straight from the contract instance? Maybe, worth to mention here, I'm using the web3 version 0.19.1. – David Jul 24 '19 at 01:37
  • I'm just wondering, the sender address is not the original ERC20 owner account. The address is the second transferred account from the original owner, in this case, wondering do I need to get the owner's approval to spend the token or transfer? – David Jul 24 '19 at 02:40
  • Yeah @DavidB I was assuming you are using web3 1.0. I recommend that you use too, because web3 0.x is too old. – Ferit Jul 24 '19 at 02:57
  • @ferit- I agree that the version 0.19.x is old. But, consider that the entire project framework design and dependencies, we cannot afford to make the change now. Could you advise on the version 0.19.x? – David Jul 24 '19 at 03:42
  • In this case, I suppose, to construct a RawTransaction message to send the ERC20 token will be ideal? – David Jul 24 '19 at 04:11
  • @ferit- Even I try on the web3 version 1.x and I got this error message below. Node error: {"code":-32601,"message":"The method eth_sendTransaction does not exist/is not available"} – David Jul 24 '19 at 08:22
  • @DavidB Debug your contract instance. Maybe the instance is wrong. – Ferit Jul 24 '19 at 10:03
  • @ferit- I don't think in this case, the contract instance could be the root cause. Because, I have done a test on local machine Ganache and it was working fine. Only, when I test on Ropsten network then it gives me the "code-32601" error message. – David Jul 25 '19 at 01:39
  • I strongly recommend to upgrade to web3. – Ferit Jul 25 '19 at 05:31
  • @ferit- Alright, any specific version would you recommend? web3 1.x? – David Jul 25 '19 at 07:22
  • @ferit- I can see the transfer method is there. I have no idea why it throwing me the "eth_sendTransaction method not found". – David Jul 25 '19 at 07:26
  • @ferit- Did you try that successfully on Ropsten via Infura? In my case, the environment is Ropsten and Infura. – David Jul 25 '19 at 08:26
  • Well, why don't you try to use another blockchain to isolate the issue? You should be doing this yourself. Try Kovan for example. @DavidB – Ferit Jul 25 '19 at 08:51
  • 1
    Thank you for your advised and your precious time much appreciated – David Aug 04 '19 at 13:09