Skip to main content

UniswapFlashLiquidation

Git Source

Inherits: IFlashSwapCallback

Author: G9 Software Inc.

This contract uses a flashswap on a PoolTogether V5 LiquidationPair to swap yield for prize tokens on Uniswap V3 and then contributes the prize tokens to the prize pool while sending any excess to the receiver as profit.

State Variables

quoter

Uniswap V3 Static Quoter

IUniswapV3StaticQuoter public immutable quoter;

router

Uniswap V3 Router

IV3SwapRouter public immutable router;

Functions

constructor

UniswapFlashLiquidation constructor.

constructor(IUniswapV3StaticQuoter quoter_, IV3SwapRouter router_);

Parameters

NameTypeDescription
quoter_IUniswapV3StaticQuoterThe Uniswap V3 Static Quoter to use to quote swap prices
router_IV3SwapRouterThe Uniswap V3 Swap Router to use for swaps

flashLiquidate

Liquidate yield via the LiquidationPair and swap _amountOut of tokenOut in exchange of _amountInMax of tokenIn. Any excess in tokenOut is sent as profit to _receiver.

Will revert if block.timestamp exceeds the _deadline.

Will revert if the tokenIn profit is less than _profitMin.

function flashLiquidate(
ILiquidationPair _liquidationPair,
address _receiver,
uint256 _amountOut,
uint256 _amountInMax,
uint256 _profitMin,
uint256 _deadline,
bytes calldata _path
) public returns (uint256);

Parameters

NameTypeDescription
_liquidationPairILiquidationPairAddress of the LiquidationPair to flash liquidate against
_receiveraddressAddress that will receive the liquidation profit (i.e. the amount of tokenIn in excess)
_amountOutuint256Amount of tokenOut to swap for tokenIn
_amountInMaxuint256Maximum amount of tokenIn to send to the LiquidationPair target
_profitMinuint256Minimum amount of excess tokenIn to receive for performing the liquidation
_deadlineuint256The timestamp in seconds by which the flash liquidation must be executed
_pathbytesThe Uniswap V3 path to take for the swap

Returns

NameTypeDescription
<none>uint256The amount of tokenIn in excess sent to _receiver

flashSwapCallback

function flashSwapCallback(address, uint256 _amountIn, uint256 _amountOut, bytes calldata _path) external;

findBestQuoteStatic

Finds the biggest profit that can be made with the given liquidation pair and swap path.

SHOULD be called statically, not intended for onchain interactions!

function findBestQuoteStatic(ILiquidationPair _liquidationPair, bytes calldata _path)
external
returns (ProfitInfo memory);

Parameters

NameTypeDescription
_liquidationPairILiquidationPairThe pair to liquidate
_pathbytesThe Uniswap V3 swap path to use

Returns

NameTypeDescription
<none>ProfitInfoThe profit info for the best swap

getProfitInfoStatic

Calculates the profit point at the given amount out.

SHOULD be called statically, not intended for onchain interactions!

function getProfitInfoStatic(uint256 _amountOut, ILiquidationPair _liquidationPair, bytes calldata _path)
public
returns (ProfitInfo memory);

Parameters

NameTypeDescription
_amountOutuint256The amount out at which to calculate the profit point
_liquidationPairILiquidationPairThe pair to liquidate
_pathbytesThe Uniswap V3 swap path to use

Returns

NameTypeDescription
<none>ProfitInfoThe profit point for the given amount out

Events

FlashSwapLiquidation

Emitted when a flashswap liquidation has been made.

event FlashSwapLiquidation(
address indexed receiver, ILiquidationPair indexed liquidationPair, bytes path, uint256 profit
);

Structs

ProfitInfo

Struct to store current profit info with the associated liquidation parameters.

struct ProfitInfo {
uint256 amountIn;
uint256 amountOut;
uint256 profit;
bool success;
}

Errors

FlashLiquidationExpired

Thrown when the flashLiquidate `deadline has passed.

error FlashLiquidationExpired(uint256 timestamp, uint256 deadline);

InsufficientProfit

Thrown when the amount of tokenIn left after the liquidation is lower than the expected minProfit.

error InsufficientProfit(uint256 profit, uint256 minProfit);

QuoterZeroAddress

Thrown if the IUniswapV3StaticQuoter address passed to the constructor is the zero address.

error QuoterZeroAddress();

RouterZeroAddress

Thrown if the IV3SwapRouter address passed to the constructor is the zero address.

error RouterZeroAddress();