Created
May 9, 2022 12:05
-
-
Save pawiromitchel/222c076203dd495a05561604e5750485 to your computer and use it in GitHub Desktop.
subscribe to pendingTransactions with web3js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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