Skip to content

Instantly share code, notes, and snippets.

@ramkumarvenkat
Last active January 28, 2023 14:45
Show Gist options
  • Save ramkumarvenkat/c37534ceab91175708a7e529af100adf to your computer and use it in GitHub Desktop.
Save ramkumarvenkat/c37534ceab91175708a7e529af100adf to your computer and use it in GitHub Desktop.

The goal is to

  • Continuously monitor the impermanent loss incurred for a position in Uniswap V3
  • And if there is a loss, we need to exit the position
  • It has to be 100% automated (the end user should not be expected to be online signing the transaction when the liquidity is pulled out)

Monitoring a position

We can use the Uniswap V3's subgraph to know a position's details like tokens, price at the time of providing liquidity, etc. We will also know the current tick in the pool.

We can use the following Impermanent Loss Calculation.

When the loss crosses a threshold (like say 50%), we need to trigger the recovery process.

Exiting a position

We need to deploy a simple smart contract that wraps around Uniswap V3's LP exiting function and trigger this function when the above monitoring logic is executed. The output of this should result in the wallet having the tokens back with the position fuly exited.

Bonus

After exiting the liquidity, it will be great if all the tokens are converted back to USDC at the current market price and deposited into the user's wallet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment