Docs

eth_blockNumber - Get Latest Block Number

Returns the current block number on Centrifuge Mainnet. Essential for syncing dApps and monitoring blockchain state.

Returns the number of the most recent block on Centrifuge Mainnet.

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

Implementation Examples

Common Use Cases

1. Block Confirmation Counter

Monitor transaction confirmations:

JavaScript
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, confirmations = 6) {
  let currentConfirmations = 0;
  
  while (currentConfirmations < confirmations) {
    currentConfirmations = await getConfirmations(txHash);
    console.log(`Confirmations: ${currentConfirmations}/${confirmations}`);
    await new Promise(r => setTimeout(r, 2000)); // Check every 2 seconds
  }
  
  return true;
}

2. Event Log Filtering

Query events from recent blocks:

JavaScript
async function getRecentEvents(contract, eventName, blockRange = 100) {
  const currentBlock = await provider.getBlockNumber();
  const fromBlock = currentBlock - blockRange;
  
  const filter = contract.filters[eventName]();
  const events = await contract.queryFilter(filter, fromBlock, currentBlock);
  
  return events;
}

3. Node Health Monitoring

Check if your node is synced:

JavaScript
async function checkNodeHealth() {
  try {
    const blockNumber = await provider.getBlockNumber();
    const block = await provider.getBlock(blockNumber);
    
    const now = Date.now() / 1000;
    const blockAge = now - block.timestamp;
    
    if (blockAge > 60) {
      console.warn(`Node may be behind. Last block was ${blockAge}s ago`);
      return false;
    }
    
    console.log(`Node healthy. Latest block: ${blockNumber}`);
    return true;
  } catch (error) {
    console.error('Node unreachable:', error);
    return false;
  }
}

Performance Optimization

Caching Strategy

Cache block numbers to reduce API calls:

JavaScript
class BlockNumberCache {
  constructor(ttl = 2000) { // 2 second cache
    this.cache = null;
    this.timestamp = 0;
    this.ttl = ttl;
  }
  
  async get(provider) {
    const now = Date.now();
    
    if (this.cache && (now - this.timestamp) < this.ttl) {
      return this.cache;
    }
    
    this.cache = await provider.getBlockNumber();
    this.timestamp = now;
    return this.cache;
  }
  
  invalidate() {
    this.cache = null;
    this.timestamp = 0;
  }
}

const blockCache = new BlockNumberCache();

Batch Requests

Combine with other calls for efficiency:

JavaScript
const batch = [
  { jsonrpc: '2.0', method: 'eth_blockNumber', params: [], id: 1 },
  { jsonrpc: '2.0', method: 'eth_gasPrice', params: [], id: 2 },
  { jsonrpc: '2.0', method: 'eth_chainId', params: [], id: 3 }
];

const response = await fetch('https://api-centrifuge.n.dwellir.com/YOUR_API_KEY', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(batch)
});

const results = await response.json();