Introduction
You can subscribe to the stream of global pending transactions running through your node's pool.
For a better & maintained examples, see:
Prerequisites
- web3.js installed
Solution
Subscribe to all global pending transactions running through your node
You can subscribe to a stream of all global pending transactions running through your node's pool.
Code (also see the commented sections //):
var add = 'WSS_ENDPOINT'
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.WebsocketProvider(add));
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) {
console.log('TX hash: ',txHash ); // transaction hash
console.log('TX confirmation: ',tx.transactionIndex ); // "null" when transaction is pending
console.log('TX nonce: ',tx.nonce ); // number of transactions made by the sender prior to this one
console.log('TX block hash: ',tx.blockHash ); // hash of the block where this transaction was in. "null" when transaction is pending
console.log('TX block number: ',tx.blockNumber ); // number of the block where this transaction was in. "null" when transaction is pending
console.log('TX sender address: ',tx.from ); // address of the sender
console.log('TX amount(in Ether): ',web3.utils.fromWei(tx.value, 'ether')); // value transferred in ether
console.log('TX date: ',new Date()); // transaction date
console.log('TX gas price: ',tx.gasPrice ); // gas price provided by the sender in wei
console.log('TX gas: ',tx.gas ); // gas provided by the sender.
console.log('TX input: ',tx.input ); // the data sent along with the transaction.
console.log('=====================================') // a visual separator
}
} catch (err) {
console.error(err);
}
})
});
where WSS_ENDPOINT is your node WSS endpoint.
See View node access and credentials.
Subscribe to all global pending transactions running through your node with an address filter
You can subscribe to a stream of all global pending transactions running through your node's pool and filter by address.
Code (also see the commented sections //):
var add = 'WSS_ENDPOINT'
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.WebsocketProvider(add));
const account = 'EVM_ACCOUNT'.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.to) { // This is the point you might be looking for to filter the address
if (tx.to.toLowerCase() === account) {
console.log('TX hash: ', txHash); // transaction hash
console.log('TX confirmation: ', tx.transactionIndex); // "null" when transaction is pending
console.log('TX nonce: ', tx.nonce); // number of transactions made by the sender prior to this one
console.log('TX block hash: ', tx.blockHash); // hash of the block where this transaction was in. "null" when transaction is pending
console.log('TX block number: ', tx.blockNumber); // number of the block where this transaction was in. "null" when transaction is pending
console.log('TX sender address: ', tx.from); // address of the sender
console.log('TX amount(in Ether): ', web3.utils.fromWei(tx.value, 'ether')); // value transferred in ether
console.log('TX date: ', new Date()); // transaction date
console.log('TX gas price: ', tx.gasPrice); // gas price provided by the sender in wei
console.log('TX gas: ', tx.gas); // gas provided by the sender.
console.log('TX input: ', tx.input); // the data sent along with the transaction.
console.log('=====================================') // a visual separator
}
}
} catch (err) {
console.error(err);
}
})
});
where
- EVM_ACCOUNT — the account address that you want to monitor.
- WSS_ENDPOINT — your node WSS endpoint.
See View node access and credentials.
Example
An example to monitor UniswapV2Router02 address:
var add = 'wss://ws-nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531'
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.WebsocketProvider(add));
const account = '0x7a250d5630b4cf539739df2c5dacb4c659f2488d'.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.to) {
if (tx.to.toLowerCase() === account) {
console.log('TX hash: ',txHash );
console.log('TX confirmation: ',tx.transactionIndex );
console.log('TX nonce: ',tx.nonce );
console.log('TX block hash: ',tx.blockHash );
console.log('TX block number: ',tx.blockNumber );
console.log('TX sender address: ',tx.from );
console.log('TX amount(in Ether): ',web3.utils.fromWei(tx.value, 'ether'));
console.log('TX date: ',new Date());
console.log('TX gas price: ',tx.gasPrice );
console.log('TX gas: ',tx.gas );
console.log('TX input: ',tx.input );
console.log('TX hash: ',txHash );
console.log('=====================================')
}
}
} catch (err) {
console.error(err);
}
})
});
You can also add the reconnect option to make sure you are always connected. See also web3.js reconnect documentation.
var options = {
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
}
};
var add = 'wss://ws-nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531'
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.WebsocketProvider(add, options));
const account = '0x7a250d5630b4cf539739df2c5dacb4c659f2488d'.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.to) { // This is the point you might be looking for to filter the address
if (tx.to.toLowerCase() === account) {
console.log('TX hash: ', txHash); // transaction hash
console.log('TX confirmation: ', tx.transactionIndex); // "null" when transaction is pending
console.log('TX nonce: ', tx.nonce); // number of transactions made by the sender prior to this one
console.log('TX block hash: ', tx.blockHash); // hash of the block where this transaction was in. "null" when transaction is pending
console.log('TX block number: ', tx.blockNumber); // number of the block where this transaction was in. "null" when transaction is pending
console.log('TX sender address: ', tx.from); // address of the sender
console.log('TX amount(in Ether): ', web3.utils.fromWei(tx.value, 'ether')); // value transferred in ether
console.log('TX date: ', new Date()); // transaction date
console.log('TX gas price: ', tx.gasPrice); // gas price provided by the sender in wei
console.log('TX gas: ', tx.gas); // gas provided by the sender.
console.log('TX input: ', tx.input); // the data sent along with the transaction.
console.log('=====================================') // a visual separator
}
}
} catch (err) {
console.error(err);
}
})
});
Additional information
See also Exploring the methods of looking into Ethereum’s transaction pool.
Whereas txpool.content provides pending transactions and transaction details on request, subscribing to pending transactions as described in this article displays them to you in real time as they come through your node.
Both txpool.content and subscribing to pending transactions as described in this article provide the same level of detail.
Compare
txpool.content
> txpool.content
0xfbd33bEA942bC7B05A157E93C36A76e28CC72F06: {
6: {
blockHash: null,
blockNumber: null,
from: "0xfbd33bea942bc7b05a157e93c36a76e28cc72f06",
gas: "0x125ef",
gasPrice: "0x773594000",
hash: "0x57ee22be57d57be3dffbe1fa84b51e30606dfb5e1eacd791747682f2e69f0756",
input: "0x095ea7b300000000000000000000000039aa39c021dfbae8fac545936693ac917d5e7563ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
nonce: "0x6",
r: "0x10fd45dd90404a555b8f83f29318f79c9de88e07d2e19a70e92ca0b0ad344ec",
s: "0x6c50631199aab6b96661adb57859e209235cad626bb56b6d6f4a344968564ad5",
to: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
transactionIndex: null,
v: "0x26",
value: "0x0"
}
},
subscribe("pendingTransactions")
# node subscribe.js
TX hash: 0x6aa09cee73f274dd2f89fdc8b9be35c6d3152bafd75e4d551ccc86e4a7de05ff
TX confirmation: null
TX nonce: 1529
TX block hash: null
TX block number: null
TX sender address: 0xC4855932fCbf11Cd3c11893eF90450F86b27979b
TX amount(in Ether): 0
TX date: 2020-11-09T09:41:33.316Z
TX gas price: 88000000000
TX gas: 100000
TX input: 0xa9059cbb0000000000000000000000001db481b097842b9d8604c80057f25bd4a825df5800000000000000000000000000000000000000000000000000000000006acfc0
TX Hash: 0x6aa09cee73f274dd2f89fdc8b9be35c6d3152bafd75e4d551ccc86e4a7de05ff
If you have any questions, feel free to contact Chainstack support by emailing support@chainstack.com or by submitting this form.
Comments
0 comments
Article is closed for comments.