Skip to content
Advertisement

How to send an ERC20 token with web3js

I’m currently using the 0.2x.x version of the Web3 JavaScript API. I deployed my custom ERC20 token by creating smart contract in solidity (on REMIX IDE). I installed the MetaMask and had a test on https://wallet.ethereum.org/ to send some custom ERC token to another my account. It worked well. I want to add the ‘send custom ERC20 Token’ function in my JavaScript code using Web3js.

Here is my code below.

JavaScript

I’m working on the ‘Ropsten Testnet’ and I sent some ‘ether’ for test using sendRawTransaction(). But what I want to do is just sending my custom ERC20 token. not the ether. How can I send my own token in this script? I have no idea. I tried to use sendTransaction() like below.

JavaScript

I continuously received the error.

JavaScript

I really can’t understand what is wrong with it. Please let me know some way to deal with this. Thanks!

Advertisement

Answer

Take EOS token transfering as the example.

Below code need web3 and ethereumjs-tx. If you’ve not install them, install them with npm install web3 ethereumjs-tx

JavaScript

You also can check the transaction with https://etherscan.io.

After the transaction send, you can check it with https://etherscan.io/address/0xaa597b7e8aaffe9f2a187bedb472ef3455957560#tokentxns, and you’ll see the pending transaction as below on the Transactions filed, but you cannot see it in the Token Transfers field.

enter image description here

Wait about 1 minute, you can see the successed on both Transactions filed and the Token Transfers field.

enter image description here enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement