How I Learned to Stop Worrying and Love the Mempool
Psssst! Hey, wanna see some pending transactions?
This lesson builds heavily on the previous lesson covering Asynchronous Websocket Listeners, so please work through that one again if you have not already.
It’s fine for traditional trading to simply monitor the latest state of the blockchain and occasionally execute a swap when market conditions match your desired threshold. However if you’re an MEV cowboy attempting to capture same-block arbitrage opportunities, you need access to the mempool.
The mempool is a shared virtual area where unconfirmed transactions are held before being permanently recorded onto the blockchain. Transactions enter the mempool at some node boundary, propagate out until they are found and processed by a miner, and then dropped from the mempool.
On Ethereum (and some other EVM-compatible blockchains like Fantom), the mempool is publicly accessible. This makes it a target for MEV bots, since they have access to two useful types of information:
The current state of the blockchain (pool reserves, asset prices, historical gas fees, address balances, etc.)
A pool of pending transactions that can alter future blockchain states in a predictable way
The beauty of blockchain technology is that all transactions are deterministic. A given input will result in the same output, regardless of the machine that executes it. This means that when I submit a
swapExactTokensForTokens() call at the UniSwap router address, it will execute the smart contract code in the same way, every time, forever.
If you have access to the blockchain’s current state, plus all of the pending transactions that could alter it, you can apply some simple heuristics to the mempool and identify transactions that might result in a favorable blockchain state in the future.
This lesson will take a specific function from the websocket lesson,
pending_transactions(), expand it to illustrate how you can monitor the mempool for transactions that benefit you, then use that to make decisions.