Delphi and the Ethereum Dark Forest
This article describes what the Ethereum mempool is, why you should care, and how to monitor the Ethereum mempool from a Delphi app or service.
Before Ethereum transactions end up in a block, they are sitting in the mempool, waiting for miners to pick them up. Miners will pick transactions with the highest gas price first. In other words: the higher the gas price, the faster your transaction will get mined and the lower the gas price, the slower your transaction will get mined.
Let’s go back to the mempool itself for a moment. This is where pending, unconfirmed transactions are sitting. There is no guarantee these transactions will eventually end up in a block and there is no consensus about them.
Arbitrage bots monitor pending transactions and attempt to exploit profitable opportunities created by them. That is why the mempool is scary shit and sometimes referred to as the dark forest.
When an arbitrage bot copies your transaction with a higher gas price while replacing addresses with their own, we refer to this as “front-running”. Arbitrage bots typically look for specific types of transactions in the mempool (such as a trade or a swap) and try to frontrun them according to a predetermined algorithm.
But there could be other — legitimate — reasons to monitor the mempool. Your understanding of the Ethereum blockchain isn’t complete if you have never looked inside the mempool. In this article, we will write a simple “hello mempool” app using Delphi and Blocknative’s mempool explorer.
Delphereum is a web3 implementation for the Delphi programming language that includes a wrapper for the Blocknative WebSocket API.
Let’s get started. Before we begin, we need to clone a few GitHub repositories.
git clone https://github.com/svanas/DelphiBigNumbers
git clone https://github.com/Xor-el/SimpleBaseLib4Pascal
git clone https://github.com/Xor-el/HashLib4Pascal
git clone https://github.com/Xor-el/CryptoLib4Pascal
Assuming you have cloned the above dependencies on your system, now is the time to clone Delphereum, a web3 implementation for the Delphi programming language:
git clone https://github.com/svanas/delphereum
Launch Delphi, and then start a new project. Please make sure the dependencies are in your search path. Drop a Button component and a Memo component on your newly created main form. Then double-click on the Button component, and add the following code to the OnClick event handler:
Let’s break it down. When you subscribe to Blocknative’s mempool events, you begin with specifying what network you will want to monitor. In the above example, we will monitor the Ethereum main net.
Then you’ll need to specify what address to watch. You will probably want to monitor your own wallet address but nothing prevents you from monitoring someone else’s wallet or a smart contract. For the purpose of this demo, we will monitor USDC because it is one of the top 10 tokens on the Ethereum blockchain.
Subscribe() expects 3 closures. The 1st closure will receive Blocknative’s mempool events (or an error). This is where we are extracting the transaction from the event, and if we have any, add it to the Memo component we dropped on our form earlier.
The 2nd closure will receive non-Blocknative errors (probably a socket error) and the last closure is called when the WebSocket is disconnected. You are probably less interested in these other two closures compared to the first one. But if you want to re-subscribe to mempool events in case of an error, this is where to do it.
One more thing. Should you want to unsubscribe from the events, then the following line of code does that for you. Please note how we are repeating the USDC contract address on the Ethereum main net here.
Are you ready for a firehose? Start your Delphi project and click on that button.
Congratulations! You are now monitoring any live transactions involving USDC on the Ethereum network before they happen.