Automating Uniswap v3 limit orders using Chainlink Keepers

This is my first story on medium, so wish me luck.

With the launch on Uniswap v3, the DEFI community got a lot of opportunities that Uniswap v2 was not able to provide mainly because of how the liquidity was managed.

CONCENTRATED LIQUIDITY

Uniswap v3 brings a new feature on the table, namely the possibility for the users to:

  • Select different fees when providing liquidity to the pools (0.05 %, 0.3% and 1%).
  • Select a price range where they want to provide their liquidity for, known as concentrated liquidity.

We will mainly focus on the second feature here and the ability to provide concentrated liquidity for pairs in a pool.

Every Uniswap v3 pool is identified by the 2 tokens in the pool (known as pairs) and the fee selected for the pool (0.05 %, 0.3% or 1%).

Every Uniswap v3 pool has a current / market price, defining the price between the 2 tokens in the pool.

When adding liquidity to the Uniswap v3 pool, user can (in the given order):

  • select the pairs (pool) and the fees. This is the Uniswap v3 pool identifier.
  • select the target price range (min-max) for providing the liquidity. The users can usually select a price range left (lower) or right(higher) than the current price.
  • deposit the amounts of the 2 tokens in the pool according to the price range selected.

Please note that target price range consist of min — max target prices.

Lets discuss about the target price range selection and the rules for it:

  • If the target price range is including the current price, the user needs to deposits amounts for the both tokens. This deposit will be immediately used (during swaps), since its price range is within the current price.
  • If the target price range is higher than the current price, the user can only deposit one of the tokens i.e. the first token of the pool. This deposit will only be used when the current price moves within this price range.
  • If the target price range is lower than the current price, the user can only deposit one of the tokens, i.e. the second token of the pool. This deposit will only be used when the current price moves within this price range.

The second and the third point are known as range orders in Uniswap V3.

LIMIT ORDERS

Limit orders are used in the crypto world whenever users:

  • want to SELL a token at a target price, usually when the price is higher than the current/market price, for example sell 1 ETH for USDC whenever the price reaches 4000 USDC/ETH.
  • want to BUY a token at the given price, usually when its price is lower than the current/market price, for examples buy 1 ETH with USDC whenever the price reaches 3000 USDC/ETH.

This is a well known investment strategy: buy low, sell high.

Those 2 mentioned cases of buy and sell limit orders are exactly what can be implemented using Uniswap v3 range orders by selecting a thin target price range.

HOW IT WORKS ?

Uniswap v3 automatically process your limit orders whenever the current / market price reaches and surpasses your selected THIN target price range.

Once the current /market price moves on the other side of your THIN target price range, your tokens are considered sold (in case of a sell limit order) or bough (in case of a buy limit order).

Sell Limit Order

User places sell limit orders for a given token pair on Uniswap v3 if they select a THIN price range higher than the current price of the pool.

Sell 1 ETH for USDC whenever the price reaches 4000 USDC/ETH. Note the min and max price are close to each other, this is what is referred as THIN price range.

ETH/USDC sell limit order on Uniswap v3

Once the current price moves ABOVE the max price of 4048 USD per ETH, your ETH tokens will be sold (automatically by Uniswap v3) for USDC.

You will get the 4048 USDC amount from selling the ETH, as WELL as any additional fees earned from Uniswap v3 for providing the liquidity. That means you are getting slightly more USDC than what you placed the sell order for, which is better than if you were to swap ETH to USDC directly.

Buy Limit Order

User places buy limit orders for a given token pair on Uniswap v3 if they select a THIN price range lower than the current / market price of the pool.

Buy ETH with 3000 USDC whenever the price reaches 3000 USDC/ETH. Note the min and max price are close to each other, this is what is referred as a THIN price range.

ETH/USDC buy limit order on Uniswap v3

Once the current price moves BELOW the min price of 2981 USD per ETH, your USDC tokens will be sold (automatically by Uniswap v3) for ETH.

You will get the 1 ETH amount from selling the 3000 USDC, as WELL as any additional fees earned from Uniswap v3 for providing the liquidity. That means you are getting slightly more ETH than what you placed the buy order for, which is better than if you were to swap USDC to ETH directly.

THE PROBLEM ?

This feature is perfect, however there are 2 problems with it:

  • The user needs to MANUALLY collect and withdraw the amounts obtained on Uniswap v3 using the limit order.
  • Most important, the user needs to do this RIGHT AFTER the current / market price surpasses your target price range.

Of course the user can sit, monitor, wait 24/7 and do it on their own. But why do that, when all of this can be automated.

This is where Chainlink keepers comes into place.

CHAINLINK KEEPERS

Next chapter would be related to how to configure Chainlink keeper to automatically monitor the limit orders and how to cover its upkeep cost from the fees charged to the end users.

To give you a prelude, find below a code snippet to give you a hint on the next chapter topic.

ChainlinkOrderMonitor → performUpkeep

Sounds good enough for you to use such a remarkable feature on Uniswap v3 ? Please contract me and help make this DAPP a success. We are planning to deploy our first version of the DAPP on Layer2 (Arbitrum ONE) utilizing the lower cost for managing limit order on Uniswap v3 on Arbitrum..

Interested in #Crypto projects, #ETH, #DeFi.