Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lvandeyar committed Aug 18, 2023
1 parent e67ae14 commit e1d38e5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
@@ -1 +1,7 @@
# programmatically-retrieving-blockchain-data
# A blog series about programmatically access the blockchain

## Summary
The blog series https://laurencevandeyar.com/category/blockchain/programmatically-retrieving-blockchain-data/focuses on the process of programmatically retrieving blockchain data using Node.js and Web3. The series delves into practical techniques for interacting with blockchain networks, particularly Ethereum-like chains, through code. It explores how to establish connections to blockchain nodes, fetch transaction details from blocks, and gather insights from the blockchain's transaction history. By guiding readers through the steps involved in using the Web3 library, the series empowers developers, researchers, and blockchain enthusiasts to access and analyze blockchain data programmatically, unlocking a deeper understanding of decentralized ledger technology.

## License
MIT
49 changes: 49 additions & 0 deletions part2.js
@@ -0,0 +1,49 @@
#!/usr/bin/node

const { Web3 } = require('web3');

var quicknodeurl = 'YOUR_QUICKNODE_URL';

// Replace this with the BSC mainnet provider URL
const providerUrl = quicknodeurl

const web3 = new Web3(providerUrl);

async function getTransactionDetails(transactionHash) {
try {
const transaction = await web3.eth.getTransaction(transactionHash);
console.log('Transaction:', transaction);
} catch (error) {
console.error('Error fetching transaction details:', error);
}
}

async function getBlockTransactions(blockNumber) {
try {
const block = await web3.eth.getBlock(blockNumber);
console.log('Block Number:', block.number);
console.log('Block Transactions:', block.transactions);

for (const transactionHash of block.transactions) {
await getTransactionDetails(transactionHash);
}
} catch (error) {
console.error('Error fetching block transactions:', error);
}
}

async function fetchAllBlocks() {
try {
const latestBlockNumber = await web3.eth.getBlockNumber();
const genesisBlockNumber = 0;

for (let blockNumber = latestBlockNumber; blockNumber >= genesisBlockNumber; blockNumber--) {
console.log('Fetching block:', blockNumber);
await getBlockTransactions(blockNumber);
}
} catch (error) {
console.error('Error fetching blocks:', error);
}
}

fetchAllBlocks();

0 comments on commit e1d38e5

Please sign in to comment.