Skip to main content

Flash Uniswap V2

Integration of Uniswap V2 flash swaps for liquidating underwater accounts in Hifi.

Constant Functions

balanceSheet

function balanceSheet() external returns (contract IBalanceSheetV2)

The BalanceSheet contract.

getRepayAmount

function getRepayAmount(
contract IUniswapV2Pair pair,
contract IErc20 underlying,
uint256 underlyingAmount
) external returns (uint256 repayAmount)

Calculates the amount of that must be repaid to Uniswap. When the collateral is not the underlying, the formula used is:

repayAmount=(collateralReservesโˆ—underlyingAmount)โˆ—1000(underlyingReservesโˆ’underlyingAmount)โˆ—997repayAmount = \frac{(collateralReserves * underlyingAmount) * 1000}{(underlyingReserves - underlyingAmount) * 997}

Otherwise, the formula used is:

repayAmount=underlyingAmountโˆ—1000997repayAmount = \frac{underlyingAmount * 1000}{997}

See "getAmountIn" and "getAmountOut" in UniswapV2Library. Flash swaps that are repaid via the corresponding pair token are akin to a normal swap, so the 0.3% LP fee applies.

Parameters

NameTypeDescription
paircontract IUniswapV2PairThe Uniswap V2 pair contract.
underlyingcontract IErc20The address of the underlying contract.
underlyingAmountuint256The amount of underlying flash borrowed.

Return Values

NameTypeDescription
repayAmountuint256The minimum amount that must be repaid.

uniV2Factory

function uniV2Factory() external returns (address)

The address of the UniswapV2Factory contract.

uniV2PairInitCodeHash

function uniV2PairInitCodeHash() external returns (bytes32)

The init code hash of the UniswapV2Pair contract.

Events

FlashSwapAndLiquidateBorrow

event FlashSwapAndLiquidateBorrow(
address liquidator,
address borrower,
address bond,
uint256 underlyingAmount,
uint256 seizeAmount,
uint256 repayAmount,
uint256 subsidyAmount,
uint256 profitAmount
)

Emitted when a flash swap is made and an account is liquidated.

Parameters

NameTypeDescription
liquidatoraddressThe address of the liquidator account.
borroweraddressThe address of the borrower account being liquidated.
bondaddressThe address of the hToken contract.
underlyingAmountuint256The amount of underlying flash borrowed.
seizeAmountuint256The amount of collateral seized.
repayAmountuint256The amount of collateral that had to be repaid by the liquidator.
subsidyAmountuint256The amount of collateral subsidized by the liquidator.
profitAmountuint256The amount of collateral pocketed as profit by the liquidator.

Custom Errors

FlashUniswapV2__CallNotAuthorized

error FlashUniswapV2__CallNotAuthorized(address caller)

Emitted when the caller is not the Uniswap V2 pair contract.

FlashUniswapV2__FlashBorrowCollateral

error FlashUniswapV2__FlashBorrowCollateral(address collateral, address underlying)

Emitted when the flash borrowed asset is the collateral instead of the underlying.

FlashUniswapV2__LiquidateUnderlyingBackedVault

error FlashUniswapV2__LiquidateUnderlyingBackedVault(address borrower, address underlying)

Emitted when liquidating a vault backed by underlying.

FlashUniswapV2__TurnoutNotSatisfied

error FlashUniswapV2__TurnoutNotSatisfied(uint256 seizeAmount, uint256 repayAmount, int256 turnout)

Emitted when the liquidation either does not yield a sufficient profit or it costs more than what the subsidizer is willing to pay.

FlashUniswapV2__UnderlyingNotInPool

error FlashUniswapV2__UnderlyingNotInPool(contract IUniswapV2Pair pair, address token0, address token1, contract IErc20 underlying)

Emitted when neither the token0 nor the token1 is the underlying.