Beginner10 min
Checking Account Balance
Query account balance and information
Checking Account Balance
In this tutorial, you'll learn how to query account balances and information on the Beatoz blockchain.
Basic Balance Check
The simplest way to check an account balance:
const { Web3 } = require('@beatoz/web3');
const web3 = new Web3('https://rpc-testnet0.beatoz.io');
async function getBalance(address) {
const account = await web3.beatoz.getAccount(address);
// Convert from fons (smallest unit) to BTZ
const balance = web3.utils.fromFons(account.value.balance, 'beatoz');
return balance;
}
// Usage
const address = '0x...your_address';
const balance = await getBalance(address);
console.log(`Balance: ${balance} BTZ`);
Understanding Units
Beatoz Units
- BTZ: The main unit (like ETH in Ethereum)
- Fons: The smallest unit (like Wei in Ethereum)
- 1 BTZ = 10^18 Fons
Converting Between Units
// BTZ to Fons
const btz = '1.5';
const fons = web3.utils.toFons(btz, 'beatoz');
console.log(`${btz} BTZ = ${fons} Fons`);
// Fons to BTZ
const fonsValue = '1500000000000000000';
const btzValue = web3.utils.fromFons(fonsValue, 'beatoz');
console.log(`${fonsValue} Fons = ${btzValue} BTZ`);
Full Account Information
The getAccount method returns more than just the balance:
async function getAccountInfo(address) {
const account = await web3.beatoz.getAccount(address);
return {
address: address,
balance: web3.utils.fromFons(account.value.balance, 'beatoz'),
nonce: account.value.nonce,
// Additional fields may be available
};
}
const info = await getAccountInfo('0x...');
console.log('Account Info:', info);
Testnet
Account Properties
| Property | Description |
|---|---|
balance | Account balance in Fons |
nonce | Transaction count (used for ordering) |
Checking Multiple Accounts
async function getMultipleBalances(addresses) {
const results = await Promise.all(
addresses.map(async (address) => {
const account = await web3.beatoz.getAccount(address);
return {
address,
balance: web3.utils.fromFons(account.value.balance, 'beatoz'),
};
})
);
return results;
}
// Usage
const addresses = [
'0x...address1',
'0x...address2',
'0x...address3',
];
const balances = await getMultipleBalances(addresses);
balances.forEach(({ address, balance }) => {
console.log(`${address}: ${balance} BTZ`);
});
Formatting Balance for Display
function formatBalance(balance, decimals = 4) {
const num = parseFloat(balance);
if (num === 0) return '0 BTZ';
if (num < 0.0001) return '< 0.0001 BTZ';
return `${num.toFixed(decimals)} BTZ`;
}
// Usage
console.log(formatBalance('1.23456789')); // "1.2346 BTZ"
console.log(formatBalance('0.000001')); // "< 0.0001 BTZ"
Real-time Balance Monitoring
Advanced Usage
For real-time updates, you can poll the balance periodically or use WebSocket connections.
async function monitorBalance(address, intervalMs = 5000) {
let lastBalance = null;
setInterval(async () => {
const account = await web3.beatoz.getAccount(address);
const balance = web3.utils.fromFons(account.value.balance, 'beatoz');
if (balance !== lastBalance) {
console.log(`Balance changed: ${lastBalance} → ${balance} BTZ`);
lastBalance = balance;
}
}, intervalMs);
}
// Start monitoring
monitorBalance('0x...your_address');
Error Handling
async function safeGetBalance(address) {
try {
const account = await web3.beatoz.getAccount(address);
return {
success: true,
balance: web3.utils.fromFons(account.value.balance, 'beatoz'),
};
} catch (error) {
return {
success: false,
error: error.message,
};
}
}
If an address has never received tokens, getAccount might throw an error or return zero balance.
