Polygon's eth_getBalance
API method retrieves the balance of any Polygon account in Wei (the smallest denomination of ether). This essential tool enables developers to verify account balances, monitor funds, and display financial information within applications.
How eth_getBalance Works
The method returns the balance stored at a specific Polygon address at a given block height. Key components:
Parameters
address
: The Polygon wallet address to queryblock parameter
: Specifies which block to reference:latest
: Current blockchain stateearliest
: Genesis blockpending
: Unconfirmed transactions
Response
quantity
: The balance in Wei (as hexadecimal)
Practical Implementation
Web3.js Code Example
const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);
const accountAddress = '0xB18614D1e3A3B67A6E0c83Be98AC04157b674083';
const minimumBalance = 10000000000000000000; // 10 Matic in wei
async function checkBalance(address) {
const balance = await web3.eth.getBalance(address, 'latest');
return balance;
}
async function fillUp(balance) {
const minimumConverted = web3.utils.fromWei(String(minimumBalance), 'ether');
if (balance < minimumBalance) {
console.log(`The balance of the account ${accountAddress} is below ${minimumConverted} Matic.`);
console.log(`Sending more funds...`)
// Additional funding logic here
} else {
console.log(`The balance of the account ${accountAddress} is above ${minimumConverted} Matic.`);
}
}
async function main() {
const balance = await checkBalance(accountAddress);
const balanceInMatic = web3.utils.fromWei(balance, 'ether');
console.log(`Current balance: ${balanceInMatic} Matic \n`);
await fillUp(balance);
}
main();
This script demonstrates automatic balance monitoring with:
- Real-time balance checks
- Minimum balance thresholds
- Conditional funding alerts
๐ Explore more Polygon developer tools
Core Use Cases
- Funds Monitoring: Track account balances in real-time
- Automated Replenishment: Maintain minimum balances
- Financial Dashboards: Display current holdings
Frequently Asked Questions
What's the difference between Wei and MATIC?
Wei is the smallest unit (1 MATIC = 10^18 Wei), used for precise calculations. The fromWei
method converts balances to user-friendly MATIC values.
How often should I check balances?
For automated systems, checks every 5-15 minutes typically balance accuracy with node request limits.
Can I use eth_getBalance with smart contracts?
Yes, but it only returns the native MATIC balance, not ERC-20 token balances which require separate contract calls.
๐ Master Polygon development with our advanced guide
Best Practices
- Cache results to reduce node requests
- Handle errors for network instability