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)
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.
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.
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
To accomplish this, we can create a simple smart contract that wraps around the Uniswap V3 LP exiting function. The contract will use the Uniswap V3 subgraph to gather information about the user's position, including the tokens and price at the time of providing liquidity, as well as the current tick in the pool.
The contract will use the Impermanent Loss Calculation formula provided in the link to continuously monitor the impermanent loss incurred for the user's position. If the loss crosses a predetermined threshold (such as 50%), the contract will trigger the LP exiting function to exit the position and recover the user's funds.
Once the position is exited, the contract can then convert the tokens back to USDC at the current market price and deposit them into the user's wallet. This entire process is automated and does not require the end user to be online to sign any transactions.
simple smart contract that wraps around Uniswap V3's LP exiting function and triggers it when the impermanent loss for a position reaches a certain threshold:
This contract can be set up to run the monitor() function on a regular interval, such as every hour, to continuously check for any positions that have reached the impermanent loss threshold. Once the position is exited, the user can use other smart contracts or DEXs like Uniswap to convert their tokens back to USDC at the current market price and deposit them into their wallet.
Here's how the monitor function can be completed using the provided impermanent loss calculation formula:
This function retrieves the position's data from the UniswapV3 subgraph and uses it to calculate the impermanent loss. If the loss is above the threshold of 50%, the function will trigger the LP exiting function to exit the position.
It's worth mentioning that the function need to be executed with a wallet connected that have the authority to call the exit function on the UniswapV3 contract.
There are a few ways to convert all the tokens back to USDC at the current market price and deposit them into the user's wallet after exiting liquidity, here is one example of how this can be done using Uniswap:
Create a new smart contract that acts as an intermediary between your main contract and Uniswap, this smart contract will have a function that takes the token address, the amount and the user's wallet address as inputs.
The intermediary contract will then interact with Uniswap's exchange contract to exchange the tokens for USDC at the current market price. This can be done by calling the eth_sendTransaction method in web3.js, passing in the address of the Uniswap exchange contract, the token address, the amount, and the user's wallet address as inputs.
Once the exchange is done, the smart contract will transfer the USDC to the user's wallet address by calling the transfer function in the ERC20 USDC contract passing in the user's wallet address and the amount as inputs.
The main contract will call the above function in the intermediary contract passing in the necessary inputs and execute it with a transaction coming from the user's wallet.
It's worth noting that this is just one example, there are other ways to interact with Uniswap and other DEXs to exchange tokens for USDC and transfer it to the user's wallet address. Additionally, you might consider to use a ready-made solution like Chainlink oracles to acquire the exchange rate of the tokens with USDC.
Here's an example of how the intermediary contract might look like:
This contract uses Uniswap's factory contract to create a pair with the token and USDC, then call the swap function of the pair, passing the amount of token to exchange and the user's address as the recipient of the USDC.
After that the contract transfer the USDC to the user's wallet address by calling the transfer function of the ERC20 USDC contract, passing the user's address and the amount as inputs.
It's worth mentioning that this is just a basic example, there are different ways to interact with Uniswap and other DEXs, and you might want to add more functionality like input/output validations, error handling and also use a ready-made solution like Chainlink oracles to acquire the exchange rate of the tokens with USDC.