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.
Tutorial
Getting Wallet Balance Using Ethers.js in Node.js
const ethers = require('ethers');
// Replace this with your Ethereum wallet's private key or mnemonic phrase
const privateKey = 'YOUR_PRIVATE_KEY_OR_MNEMONIC';
// Use a public Ethereum node URL (this is a public node for demonstration purposes)
const publicProvider = new ethers.JsonRpcProvider('https://mainnet.publicnode.com');
// Create a wallet instance using your private key and connect it to the public node provider
const wallet = new ethers.Wallet(privateKey, publicProvider);
// Function to get the balance of the wallet
async function getWalletBalance() {
// Get the wallet's balance in wei
const balanceInWei = await wallet.getBalance();
// Convert the balance from wei to Ether for readability
const balanceInEther = ethers.utils.formatEther(balanceInWei);
// Log the results
console.log(`Wallet Address: ${wallet.address}`);
console.log(`Wallet Balance: ${balanceInEther} ETH`);
}
// Execute the function
getWalletBalance();- Replace
'YOUR_PRIVATE_KEY_OR_MNEMONIC'with your actual Ethereum wallet's private key or mnemonic phrase. - For the Infura method, replace
'YOUR_INFURA_PROJECT_ID'with the Infura Project ID you received when you created your Infura account.
Oct 24, 2024
Read More