Building a Trading Bot with 1inch API on Polygon

Let’s build a simple trading bot on polygon with 1inch API!

yuichiro aoki

--

Photo by Maxim Hopman on Unsplash

Today I’m going to show you how to build a simple trading bot on polygon using 1inch api.

1inch api

With 1inch api, you can get the latest rates on 1inch, trade, and get some tokens information. It’s all free and easy to use, and has an example in the docs. Supported networks are as follows:

  • Ethereum
  • Binance Smart Chain
  • Polygon
  • Optimism
  • Arbitrum

For more

The repo I use in the demo is here.

Let’s Get Started😁

Strategy

This bot trade based on the price change from the last buy/sell action. First, set the base price to the current price, and update it when the bot executes trade. Here is an example of how this would trade when trading Matic and DAI. It sets the threshold as 3%.

example

Get Price on 1inch

You can get price on 1inch by sending a get request with addresses of a token pair, the amount you want to trade, and the chain id of network.

get price (src/inchPrice.ts)

Trade on 1inch

Trading on 1inch is interesting because it routes many Defi protocols during the trade and prevents the risk of front-running.

Using 1inch api, you can get this benefit.

Get calldata for approve transaction

First, you send a get request and get calldata for an approve transaction. You need this step before you trade except native token like Matic on polygon.

--

--