This article applies to:
- Ethereum
- Polygon
- BNB Smart Chain
- Fantom
- Harmony
- EVM nodes
Introduction
This article provides you with a simple way to test how long it takes for a submitted transaction to show up in the mempool.
In brief:
- Set up mempool monitoring with web3.js.
- Submit a transaction using your favorite tool (e.g. MetaMask).
- Check when the transaction shows up in the mempool.
How-to
Set up monitoring with web3.js.
For a general overview of the method you are going to use, see web3.js: subscribe("pendingTransactions").
Use the following code to set up the mempool monitoring:
var add = 'WSS_ENDPOINT'
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.WebsocketProvider(add));
const account = 'ADDRESS'.toLowerCase();
const subscription = web3.eth.subscribe('pendingTransactions', (err, res) => {
if (err) console.error(err);
});
subscription.on('data', (txHash) => {
setTimeout(async () => {
try {
let tx = await web3.eth.getTransaction(txHash);
if (tx && tx.from) {
if (tx.from.toLowerCase() === account) {
console.log('Transaction Hash: ',txHash );
console.log('Block: ',tx.blockNumber );
console.log('Timestamp: ',new Date());
console.log('From: ',tx.from );
console.log('To: ',tx.to );
console.log('Value: ',web3.utils.fromWei(tx.value, 'ether'));
console.log('Max fee: ',web3.utils.fromWei(tx.maxFeePerGas, 'ether'));
console.log('Max priority fee: ',web3.utils.fromWei(tx.maxPriorityFeePerGas, 'ether'));
console.log('=====================================')
}
}
} catch (err) {
console.error(err);
}
})
});
where
- WSS_ENDPOINT — your node WSS endpoint. See View node access and credentials.
- ADDRESS — your account from which you are going to send the transaction.
Send a transaction
Send a transaction using your favorite tool—for example, MetaMask.
The monitoring instance will print your transaction the moment it enters your node's mempool.
The printout is structured in the same way as the Etherscan-based explorers are structured for ease of reading.
Example:
Transaction hash: 0xd55dd4ff30052265160ffbadc0a3f55dd6253c912359d77b0b4d772acd4f72cc
Block: null
Timestamp: 2022-02-07T02:00:38.581Z
From: 0x2097411FfDF979Ef6E7Ff61438c3d8243F2207B9
To: 0x06908fDbe4a6af2BE010fE3709893fB2715d61a6
Value: 0.001
Max fee: 0.000000001
Max priority fee: 0.000000001
=====================================
The null value in Block means that the transaction is pending.
The Timestamp value is in UTC.
For a more comprehensive guide and instructions on how to handle Blockchain transactions in Ethereum mempool, you may refer to our article Blockchain transactions in Ethereum mempool – Coding edition
Comments
0 comments
Article is closed for comments.