Skip to content

Instantly share code, notes, and snippets.

@pawiromitchel
Created May 9, 2022 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawiromitchel/222c076203dd495a05561604e5750485 to your computer and use it in GitHub Desktop.
Save pawiromitchel/222c076203dd495a05561604e5750485 to your computer and use it in GitHub Desktop.
subscribe to pendingTransactions with web3js
const Web3 = require('web3');
const RPC = 'wss://xxxx';
const options = {
timeout: 30000, // ms
headers: {},
// Enable auto reconnection
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
}
};
const web3 = new Web3(RPC, options);
web3.eth.subscribe('pendingTransactions')
.on("connected", function (subscriptionId) {
console.log('connected to WSS ID:', subscriptionId);
})
.on("data", async function (transaction) {
if (transaction) {
await web3.eth.getTransaction(transaction).then(tx => {
if(tx) {
console.log(tx.hash);
} else {
console.log('FAILED TO GET DATA');
}
})
} else {
console.log(`FAIL`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment