DEV Community

Cover image for 4 steps to send token using Web3.js
shrey vijayvargiya
shrey vijayvargiya

Posted on • Updated on

4 steps to send token using Web3.js

Under the Hood

For the past 4 months, I have been working on the web3 blockchain app building a wallet app.

Our wallet app contains the following core functionalities

  • Send token
  • Swap token
  • View tokens
  • Receive tokens
  • Buy tokens Let’s start with the send token, How do I add it to my mobile app using react-native?

4 steps to send a token

Authenticate user using the seed phrase
First users need to log in to our web3 wallet app.

Similar to the web2 JWT token we recognise whether is user is logged in or not in web3 we use the wallet address and user wallet private key to authenticate the user in our app.

Authenticate the user to get his wallet address and private key, authentication can be done via

  • User 12 words seed phrase also called a mnemonic phrase
  • Using metamask or third-party wallets such as Trustwallet, WalletConnect Once the user is authenticated.

Validate the receiver wallet address using web3.js

Web3.js provides utils methods to validate the user's wallet address.

const isValidAddress = web3.utisl.isAddress(receiverWalletAddress);
return isValidAddress;
Enter fullscreen mode Exit fullscreen mode

Select the token and amount of token the user wants to send
Give the user a dropdown or select to pick the token he/she wants to send
Add token amount input to get the amount of tokens sent and make sure you convert this amount into wei instead of number.

const amount = web3.utils.toWei(tokenAmount, 'ether');
Enter fullscreen mode Exit fullscreen mode

Sign this transaction using the user wallet address and token contract address

We are sending tokens from one wallet to another in that case we have to deal with the token contract address on the blockchain network.

The simple maths is deducting the token amount from one wallet and adding the same token amount to the receiver wallet address.

For the same, we need to deal with tokens so for that we need a token contract that can be created using a token address and token ABI

Token ABI is almost the same for all ERC-20 tokens on eth, binance and polygon networks and can be found easily on the internet.

const tokenContract = web3.eth.contract(tokenContractAddress, TOKEN_ABI);
Enter fullscreen mode Exit fullscreen mode

Send the signed transaction using the private key and web3.js

Finally, we just need to send the signed transaction using the private key.

const txn = signedTxn(txn, {privateKey})
return txn.transactionHash;
Enter fullscreen mode Exit fullscreen mode

This transaction hash is the unique hash for this particular transaction and in return, it will give the transaction status.

You can also find each transaction's details on the explorer such as ETH scan or BSC can use the following link

Eth Explorer - https://ethscan/tx/trsansactionhash
BSC Explorer - https://bscscan/tx/trsansactionhash
Enter fullscreen mode Exit fullscreen mode

Conclusion

That’s it, in just 4 steps you can send tokens from one wallet to another

  • Authenticate the user to get private key and wallet address/addresses using the seed phrase
  • Track the transaction status using the transaction hash
  • Check the transaction details on the corresponding explorer using the hash
  • Send tokens on a particular network such as Ethereum, binance or polygon Keep developing Shrey iHateReading

Top comments (0)