Find the last transaction

Introduction

This guide walks through the process of using web3.js to get last block and get last transaction.
For this example, we will use Node.js and straightforward JavaScript code.
This example assumes that you have configured Web3 as described in the previous section.

Get Block Number

1
const lastBlockNumber = await web3.eth.getBlockNumber();
2
console.log('Last block number: ', lastBlockNumber);
3
4
let block = await web3.eth.getBlock(lastBlockNumber);
5
6
console.log('Last block hash: ', block.hash);
7
console.log('Last block transactions: ', block.transactions);
Copied!

Get Last Transaction

1
block = await web3.eth.getBlock(blockNumber);
2
3
const lastTransaction = block.transactions[block.transactions.length - 1];
4
console.log('Last transaction hash: ', lastTransaction);
5
6
const transaction = await web3.eth.getTransaction(lastTransaction);
7
console.log('Last transaction: ', JSON.stringify(transaction));
Copied!
Code samples can be found here.
Last modified 3mo ago