Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |