Smart Contract Arbitrage — Bot
We Have The Parts, Now We Add The Smarts
Through the last several posts we have learned about flash borrows, how to write and deploy a smart contract, how to build an interface to interact with other contracts, how the Uniswap flash swap callback function works, wrote a simple 2-pool swap smart contract, improved its security checks and optimized the gas, and finally developed a method to quickly calculate the optimal flash borrow amount.
Quite a journey.
This post will tie it all together. We’ll build a bot that will monitor pools and send arbitrage execution calls to our deployed smart contract.
The bot structure is quite similar to the more traditional versions I’ve already presented. The bot needs to handle the following:
Monitor the current price of the tokens in question
Monitor all relevant liquidity pools
Calculate possible arbitrage pathways and amounts
When an opportunity is found, execute a transaction using our deployed smart contract, providing all relevant data that the contract needs to secure the arbitrage
Here is the rough pseudo-code for the bot —
[SET UP DATA STRUCTURES] - Create objects to represent tokens - Create objects to represent liquidity pools - Create objects to represent arbitrage pathways [MAIN LOOP] - Calculate relevant token values - Update all liquidity pools - Check for possible arbitrage - Call deployed smart contract for all arbitrage opportunities that exceed some threshold