UniswapFlashswapHandler
Inherits: IonHandlerBase, IUniswapV3SwapCallback
This contract allows for easy creation and closing of leverage positions through Uniswap flashswaps--flashloan not necessary! In terms of creation, this may be a more desirable path than directly minting from an LST provider since market prices tend to be slightly lower than provider exchange rates. DEXes also provide an avenue for atomic deleveraging since the LST -> ETH exchange can be made.
When using the UniswapFlashSwapHandler
, the IUniswapV3Pool pool
fed to the constructor should be the WETH/[LST] pool. Unlike Balancer flashloans, there is no concern here that somebody else could initiate a flashswap, then direct the callback to be called on this contract. Uniswap enforces that callback is only called on msg.sender
.
State Variables
MIN_SQRT_RATIO
The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
MAX_SQRT_RATIO
The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
UNISWAP_POOL
WETH_IS_TOKEN0
Functions
constructor
Creates a new UniswapFlashswapHandler
instance.
Parameters
Name | Type | Description |
---|---|---|
|
| Pool to perform the flashswap on. |
|
| Whether WETH is token0 or token1 in the pool. |
flashswapLeverage
Transfer collateral from user -> initiate swap for collateral from WETH on Uniswap (contract will receive collateral first) -> deposit all collateral into IonPool
-> borrow WETH from IonPool
-> complete swap by sending WETH to Uniswap.
Parameters
Name | Type | Description |
---|---|---|
|
| in collateral terms. [WAD] |
|
| in collateral terms. [WAD] |
|
| in WETH terms. This value also allows the user to control slippage of the swap. [WAD] |
|
| for the swap. Recommended value is the current exchange rate to ensure the swap never costs more than a direct mint would. Passing the current exchange rate means swapping beyond that point is worse than direct minting. |
|
| timestamp for which the transaction must be executed. This prevents txs that have sat in the mempool for too long to be executed. |
|
| that the user is whitelisted. |
_flashswapLeverage
Parameters
Name | Type | Description |
---|---|---|
|
| in terms of swETH |
|
| in terms of swETH. How much collateral to add to the position in the vault. |
|
| in terms of WETH. How much debt to add to the position in the vault. |
|
| for the swap. Recommended value is the current exchange rate to ensure the swap never costs more than a direct mint would. |
flashswapDeleverage
Initiate swap for WETH from collateral (contract will receive WETH first) -> repay debt on IonPool
-> withdraw (and gem-exit) collateral from IonPool
-> complete swap by sending collateral to Uniswap.
The two function parameters must be chosen carefully. If maxCollateralToRemove
's ETH valuation were higher then debtToRemove
, it would theoretically be possible to sell more collateral then was required for debtToRemove
to be repaid (even if debtToRemove
is worth nowhere near that valuation) due to the slippage of the sell. maxCollateralToRemove
is essentially a slippage guard here.
Parameters
Name | Type | Description |
---|---|---|
|
| he max amount of collateral user is willing to sell to repay |
|
| The desired amount of debt to remove. [WAD] |
|
| for the swap. Can be set to 0 to set max bounds. |
|
|
_initiateFlashSwap
Handles swap initiation logic. This function can only initiate exact output swaps.
Parameters
Name | Type | Description |
---|---|---|
|
| Direction of the swap. |
|
| Desired amount of output. |
|
| of output tokens. |
|
| of the swap. |
|
| Arbitrary data to be passed through swap callback. |
uniswapV3SwapCallback
From the perspective of the pool i.e. Negative amount means pool is sending. This function is intended to never be called directly. It should only be called by the Uniswap pool during a swap initiated by this contract.
One thing to note from a security perspective is that the pool only calls the callback on msg.sender
. So a theoretical attacker cannot call this function by directing where to call the callback.
Parameters
Name | Type | Description |
---|---|---|
|
| change in token0 |
|
| change in token1 |
|
| arbitrary data |