
Want to profit from the Defi world? Check out the combination of Flash Loans and The Graph! By using these decentralized market tools, you can unlock a lucrative world of triangular arbitrage in just a few clicks. But where to start? Here, of course! We’ll explore the ins and outs of this strategy and provide enough material to get you started.
plan
Building a decentralized market data index for Ethereum mainnet using The Graph
Building an algorithm to discover and review triangular arbitrage opportunities
Use flash loans to build smart contracts and execute trades that take advantage of profitable triangular arbitrage opportunities.
prerequisites
Web3 terminology (liquidity pools, DEXs, AMMs, etc.)
Uniswaps CFMM (Constant Function Market Maker)
Graph Knowledge
Knowledge of smart contracts and flash loans
If you have a firm grasp of the above prerequisites, you can skip the next section. If you need a quick refresher, we will review some basics before we start building the algorithm.
What is triangular arbitrage?
Triangular arbitrage is the result of price differences, which occurs when three currency rates do not match. With regard to Defi (decentralized finance), these differences can be found between three separate liquidity pools (LPs) held on DEX (decentralized exchanges). We will focus on three separate DEXs, which will expand our network and capabilities to find greater differences in the entire Defi market.

Markets we will focus on
Uniswap V2, Uniswap V3, and Sushiswap are all DEXs that are similar in many ways, but here are three reasons why they are good for us in the long run.
They are all AMMs (Automated Market Makers) sharing the same CFMM (Constant Function Market Maker).
Both have similar subgraph modes hosted on The Graphs protocol
Every DEX has a fast and efficient way to perform flash loans.
What type of CFMM are we using?
Let’s quickly review Uniswaps CFMM. According to the Uniswap whitepaper, the following formula automatically sets the price within the LP for a given transaction.

CFMM
Ra is the reserve amount of Token A
Rb is the reserve amount of Token B
Δb is the input quantity (what you have)
Δa is the output (what you receive)
r is the included fee (each LP has a transaction fee, usually 3%), in this case r is expressed as r — 3%
Finally, k can be found as the product of the two reserves: Ra * Rb = k
By keeping the total reserves constant, we can easily break down this formula and determine how many tokens we will receive in a transaction. Pop Quiz!
Assume that in a liquidity pool, the token reserve of Token A is 5 and the token reserve of Token B is 20. If trader Ryan wants to exchange his 1 Token A for Token B, how many tokens will Ryan receive?
Breaking down the equation above, we can replace the following:

Next, we can simplify the equation and solve for Δb to get the final amount. But first, let’s remove the interchange fee from Ryan’s amount, which will give Ryan’s amount of 0.997.

Finally, Ryans will receive 3.324996 of token B and leave the liquidity pool with a new token reserve amount. If we reapply the CFMM formula, we will see that the result has not changed. This is why it is called a constant function market maker.
So, now that we’ve reviewed how to calculate the amount of tokens we will receive in a given trade, all we need is the liquidity pool data.
How do we get the data?
Introducing The Graph, an open source and decentralized indexing protocol for blockchain data. Developers can build and publish various APIs called "subgraphs" that perform queries on GraphQL. Currently, most DEXs do not support traditional API requests. Instead, most DEXs have subgraphs designed to query data found on their platform using standard GraphQL APIs.
To set this up, perform the following Axios post request to the Uniswap V3 subgraph,

This parameter will take a GraphQL query to index blockchain data in the Uniswap V3 subgraph. Don't worry, you don't have to build these queries entirely yourself. You can jump into the DEXs playground and test your own queries. Take the following Uniswap V3 subgraph query.
This indexes the Uniswap V3s protocol for 1000 liquidity pools. The resulting response will be an array of n objects, sorted by the volume traded by each pool in the given timeframe (note that some liquidity pools may have no trades).
What is a flash loan?
Similar to traditional loans, flash loans are funds borrowed with the expectation that they will eventually be repaid. However, they operate in a different capacity:
They are unsecured
They operate through smart contracts
They are instantaneous
I. Quick loans as unsecured loans
Unlike traditional loans, flash loans are an unsecured loan, which means that no collateral is required. Since flash loans are unsecured, they are highly risky for lenders. However, flash loans have a strict set of rules in the smart contract. These strict rules are the security provided, in short, it reverts the transaction if anything goes wrong.
2. Flash Loans in Smart Contracts In essence, smart contracts can be defined as electronic scripts that are automatically executed based on events when specified scenarios occur.
However, unlike building smart contracts, where you can set the terms for yourself, flash loans have predefined rules. One stipulation is that the borrower must repay the entire loan before the transaction is completed. For example, when a transaction is planned, if the funds are not returned in full to the initial lender at the end of the loan period, the loan will be considered invalid and the transaction will be reversed. The downside of this outcome is the transaction fees (gas fees) and exchange fees (swap fees) that are incurred.

3. Flash loans are instant
Getting approved for a traditional loan is often a lengthy process. Borrowers must submit documents, wait for approval, and repay the loan which can take days, months, or years.
Flash loans are executed instantly. The smart contract for the loan must be executed during the transaction period in which it is lent. Therefore, the borrower needs to call other smart contracts to perform instant transactions using the borrowed funds.

