TechnologyJuly 20, 2022

Pulling Real-Time Ethereum Transactions with Web3.js

Alex Leventer
Alex LeventerEcosystem
Pulling Real-Time Ethereum Transactions with Web3.js
mkdir w3 cd w3 yarn init touch app.js
yarn add web3
const Web3 = require('web3');
const RPC_ENDPOINT = “REPLACE_ME”;
const web3 = new Web3(RPC_ENDPOINT); #replace RPC_ENDPOINT with the endpoint you spun up in step 1.
const subscription = web3.eth.subscribe(
 "newBlockHeaders",
 (err, result) => {
     const { number } = result;
     console.log(number)
});
web3.eth.getBlock('123').then(blockResults => console.log(blockResults))
{ difficulty: '18118731572', extraData: '0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32', gasLimit: 5000, gasUsed: 0, hash: '0x37cb73b97d28b4c6530c925d669e4b0e07f16e4ff41f45d10d44f4c166d650e5', logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', miner: '0xbb7B8287f3F0a933474a79eAe42CBCa977791171', mixHash: '0x2e4b92a11b1bac2a311f6a47006442bf1dc689e76c9c1fee90da56ff6f2df7a7', nonce: '0x18c851620e8d6cb6', number: 123, parentHash: '0x701bc7632e80976d1a2c408ffa58e4f11aa3ed3c5a030d1125930a9d944e4343', receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', size: 542, stateRoot: '0x2d1e6407139174d74e9485ce0b9f80d31f6ec55f447708796d2582e3ffbdbb85', timestamp: 1438270492, totalDifficulty: '2181259381686', transactions: [], transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', uncles: [] }
Discover more
Amazon Web ServicesData Processingdata operationsGraphQLDataStax Astra DB
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.