Get Your Copy of The CXO's Playbook for Gen AI: Practical Insights From Industry Leaders.  Download Now >
Back to Blogs

Step-by-Step Guide to Create and Transact Your ERC20 Token

When it comes to creating tokens, most entrepreneurs choose to issue ERC-20 cryptocurrency units due to the high flexibility and security provided by the Ethereum blockchain. ERC-20 is one of the most significant Ethereum Request for Comments (ERCs) and has emerged as the technical standard for writing smart contracts on the Ethereum network, facilitating token implementation. Today, wallets and exchanges use the ERC-20 standard to integrate various standardized tokens onto their platforms, allowing for seamless and easy exchange between ERC-20 tokens and other tokens. This widespread adoption of the ERC-20 token standard has made interaction between tokens almost seamless and painless.

At the end of this post, you'll learn how to:

  1. Create a new ERC20 token in ethereum and make it available over the Ropsten Test Network. (So that we need not spend real ethers for creation and transaction fees when we do in main network)
  2. Spend the actual token across available addresses over the same network with some real time transactions using a node js service

Before proceeding to this section, one must have basic knowledge of:

  1. Ethereum private network, public network, main network and test network
  2. Any one of the ethereum clients, possibly go-ethereum(geth)

How to create your ERC20 token

As mentioned earlier, we are going to create and deploy our new token’s smart contract inside the Ropsten Test network. But remember, if we want to monetize our token ever and have it listed in exchanges, we will have to do the same process in Ethereum main network. But, this is going to result in spending real ethers for token deployment and transaction fee.

I would recommend following all the steps mentioned here which has pretty much straightforward steps in creating your identity in MyEtherWallet and also having your new token up and running in the test network.

At this point, I hope you have the following components handy with you,

  1. Your own private and public key identities in MyEtherWallet
  2. Your new token with the corresponding token address where its contract is deployed
  3. Your MyEtherWallet account loaded with some fake Ethers
  4. Knowledge in writing basic smart contracts with Solidity language and Remix editor
  5. What is ABI and Gas in ethereum

Transacting with your own tokens:

Now comes the more critical part. We are going to spend the actual tokens over the network in which we created the same.

For this we are going to use, Web3 JS - the official javascript API by ethereum to talk with the smart contract that we deployed earlier for our token.

Connecting to the Ropsten network via local node: (Optional):

This section is useful if you want to run a local node of the Ropsten test network. By doing this, you become a participating node of the Ropsten network, allowing you to sync a copy of the entire blockchain on your local machine and enabling you to submit transactions, mine blocks, etc. To achieve this, follow these steps:

Ethereum Client:

  1. Preferably use go-ethereum (geth) to connect to any existing network and its chain. (Alternatives like Parity or TestRPC can also be used.)
  2. Download and install geth from the official website.
  3. Choose to download and compile from source, which will be straightforward.

Setup:

  1. Navigate to the directory where your geth executable is located (typically inside the build/bin directory)
  2. Run the following command:

./geth --testnet --syncmode "fast" --cache=2048 --rpc

This command starts geth in fast sync mode and connects to the --testnet, which implicitly connects to the Ropsten network. Adjust the cache memory value as per your system’s configuration. 

The --rpc flag denotes that once the sync is completed, a new endpoint will be exposed, typically https://localhost:8545, for RPC connections from external applications.

Note:

  • The initial sync can take more than 24 hours depending on network speed, as you are syncing the entire Ropsten blockchain.
  • Reserve at least 20GB of space for the <home>/.ethereum/testnet/geth/chaindata folder where your blocks will be downloaded. This space will increment over time as new blocks are added to the test network.
  • Alternatives to geth, such as Parity, can help overcome some of these issues.

Sending tokens with Web3JS:

Now comes the actual transaction part. As mentioned earlier, we are going to use the Web3JS API to interact with the smart contract over the network. 

For this, I will install the NodeJS implementation of Web3JS. However, you can choose other options like Yarn, Meteor, or even browser-based implementations as mentioned in their documentation.

Steps to Follow:

1.Install Node Modules:

npm install web3
npm install ethereumjs-tx

2.Create a New NodeJS Script:

Before copying the code, declare the following required variables at the top of the script:

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

// Define variables
var contractAddress = "<Your contract address>";
var fromAddress = "<Your public key from MyEtherWallet>";
var privateKey = "<Your private key from MyEtherWallet>";
var toAddress = "<Public key to which you need to transfer>";
var chainId = 0x03; // Ropsten network ID

// Contract ABI
var abi = [{"constant":true.....ABI is a blueprint of the methods available in your smart contract. Get the ABI value from https://ropsten.etherscan.io/address/<your contract address> or from the Remix editor used to compile the code.

// HTTP Endpoint
var httpEndPoint = 'https://ropsten.infura.io'; // or 'https://localhost:8545' if using a local node

// Gas limit and Gas price
var gasLimit = '0x...'; // Set appropriate gas limit
var gasPrice = '0x...'; // Set appropriate gas price in hex

// Transfer amount
var transferAmount = ...; // Set the amount to be transferred, appending zeros as per token's decimal

Example Code:

'use strict';

var web3 = new Web3(new Web3.providers.HttpProvider(httpEndPoint));

var count = web3.eth.getTransactionCount(fromAddress);
var contract = web3.eth.contract(abi).at(contractAddress);

var rawTx = {
  "from": fromAddress,
  "nonce": "0x" + count.toString(16),
  "gasPrice": gasPrice,
  "gasLimit": gasLimit,
  "to": contractAddress,
  "value": "0x0", // Indicates no ethers are being sent, only tokens
  "data": contract.transfer.getData(toAddress, transferAmount, {from: fromAddress}),
  "chainId": chainId
};

var privKeyBuffer = new Buffer(privateKey, 'hex');
var tx = new Tx(rawTx);
tx.sign(privKeyBuffer);

web3.eth.sendRawTransaction('0x' + tx.serialize().toString('hex'), function(error, tHash) {
  if (error) {
    console.log(error);
  } else {
    console.log(tHash);
  }
});

Run this NodeJS script in your command prompt. Upon successful completion, you will see a transaction hash printed in the console. 

This hash represents the actual transaction that took place. If you are running a local node, wait for a few seconds as the transaction needs to be submitted locally and then synced with the actual Ropsten network.

3.View the Transaction Status:

Navigate to Ropsten Etherscan and enter your transaction hash (<tHash>). Here, you can view the status of your transaction.

4.Check Token Balance:

Go to your public address on Etherscan. You will see the number of tokens reduced from your wallet and added to the receiver's address.

Next Steps:

Once you are comfortable with the process, you can repeat the same token creation and transaction steps on the Ethereum main network for real-time monetized trading. Ensure you have real ethers in your account ready to be spent for deployment and transaction fees.

By following these steps, you have completed creating and transacting with your own ERC-20 token.

Ideas2IT Team

Connect with Us

We'd love to brainstorm your priority tech initiatives and contribute to the best outcomes.

Open Modal
Subscribe

Big decisions need bold perspectives. Sign up to get access to Ideas2IT’s best playbooks, frameworks and accelerators crafted from years of product engineering excellence.

Big decisions need bold perspectives. Sign up to get access to Ideas2IT’s best playbooks, frameworks and accelerators crafted from years of product engineering excellence.