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 (IBalanceSheetV1);

The Hifi balance sheet.


function getRepayAmount(
IUniswapV2Pair pair,
IErc20 collateral,
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 applied is:

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

Otherwise, the formula used is:

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

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


paircontract IUniswapV2PairThe Uniswap V2 pair contract
collateralcontract IErc20The address of the collateral 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