A bot that allows users to set stop-losses on the value of an existing debt and collateral, which will automatically self-liquidate using AAVE flashloans and the 1Inch DEX when the user-defined threshold is reached.
The Watchful Eye is an app that includes a bot (the Oracle), a smart contract (The Watchful Eye), and an interactive dashboard. The dashboard allows a user to see some details about their existing positions in the AAVE protocol, and further specify the details of the loan position that they wish to have watched over by the Watchful Eye. Currently, the user has the option to set their debt and collateral assets, the amounts of each they wish to repay/liquidate in the flashloan, and the price limits that they wish to execute a liquidation at. In the future, we'd like to have the dashboard pre-populated with the details from Aave and other popular liquidity pools. The bot continuously monitors the smart contract for a user inputted loan position. If there is a position stored in the smart contract, the bot compares the prices of the debt and collateral assets to the prices limits stored in the smart contract. If the current price meets or exceeds the debt and collateral price limits in the smart contract, then the Oracle calls upon the smart contract to liquidate the loan/debt that the user specified previously. The smart contract then requests a flashloan from an Aave lending pool. The asset that is requested is the same asset that the user specified in their initial set up (the one to pay off their existing loan). When the contract gets the funds from the lending pool, it first repays the existing debt, then swaps the collateral (which is an aToken) to it's underlying asset (Ex: aLink -> Link). Finally, the contract swaps the asset to the flashloaned asset using the 1Inch DEX and pays back the flashloan, sending the remaining funds back to the user who's debt is being paid off. Here's a quick word-diagram to explain what transfers happen in the smart contract: Aave Lending Pool - Debt --> The Watchful Eye The Watchful Eye - Debt --> Repay user's debt User - aToken Collateral --> The Watchful Eye (Uses transferFrom) The Watchful Eye - aToken Collateral --> Aave Lending Pool
How it's made
Scaffold-eth on the front end allows us to use a bunch of useful components. It also came with a good setup for deployment. Metamask, hardhat's deployment, the client, and the Watchful Eye oracle are all connected to a local mainnet fork. Initially, we couldn't get the external contracts to work (1Inch and Aave), so we started with some mock contracts that simulated those actions we would take with those. However, after moving to the local mainnet fork, we were able to move to use the mainnet Aave and 1Inch protocols! :) When a flashloan is made, we repay the users debt on Aave, swap the aToken collateral to it's underlying asset using Aave, then swap that asset for the flashloaned asset. It's an extra step in the swap, but we had to swap the aToken -> reserve before doing the swap, since 1Inch doesn't yet support v2 aTokens. One particular hacky part of our app is that we had to make a deposit and borrow on Aave before using the Watchful eye, since we forked from mainnet and didn't have any previous mainnet positions on Aave. (Grrrr gas fees :() Another particular hack was that we needed to replace the idea of the Oracle with a centralized always running script. Since the team all started off with zero experience in ethereum development, we eventually figured out that it was too much to take on the smart contract programming and the Oracle programming. However, if we had another week we believe we could have gotten that to work too. :)Technologies used