UniswapFlashloanBalancerSwapHandler
Inherits: IUniswapV3FlashCallback, IonHandlerBase
This contract allows for easy creation and closing of leverage positions through Uniswap flashloans and LST swaps on Balancer. 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. NOTE: Uniswap flashloans do charge a small fee.
Some tokens only have liquidity on Balancer. Due to the reentrancy lock on the Balancer VAULT, utilizing their free flashloan followed by a pool swap is not possible. Instead, we will take a cheap (0.01%) flashloan from the wstETH/ETH uniswap pool and perform the Balancer swap. The rETH/ETH uniswap pool could also be used since it has a 0.01% fee but it does have less liquidity.
State Variables
VAULT
WETH_IS_TOKEN0_ON_UNISWAP
FLASHLOAN_POOL
BALANCER_POOL_ID
Functions
constructor
Creates a new instance of UniswapFlashloanBalancerSwapHandler
Parameters
Name | Type | Description |
---|---|---|
|
| UniswapV3 pool from which to flashloan |
|
| Balancer pool identifier through which to route swaps. |
flashLeverageWethAndSwap
Transfer collateral from user + flashloan WETH from Uniswap -> swap for collateral using WETH on Balancer pool -> deposit all collateral into IonPool
-> borrow WETH from IonPool
-> repay Uniswap flashloan + fee. Uniswap flashloans do incur a fee.
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] |
|
| timestamp for which the transaction must be executed. This prevents txs that have sat in the mempool for too long to be executed. |
|
| used to validate the user is whitelisted. |
flashDeleverageWethAndSwap
Flashloan WETH from Uniswap -> repay debt in IonPool
-> withdraw collateral from IonPool
-> sell collateral for WETH
on Balancer -> repay Uniswap flashloan + fee. Uniswap flashloans do incur a fee.
Parameters
Name | Type | Description |
---|---|---|
|
| The max amount of collateral user is willing to sell to repay |
|
| The desired amount of debt to remove. [WAD] |
|
| timestamp for which the transaction must be executed. This prevents txs that have sat in the mempool for too long to be executed. |
uniswapV3FlashCallback
Called to msg.sender
after transferring to the recipient from IUniswapV3Pool#flash.
In the implementation, you must repay the pool the tokens sent by flash()
plus the computed fee amounts. The caller of this method must be checked to be a UniswapV3Pool. Initiator is guaranteed to be this contract since UniswapV3 pools will only call the callback on msg.sender.
Parameters
Name | Type | Description |
---|---|---|
|
| The fee amount in tokenInBalancer due to the pool by the end of the flash |
|
| The fee amount in tokenOutBalancer due to the pool by the end of the flash |
|
| Any data passed through by the caller via the IUniswapV3PoolActions#flash call |
_simulateGivenOutBalancerSwap
Simulates a Balancer swap with a desired amount of assetOut
.
Parameters
Name | Type | Description |
---|---|---|
|
| Balancer fund management struct |
|
| asset to swap from |
|
| asset to swap to |
|
| desired amount of assetOut. Will revert if not received. [WAD] |