Last week we laid out the first few bricks for our bot and we will continue building on top this week. Recall that our goal is to find arbitrage opportunities and arbitrage can arise in many contexts.
In the first week, we've seen simple arbitrage on a single pool and we've seen that finding such opportunity amount to finding unbalanced pools. However, finding such pools can be a bit tricky because we need an oracle to compare prices.
However, there is something interesting we can do ! Instead of considering just single swap, we will consider multi-swap that can go through multiple pools. This has many advantages, first it will allow us to find arbitrage opportunity for token that we don't necessarily own but that we could get if we exchanged some other token that we do have. More specifically, suppose there is an arbitrage
Actually, for any two tokens that have several pools available, we can estimate their price and use that value as the oracle. The accuracy and variance of the value will depend on the amount of pools available as well as the depth of their liquidity but in general this is feasible. This approach allow us to get rid of oracle for most scenarios.
Following what has just been said, we are going to separate the process into two stages, first generate the paths and then finding arbitrages using optimisation algorithms.
Recall that by definition our graph is a undirected graph, i.e edges can be taken in any direction. To generate all the paths from to token, we will start from the all the pools that have as token, the token
However, there are a few subtleties to take into account.
Is is possible to have a path
Is it possible to have a path with an arbitrage opportunity
Suppose that you have an arbitrage opportunity on a path
So using the observations made with the exercise above, let's write our path generation algorithm !
On the
route(from, to) -> RouteGraph // Generate all the paths starting at from and ending at to
Where
RouteGraph = [Route(uuid, DirectedPool, RouteGraph)]
DirectedPool = (From, To, Pool)
We propose to assign to each node of the route a unique index (uuid) that will be useful when we are going to perform the optimisation. We propose to simply assign consecutive numbers starting from 0 as the uuid.
Congrats, you should now be able to fetch the pools from the database, initialize a graph and generate valid