Code Pack - mempoollistener

Listen To and Decode DEX Transactions in the Ethereum Blockchain Mempool

Running your own Ethereum node or want to listen to transactions before they happen on the blockchain

Technologies
javascript
Themes
crypto
trading
frontrunning
flashloans
Skill Level
intermediate
Last updated
2023-02-12

What's Inside

As seen on YouTube near the end of this video:

https://youtu.be/uElOqz-Htos.

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.

Uses

-
Connect to the blockchain quickly and easily
-
Listen to transactions taking place in the pending transactions 'mempool'
-
Decode Uniswap trades and see the code which physically does this
-
Just use Javascript. No Solidity, Typescript etc here. Perfect for newbies looking for a challenge.

Requirements

-
You must have node installed on your machine.
-
You must also be comfortable using the CLI (command line interface) on your machine.

START

What you need to get running

Below is the basic software you will need to get started:

Node(developed with version: 18.11.0)
npm(developed with version: 10.9.0)
Yarn(developed with version: 1.22.19)

Step 1

Download package

Click the download button at the top of this page. Then save the project on your desktop of project folder. Going forward, we will assume a project name as 'myproject'.
However, you may of course name the project folder whatever makes sense for your purpose.

Step 2

Install packages

In your terminal, cd into your project.

~

cd myproject

Then install the exact same packages using yarn (you can use npm instead if you prefer)

~/myproject

yarn --exact

Step 3

Environment variables

In your terminal, cd into your project

~/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/

Step 4

Run the code

~/myproject

yarn start

CONGRATULATIONS

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.