facebook

How to Transfer Ethereum Token using NodeJS

Posted By : Vishal

Jun 30, 2020

This article gives you a step-by-step guide to transferring an Ethereum token using NodeJS.

 

Ethereum

 

Ethereum is the second-largest blockchain-powered cryptocurrency after bitcoin. It enables developers to program over its blockchain platform to create new types of dApps and smart contract solutions.

 

Ethereum is a decentralized platform that means any government, private organization, or centralized authority cannot control it. People can use applications built with the Ethereum blockchain from any part of the world with an internet connection.

 

Check Out: An Introductory Guide to Ethereum 2.0 | A Major Upgrade

 

Functioning of Ethereum Network

 

Smart contracts define how things get done in the Ethereum system. A smart contract is a set of governance rules used to achieve any business goal. Solidity language is used to write smart contracts.

 

You can create a token over the Ethereum network using a smart contract. Network users can utilize these tokens as digital currencies. One of the most popular types of tokens on Ethereum is an ERC20 token. 

 

The smart contract can be used for various business purposes. It can be used to support other contracts in a similar way software libraries work.

 

Types of Tokens on Ethereum

 

  • ERC20: These are fungible tokens, they hold a specific value.
  • ERC721: These are non-fungible tokens. They hold unique assets like documents and certificates that are not interchangeable.

 

Read More about Ethereum ERC Token Standard

 

How to Transfer Ethereum Token using NodeJS

 

To connect tokens to any network, you can use an infra API key, set up a local node, and sync it with Mainent or another testnet.

 

Prerequisites

 

  • NodeJS
  • Infra API
  • Smart contract ABI and address

 

Steps to Transfer Ethereum Token using NodeJS

 

Step 1: Install the required dependencies for Ethereum

 

npm install web3
npm install ethereumjs-tx

 

Step 2: Create a connection to testnet

 

const Web3 = require("web3");
const Tx = require('ethereumjs-tx');

// Create connection with test net
var web3=new Web3("Infura API endpoint);

var contractAbi = ABI //ABI in JSON format
var contractAddress = Contract_Address

var sender = Sender_Address
var private = Sender_privatekey

var receiver = Reciever_Address
var amountInDecimal = 1 //Amount of token

 

Step 3: Create a signed transaction and send tokens

 

const myContract = new web3.eth.Contract(contractAbi, contractAddress);
var privateKey = new Buffer(private, 'hex')

var txObject = {};
txObject.nonce = web3.utils.toHex(result);
txObject.gasLimit = web3.utils.toHex(90000);

txObject.gasPrice = web3.utils.toHex(10);
txObject.to = contractAddress;
txObject.from = sender;
txObject.value = '0x';

// Calling transfer function of contract and encode it in AB format
txObject.data = myContract.methods.transfer( receiver, web3.utils.toHex(
web3.utils.toWei(amountInDecimal.toString(), "ether"))).encodeABI();

//Sign transaction before sending
var tx = new Tx(txObject);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('transactionHash', ((data) => {
         console.log(data);
   }));
}).catch(err => {
   console.log(err);
})

 

Also, Read: ERC-20 Token Standard | A Compact Guide to Development

 

Conclusion

 

By following the above steps you can create your code to send tokens from one account to another account. You don't need to rely on any third-party software to interact with the Ethereum network. You can simply send tokens with very few lines of code.

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

December 4, 2024 at 09:10 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text