Blockchain Development Programming Tutorials, Guides & Best Practices
Explore 30+ expertly crafted blockchain development tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Etherscan vs Infura: Choosing the Right API for Your Blockchain Application
You should use Etherscan when you need to read data from the Ethereum blockchain, such as querying transaction details, wallet balances, or token transfers. Etherscan is a powerful tool for building blockchain explorers or applications that focus on data analytics.
const axios = require('axios');
// Replace with your actual Etherscan API key
const apiKey = 'YOUR_ETHERSCAN_API_KEY';
// Replace with the Ethereum address you want to query
const address = '0xYourEthereumAddress';
// Etherscan API URL to fetch wallet balance
const url = `https://api.etherscan.io/api?module=account&action=balance&address=${address}&tag=latest&apikey=${apiKey}`;
async function getWalletBalance() {
try {
const response = await axios.get(url);
const balanceInWei = response.data.result;
// Convert balance from Wei to Ether
const balanceInEther = balanceInWei / 1e18;
console.log(`Wallet Balance: ${balanceInEther} ETH`);
} catch (error) {
console.error('Error fetching balance:', error);
}
}
getWalletBalance();Sending Transactions and Interacting with Smart Contracts Using Infura and Ethers.js
Next, let’s interact with a smart contract using Ethers.js and Infura. For this example, we will call a read-only function from an ERC-20 token contract (like querying the balance of a wallet).
const ethers = require('ethers');
// Replace with your Infura Project ID
const infuraProvider = new ethers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
// Replace with the ERC-20 contract address (e.g., USDT, DAI, or any token)
const contractAddress = '0xTokenContractAddress';
// Replace with the wallet address to query the balance
const walletAddress = '0xYourWalletAddress';
// ABI of the ERC-20 token (we only need the balanceOf function here)
const abi = [
'function balanceOf(address owner) view returns (uint256)'
];
// Create a contract instance
const contract = new ethers.Contract(contractAddress, abi, infuraProvider);
async function getTokenBalance() {
try {
// Call the balanceOf function
const balance = await contract.balanceOf(walletAddress);
// Convert the balance from wei (for ERC-20 tokens, it could be small denominations)
console.log(`Token Balance: ${balance.toString()}`);
} catch (error) {
console.error('Error fetching token balance:', error);
}
}
// Call the function to query the token balance
getTokenBalance();Getting Wallet Balance Using Ethers.js in Node.js
> Important: Keep your private key secure. Never share it publicly or commit it to version control. For better security, consider using environment variables to store sensitive information like private keys.
Once your script is set up, run it from the command line:
Blockchain Development Tools, Libraries, and Frameworks Cheatsheet
- Website: Tenderly
- Description: An open-source blockchain explorer for Ethereum-based networks.
- Key Features:
- Provides a user-friendly interface for exploring blocks, transactions, and tokens.
- Supports custom networks and sidechains.
- Allows token holders to track token balances and transaction histories.
- Easy to deploy and customize.
- Website: BlockScout