avataryuichiro aoki

Summary

The web content describes the process of building a flashloan bot on the Polygon network using the 1inch API and DODO, discussing the integration of trading protocols, the detection of arbitrage opportunities, and the challenges faced with DODO's trading interface and using 1inch API's call data.

Abstract

The article titled "Flashloan Bot on Polygon Part.1" outlines the creation of a simple flashloan bot that leverages the 1inch API for identifying the best trading routes and price data on Polygon. The author, Yuichiro Aoki, provides a GitHub repository for the flashloan bot and smart contract code, emphasizing the importance of detecting arbitrage opportunities before executing flashloans to avoid transaction fees on failed transactions. The article also addresses the complexities of integrating DODO protocol due to its unique trading interface and the inability to swap on the same pool from which the flashloan is borrowed, likely due to security concerns such as re-entrancy attacks. Additionally, the author explores the potential of using call data from the 1inch API to simplify the trading process within the flashloan contract but notes that the API call often fails, possibly because the flashloan contract does not initially hold the required token amount. The article concludes with additional resources and related readings on cryptocurrency trading and investing.

Opinions

  • The author believes that building a flashloan bot on Polygon is advantageous due to the abundance of Uniswap fork DeFi protocols, which simplifies the integration process.
  • There is an opinion that DODO protocol offers better trading prices, but its integration into a flashloan bot is complex due to the need for a specific swap function and trading direction logic.
  • The author suggests that using 1inch API's call data could streamline the trading process by offloading route calculations to 1inch contracts, but this approach is currently hindered by API call failures.
  • The author humorously questions the purpose of a flashloan if one already possesses sufficient funds for trading, highlighting the core utility of flashloans in leveraging capital.
  • The article implies that the 1inch API may perform a check to ensure the flashloan contract has enough tokens before executing a swap, which is not feasible given the nature of flashloans.

Flashloan Bot on Polygon Part.1

Let’s build a simple flashloan bot on polygon with 1inch API and DODO!

Photo by Marc-Olivier Jodoin on Unsplash

Today I’m going to show you a simple flashloan bot on polygon using 1inch api and DODO.

Flashloan Bot Repo

Flashloan Smart Contract

In the previous story, I talked about how to implement flashloan without flashloan fee. Here is the link of the previous story. You can find the smart contract code used in this flashloan bot.

Flashloan Smart Contract Repo

Detect Arbitrage Opportunities

It’s important to check price data and see if there’s an arbitrage opportunity before executing flashloan because you have to pay transaction fees for even failed transactions.

You can get price data on Defi protocols by executing view functions which costs nearly nothing because Infura and Alchemy have a generous free tier.

But, is there any way I get the best route on polygon easily??

1inch API

With 1inch API, you can get the best route and the price data for free. The flashloan bot gets the prices of each Uniswap fork and implements flashloan when it finds an arbitrage opportunity.

Why only Uniswap forks ?? Why not DODO??

Well I’m going to talk about that !

1inch API Document

Easy to Integrate

One of the great things about building a flashloan bot on polygon is that there’s so many Uniswap fork Defi protocols so that you can use the same interface, which is uniswap, for trading using smart contracts.

src/config.ts — trading protocols
src/contracts/Flashloan.sol — swapping function on uniswap fork

swapping function on Uniswap fork code

These protocols are a fork of Uniswap V2, so it’s pretty easy to integrate into the flashloan smart contract.

Issues

Swap on DODO

When I look at the trading route on 1inch UI, I noticed that DODO protocol usually offers better prices, so I wanted to integrate it into the trading protocols like the all Uniswap fork protocols.

However, DODO has their own trading interface like this. You cannot just copy & paste the protocol address like Uniswap fork protocols. You need to have a swap function of DODO on your flashloan smart contracts and some logic to calculate a trading direction used in the trading function on somewhere in the flashloan bot code.

Also, you cannot swap on the same pool you borrowed your flashloan from and probably that’s because of a security reason (re-entrancy attack). This increases the complexity of the flashloan bot logic.

For these reasons, I have not integrated DODO into the flashloan bot.

Using Call Data from 1inch API

You get call data from 1inch API which can be used in a trading bot so you can swap tokens with the best route that 1inch calculates off-chain.

1inch API — swap query params

1inch API Doc — Swap

I thought of using call data you receive from 1inch API on the flashloan smart contract. That way, the flashloan contract have to only borrow flashloan, execute the call data from 1inch API, and pay back the loan. You don’t have to think about the trading routes or writing each swap function for a specific Defi because 1inch contracts deal with it.

However, the API call for 1inch always fails for some reasons.

I assume 1inch API checks if fromTokenAddress, which is the flashloan contract address in this case, has enough the amount of the fromTokenAddresstoken in the background, and of course, the flashloan contract doesn’t have that amount of the token before calling flashloan function.

If you have enough amount of swapping tokens up front, then what’s the point of flashloan 😂

Hope this helps. Thank you for reading😁

Flahsloan Bot Code | Flashloan Smart Contract Code

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also Read

Blockchain
Ethereum
Polygon
Flash Loan
Bots
Recommended from ReadMedium