eth_blockNumber
Returns the number of the most recent block on TRON network.
When to Use This Method
eth_blockNumber
is fundamental for:
- Syncing Applications - Keep your dApp in sync with the latest blockchain state
- Transaction Monitoring - Verify confirmations by comparing block numbers
- Event Filtering - Set the correct block range for querying logs
- Health Checks - Monitor node connectivity and sync status
Parameters
This method accepts no parameters.
{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}
Returns
QUANTITY
- Integer of the current block number the node is synced to.
- Type: Hexadecimal string
- Format:
0x
prefixed - Example:
0x37a6f80
(58290048 in decimal)
Implementation Examples
- cURL
- JavaScript
- Python
curl -X POST https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}'
// Using fetch
const response = await fetch('https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
id: 1,
}),
});
const data = await response.json();
const blockNumber = parseInt(data.result, 16);
console.log('Current block:', blockNumber);
// Using ethers.js
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY');
const blockNumber = await provider.getBlockNumber();
console.log('Latest block:', blockNumber);
// Using web3.js
import Web3 from 'web3';
const web3 = new Web3('https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY');
const blockNumber = await web3.eth.getBlockNumber();
console.log('Current block height:', blockNumber);
import requests
import json
# Using requests library
url = 'https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY'
headers = {'Content-Type': 'application/json'}
payload = {
'jsonrpc': '2.0',
'method': 'eth_blockNumber',
'params': [],
'id': 1
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
result = response.json()
block_number = int(result['result'], 16)
print(f'Current block: {block_number}')
# Using web3.py
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://api-tron-jsonrpc.dwellir.com/YOUR_API_KEY'))
block_number = w3.eth.block_number
print(f'Latest block: {block_number}')
Example Response
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x37a6f80"
}
Common Use Cases
1. Block Confirmation Tracking
async function getConfirmations(txHash) {
const tx = await provider.getTransaction(txHash);
if (!tx || !tx.blockNumber) return 0;
const currentBlock = await provider.getBlockNumber();
return currentBlock - tx.blockNumber + 1;
}
// Wait for specific confirmations
async function waitForConfirmations(txHash, requiredConfirmations = 12) {
while (true) {
const confirmations = await getConfirmations(txHash);
if (confirmations >= requiredConfirmations) {
return confirmations;
}
await new Promise(resolve => setTimeout(resolve, 3000)); // 3 second blocks
}
}
2. Real-time Block Monitoring
let lastBlock = 0;
async function monitorBlocks() {
setInterval(async () => {
try {
const currentBlock = await provider.getBlockNumber();
if (currentBlock > lastBlock) {
console.log(`New block: ${currentBlock}`);
// Process new blocks
for (let i = lastBlock + 1; i <= currentBlock; i++) {
await processBlock(i);
}
lastBlock = currentBlock;
}
} catch (error) {
console.error('Block monitoring error:', error);
}
}, 3000); // Check every 3 seconds (TRON block time)
}
3. Event Filtering with Block Range
async function getRecentEvents(contractAddress, eventName, blockCount = 100) {
const currentBlock = await provider.getBlockNumber();
const fromBlock = currentBlock - blockCount;
const filter = {
address: contractAddress,
topics: [ethers.id(eventName)],
fromBlock: fromBlock,
toBlock: currentBlock
};
const logs = await provider.getLogs(filter);
return logs;
}
4. Node Sync Status Check
async function checkNodeSync() {
try {
const blockNumber = await provider.getBlockNumber();
const block = await provider.getBlock(blockNumber);
const currentTime = Date.now() / 1000;
const blockTime = block.timestamp;
const timeDiff = currentTime - blockTime;
if (timeDiff > 60) { // More than 60 seconds behind
console.warn(`Node is ${timeDiff} seconds behind`);
return false;
}
console.log(`Node is synced at block ${blockNumber}`);
return true;
} catch (error) {
console.error('Sync check failed:', error);
return false;
}
}
Best Practices
- Cache Block Numbers - Avoid excessive calls by caching recent block numbers
- Handle Hex Conversion - Always parse the hex result to decimal for calculations
- Monitor Block Production - TRON produces blocks every ~3 seconds
- Error Handling - Implement retry logic for network issues
Error Codes
Code | Message | Description |
---|---|---|
-32600 | Invalid Request | The JSON sent is not a valid Request object |
-32601 | Method not found | The method is not available |
-32602 | Invalid params | Invalid method parameters |
-32603 | Internal error | Internal JSON-RPC error |
-32000 | Node not synced | The node is still syncing |
Block Time Information
TRON network produces blocks approximately every 3 seconds, which is faster than Ethereum's ~12 seconds. This means:
- 20 blocks per minute
- 1,200 blocks per hour
- 28,800 blocks per day
Related Methods
- eth_getBlockByNumber - Get block details by number
- eth_getBlockByHash - Get block details by hash
- eth_syncing - Check if node is syncing