Introduction
In this tutorial, we will learn how to track newly created tokens on the Ethereum blockchain. With the rapid growth of decentralized finance (DeFi) and non-fungible tokens (NFTs), keeping an eye on newly created tokens can be valuable for investors, developers, and enthusiasts. We will use Etherscan's API to monitor token creation events and Web3.js to interact with the Ethereum network.
Prerequisites
- Node.js and npm installed on your system.
- Basic knowledge of JavaScript and Ethereum.
- An Etherscan API key (you can obtain one by signing up on Etherscan).
Step 1: Set Up Your Project
- Create a new project directory:
mkdir ethereum-token-tracker
cd ethereum-token-tracker
- Initialize a new Node.js project:
npm init -y
- Install the necessary libraries:
npm install web3 axios
Step 2: Connect to the Ethereum Network
Create a new file called index.js
and add the following code to connect to the Ethereum blockchain:
const Web3 = require('web3');
// Connect to an Ethereum node using Infura
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
console.log('Connected to Ethereum Mainnet');
Replace YOUR_INFURA_PROJECT_ID
with your Infura project ID. You can sign up for a free Infura account and create a project to get the project ID.
Step 3: Fetch Newly Created Tokens
We will use Etherscan's API to fetch information about newly created tokens. Add the following function to your index.js
file:
const axios = require('axios');
// Replace with your Etherscan API key
const ETHERSCAN_API_KEY = 'YOUR_ETHERSCAN_API_KEY';
async function getNewTokens() {
try {
const url = `https://api.etherscan.io/api?module=account&action=tokentx&address=0x0&startblock=0&endblock=99999999&sort=asc&apikey=${ETHERSCAN_API_KEY}`;
const response = await axios.get(url);
if (response.data.status === '1') {
const transactions = response.data.result;
const newTokens = transactions.filter(tx => tx.tokenSymbol && tx.tokenName && tx.from === '0x0000000000000000000000000000000000000000');
console.log('Newly Created Tokens:');
newTokens.forEach(token => {
console.log(`- Token Name: ${token.tokenName}`);
console.log(` Token Symbol: ${token.tokenSymbol}`);
console.log(` Token Contract Address: ${token.contractAddress}`);
});
} else {
console.error('Error fetching token transactions:', response.data.message);
}
} catch (error) {
console.error('Error:', error);
}
}
getNewTokens();
Explanation
- Etherscan API: We use the
tokentx
action from Etherscan's API to fetch token transfer events. The API returns a list of token transactions, including newly created tokens. - Filter for New Tokens: We filter transactions where the
from
address is 0x0000000000000000000000000000000000000000
, indicating a token creation event. This address is used as a placeholder for the "zero address" in token contracts. - Display Information: We log the name, symbol, and contract address of each newly created token.
Step 4: Monitor for New Tokens
You can run this script periodically to monitor for new token creation events. Consider setting up a cron job or a scheduled task to execute the script at regular intervals.
node index.js
Conclusion
In this tutorial, we explored how to track newly created tokens on the Ethereum blockchain using Etherscan's API and Web3.js. This approach provides a simple yet effective way to monitor token creation events, which can be useful for various applications in the blockchain space.