\ʕ•ᴥ•ʔ/
\ʕ•ᴥ-ʔ/
Alexzander Stone

Source: Alex Stone
Finding (and completing) arbitrage opportunities within a 13~ sec time.
2023-09-11 : ethereum, defi, solidity, hardhat, python, node.js, microservices, postgresql
The why
On Ethereum, numerous decentralized exchanges (DEXs) are constantly emerging, with new tokens being added daily. Since each DEX operates independently, this results in price disparities. The same token pair can exhibit significantly different rates across various DEXs.
As is characteristic of a free market, these rate differences eventually get corrected due to the profit potential in swapping tokens from lower-rate DEXs to higher-rate ones. This phenomenon is known as arbitrage, with one of its simplest applications being triangular arbitrage within DEXs.
Source: OKX twitter
Assuming one can identify profitable rates that exceed the gas costs associated with the swaps on each platform, consistent profits can be realized.
Source: 7tv: BOARDA emote - OHBENTLEY
Thus, the challenge can be distilled into three key components:
- Identifying the most profitable path of swaps based on the current state of the blockchain.
- Minimizing costs (gas expenses, slippage).
- Submitting the TX for the chosen path of swaps within a ~13-second window.
My (partial) solution
Source: Vecteezy - moopick
In this discussion, I will focus on the first and third problems: determining the most profitable path within 13 seconds.
(The second problem pertains to Solidity coding, which may not be as intriguing but remains crucial.)
To increase the likelihood of finding a profitable path, we aim to have as many token pairings as possible, all while ensuring the efficiency of our algorithm. The 13-second timeframe encompasses not only our pathfinding algorithm but also:
- Normalizing the pair data.
- Checking for token pair vulnerabilities.
- Identifying an optimal principal amount.
- Submitting the transaction.
Regrettably, I won’t delve into the details of my algorithm’s implementation, as I intend to keep it closed-source. I suggest starting your research in the realm of general pathfinding.
However, I will provide an interactive section below for you to witness my work in action!
The algorithm will consider paths of any length, and, more importantly, isn’t limited to a depth.
Additionally, it’ll only consider one cycle at a time (eth -> !eth* -> eth).
Interactive Demo
- Update rate (x To Tokens received per 1 From Token) to add arbitrage opportunities.
- The starting token is "1 eth".
- Each swap will incur a 0.01 eth cost fee.
Total Pairs/Pools/Edges: 5566
Start Time: waiting ---- End Time: waiting
Total Elapsed Time (seconds): waiting
Profitable Route: Waiting for calculation...
This is ran entirely on a tiny, weak cloud instance!
WARNING: This is not a production ready API, so please don’t try to use it for alpha. There’s zero notion of slippage, ‘exploits’, …, …, and more.
Tech and knowledge used
Source: ethereum.org
-
Ethereum and DEXs: Ethereum was selected as the blockchain of choice due to its high market capitalization of token pairs within DEXs.
-
Solidity: Enabling routes involving multiple DEXs with n swaps necessitated custom Solidity code. The code has been highly optimized for gas efficiency to enhance the probability of identifying profitable routes.
-
Hardhat: Hardhat was employed for testing the viability of routes, ensuring they were not merely deceptive.
-
Node.js and PostgreSQL: Node.js microservices and PostgreSQL triggers played a pivotal role in the project’s infrastructure.
-
Python: Python, supplemented with C++ modules, was indispensable for mission-critical tasks, specifically for locating swaps within the 13-second window.
What’s Next
Two potentially profitable paths (haha) can be envisioned:
- The introduction of mempool considerations to capitalize on pool/pair rate changes swiftly.
- The utilization of this approach by a DEX aggregator.
For now, I’ll take some time to reflect further on these possibilities.