Whether you are a complete programming beginner or a super advanced blockchain professor, this code can be useful for everyone.
Simply put, this function will allow you to listen to transactions on the blockchain before they happen.
Indeed, you heard this correctly. In case you are not aware, you can actually watch transactions on the blockchain before they get embedded into blocks. This is how traders become victim to front running attacks. However, this ability can also be used for good. For example, this code will enable you to filter on live transactions that traders are looking to make on Uniswap.
If you are a trader, this can be immensely useful. Imagine having your own set of code telling you whether a trade could go against you, before it does. Imagine again if we could do that in the world outside blockchain. Well, we can’t, but in blockchain you can.
Nothing major here though, just a small snippet of code that helps you listen for transactions in the mempool.
Download package
Install packages
~
cd myproject
Then install the exact same packages using yarn (you can use npm instead if you prefer)
~/myproject
yarn --exact
Environment variables
~/myproject
touch .env
Now add your environment variables .env file (you can access this via VS Code or by typing sudo nano .env)
PROVIDER_HTTP = ENTER YOUR PROVIDER HTTP URL HERE
PROVIDER_WSS = ENTER YOUR PROVIDER WSS URL HERE
You can get a free url by signing up to a provider, for example at
https://www.alchemy.com/Run the code
~/myproject
yarn start
Finished
Your code is now running. It may take some time for a transaction to show (this depends on how busy Uniswap is at the time you are running this). If unsure what is going on check the video above.