Skip to main content

Flash Uniswap V2

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

Constant Functions


function balanceSheet() external view returns (IBalanceSheetV2);

The BalanceSheet contract


function getRepayAmount(
IUniswapV2Pair pair,
IErc20 underlying,
uint256 underlyingAmount
) external view 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.


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

Return Values

repayAmountuint256The minimum amount that must be repaid


function uniV2Factory() external view returns (address);

The address of the UniswapV2Factory contract.


function uniV2PairInitCodeHash() external view returns (bytes32);

The init code hash of the UniswapV2Pair contract.



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

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


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.
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


error FlashUniswapV2__CallNotAuthorized(address caller);

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


error FlashUniswapV2__FlashBorrowCollateral(address collateral, address underlying);

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


error FlashUniswapV2__LiquidateUnderlyingBackedVault(address borrower, address underlying);

Emitted when liquidating a vault backed by underlying.


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.


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

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