Premiers pas avec TheRPC
Ethereum/Guides/Horodatages

Block Timestamps Guide

Block timestamps in Ethereum provide timing information for transactions and smart contracts. Understanding how to work with them is essential for time-sensitive applications.

Understanding Block Timestamps

  • How timestamps are determined
  • Accuracy and limitations
  • Miner manipulation considerations
  • Network time drift

Working with Timestamps

// Get block timestamp
const getBlockTimestamp = async (blockNumber) => {
	const block = await web3.eth.getBlock(blockNumber);
	return block.timestamp;
};

// Calculate average block time
const getAverageBlockTime = async (blockCount = 100) => {
	const latestBlock = await web3.eth.getBlockNumber();
	const oldBlock = await web3.eth.getBlock(latestBlock - blockCount);
	const newBlock = await web3.eth.getBlock(latestBlock);

	return (newBlock.timestamp - oldBlock.timestamp) / blockCount;
};

Best Practices

  1. Don't rely on exact timestamp precision
  2. Account for potential miner manipulation
  3. Use block numbers for precise ordering
  4. Consider network time drift
  5. Implement safe time buffers

See also

Aidez-nous à nous améliorer !
Partagez cette page et aidez-nous à créer un produit encore meilleur pour vous.