hifi-finance / hifi

Monorepo implementing the Hifi fixed-rate, fixed-term lending protocol
https://app.hifi.finance
Other
106 stars 15 forks source link

feat(flash-swap): support liquidating vaults with underlying as collateral #60

Closed scorpion9979 closed 3 years ago

scorpion9979 commented 3 years ago

Regarding liquidating USDC collateral, the following modifications needed to be applied to the flash-swap contract:

  1. The bot is not able to access a USDC-USDC trading pair, and so for this special case we are assuming the use of any pair that has USDC on one side. In real life, the pair would preferably be the one with the largest liquidity out of all pairs containing USDC on one side.
  2. Since we're only going to be using one side of the trading pair, we need to repay the borrowed USDC in USDC + 0.3% Uniswap v2 fee. This required modifying the getRepayCollateralAmount(...) function in flash-swap to enable different calculation for this special case.
  3. We also needed to do something about the msg.sender verification in flash-swap, as it previously required the msg sender address to be equal to the pair address of underlying and collateral computed using pairFor(...). Since it's not possible to have a USDC-USDC pair, we needed to constrain this check to the case where the underlying token is not the same as the collateral to be liquidated.
  4. In most cases, the liquidated collateral is not going to be enough to repay the 0.3% Uniswap fee. A modification was needed to enable the flash-swap contract to repay the fee from its own balance if necessary.
PaulRBerg commented 3 years ago

Unfortunately, I cannot approve of this. The HifiFlashUniswapV2 is bloated already - applying the modifications brought about about by this PR would add too much complexity in a single contract. That complexity would percolate through the tests and the liquidator bot.

After some research I did this morning, I think that Aave would be a better choice when repay a borrow that was made with underlying as collateral. As per this article that compares flash loan providers (Aave, dYdX, Uniswap), Aave works best when you don't want to make a swap and you need multiple currency options. dYdX is ok too but as far as I know it's not running on Polygon.

By writing an Aave flash loan implementation, we would: