Skip to content

Commit

Permalink
remove uniswap specific variables and move pool interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jpick713 committed Jul 26, 2024
1 parent f1bbfdd commit b9e057d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { IPMarketV3 } from "pendle-core-v2-public/interfaces/IPMarketV3.sol";

import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import { IERC20 } from "openzeppelin-contracts/contracts/interfaces/IERC20.sol";
import {IPool} from "./interfaces/IPool.sol";
import {IPool} from "./interfaces/aerodrome/IPool.sol";

uint8 constant REDSTONE_DECIMALS = 8;

Expand Down
41 changes: 9 additions & 32 deletions src/flash/AerodromeFlashswapHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { WadRayMath } from "../libraries/math/WadRayMath.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IPool} from "../interfaces/IPool.sol";
import {IPool} from "../interfaces/aerodrome/IPool.sol";
import {IIonPool} from "../interfaces/IIonPool.sol";

import {console} from "forge-std/Test.sol";
Expand Down Expand Up @@ -37,7 +37,6 @@ interface IPoolFactory {
*
* 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`.
*
* @custom:security-contact [email protected]
*/
Expand All @@ -46,21 +45,15 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
using SafeCast for uint256;
using SafeERC20 for IERC20;

error InvalidUniswapPool();
error InvalidAerodromePool();
error InvalidZeroLiquidityRegionSwap();
error InvalidSqrtPriceLimitX96(uint160 sqrtPriceLimitX96);

error FlashswapRepaymentTooExpensive(uint256 amountIn, uint256 maxAmountIn);
error CallbackOnlyCallableByPool(address unauthorizedCaller);
error OutputAmountNotReceived(uint256 amountReceived, uint256 amountRequired);
error ZeroAmountIn();
error AmountInTooHigh(uint256 amountIn, uint256 maxAmountIn);

/// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
uint160 internal constant MIN_SQRT_RATIO = 4_295_128_739;
/// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
uint160 internal constant MAX_SQRT_RATIO = 1_461_446_703_485_210_103_287_273_052_203_988_822_378_723_970_342;

IPool public immutable AERODROME_POOL;
bool private immutable WETH_IS_TOKEN0;

Expand All @@ -69,16 +62,16 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
* @param _pool Pool to perform the flashswap on.
*/
constructor(IPool _pool, bool /*_wethIsToken0*/){
if (address(_pool) == address(0)) revert InvalidUniswapPool();
if (address(_pool) == address(0)) revert InvalidAerodromePool();

address token0 = _pool.token0();
address token1 = _pool.token1();

// I added this
// require(_wethIsToken0 && token0 == address(WETH) || !_wethIsToken0 && token1 == address(WETH), "incorrect weth is token 0");

if (token0 != address(WETH) && token1 != address(WETH)) revert InvalidUniswapPool();
if (token0 == address(WETH) && token1 == address(WETH)) revert InvalidUniswapPool();
if (token0 != address(WETH) && token1 != address(WETH)) revert InvalidAerodromePool();
if (token0 == address(WETH) && token1 == address(WETH)) revert InvalidAerodromePool();

AERODROME_POOL = _pool;

Expand All @@ -104,10 +97,6 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
* @param resultingAdditionalCollateral in collateral terms. [WAD]
* @param maxResultingAdditionalDebt in WETH terms. This value also allows
* the user to control slippage of the swap. [WAD]
* @param sqrtPriceLimitX96 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.
* @param deadline timestamp for which the transaction must be executed.
* This prevents txs that have sat in the mempool for too long to be
* executed.
Expand All @@ -117,7 +106,6 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
uint256 initialDeposit,
uint256 resultingAdditionalCollateral,
uint256 maxResultingAdditionalDebt,
uint160 sqrtPriceLimitX96,
uint256 deadline,
bytes32[] calldata proof
)
Expand All @@ -126,7 +114,7 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
onlyWhitelistedBorrowers(proof)
{
LST_TOKEN.safeTransferFrom(msg.sender, address(this), initialDeposit);
_flashswapLeverage(initialDeposit, resultingAdditionalCollateral, maxResultingAdditionalDebt, sqrtPriceLimitX96);
_flashswapLeverage(initialDeposit, resultingAdditionalCollateral, maxResultingAdditionalDebt);
}

/**
Expand All @@ -135,15 +123,11 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
* collateral to add to the position in the vault.
* @param maxResultingAdditionalDebt in terms of WETH. How much debt to add
* to the position in the vault.
* @param sqrtPriceLimitX96 for the swap. Recommended value is the current
* exchange rate to ensure the swap never costs more than a direct mint
* would.
*/
function _flashswapLeverage(
uint256 initialDeposit,
uint256 resultingAdditionalCollateral,
uint256 maxResultingAdditionalDebt,
uint160 sqrtPriceLimitX96
uint256 maxResultingAdditionalDebt
)
internal
{
Expand Down Expand Up @@ -194,7 +178,7 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
isLeverage: true
});

_initiateFlashSwap(WETH_IS_TOKEN0, amountToLeverage, address(this), sqrtPriceLimitX96, flashswapData);
_initiateFlashSwap(WETH_IS_TOKEN0, amountToLeverage, address(this), flashswapData);

console.log("AfterK actual ", AERODROME_POOL.getK());
console.log("balance of pool in collateral post: ", LST_TOKEN.balanceOf(address(AERODROME_POOL)));
Expand All @@ -216,12 +200,10 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
* @param maxCollateralToRemove he max amount of collateral user is willing
* to sell to repay `debtToRemove` debt. [WAD]
* @param debtToRemove The desired amount of debt to remove. [WAD]
* @param sqrtPriceLimitX96 for the swap. Can be set to 0 to set max bounds.
*/
function flashswapDeleverage(
uint256 maxCollateralToRemove,
uint256 debtToRemove,
uint160 sqrtPriceLimitX96,
uint256 deadline
)
external
Expand Down Expand Up @@ -267,7 +249,7 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
isLeverage: false
});

_initiateFlashSwap(!WETH_IS_TOKEN0, debtToRemove, address(this), sqrtPriceLimitX96, flashswapData);
_initiateFlashSwap(!WETH_IS_TOKEN0, debtToRemove, address(this), flashswapData);

console.log("AfterK actual ", AERODROME_POOL.getK());
console.log("balance of pool in collateral post: ", LST_TOKEN.balanceOf(address(AERODROME_POOL)));
Expand All @@ -280,21 +262,16 @@ abstract contract AerodromeFlashswapHandler is IonHandlerBase, IPoolCallee {
* @param zeroForOne Direction of the swap.
* @param amountOut Desired amount of output.
* @param recipient of output tokens.
* @param sqrtPriceLimitX96 of the swap.
* @param data Arbitrary data to be passed through swap callback.
*/
function _initiateFlashSwap(
bool zeroForOne,
uint256 amountOut,
address recipient,
uint160 sqrtPriceLimitX96,
FlashSwapData memory data
)
private
{
if ((sqrtPriceLimitX96 < MIN_SQRT_RATIO || sqrtPriceLimitX96 > MAX_SQRT_RATIO) && sqrtPriceLimitX96 != 0) {
revert InvalidSqrtPriceLimitX96(sqrtPriceLimitX96);
}
// the following are AerodromePool.swap()s first 3 inputs:
// @param amount0Out Amount of token0 to send to `to`
// @param amount1Out Amount of token1 to send to `to`
Expand Down
File renamed without changes.
18 changes: 5 additions & 13 deletions test/fork/concrete/handlers-base/AerodromeFlashswapHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AerodromeFlashswapHandler } from "../../../../src/flash/AerodromeFlashs
import { IonHandlerBase } from "../../../../src/flash/IonHandlerBase.sol";
import { Whitelist } from "../../../../src/Whitelist.sol";
import { BASE_RSETH_WETH_AERODROME, BASE_RSETH, BASE_WETH } from "../../../../src/Constants.sol";
import { IPool } from "../../../../src/interfaces/IPool.sol";
import { IPool } from "../../../../src/interfaces/aerodrome/IPool.sol";

import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";

Expand All @@ -22,7 +22,6 @@ interface IPoolFactory {
}

abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
uint160 sqrtPriceLimitX96;

function testFork_FlashswapLeverage() external {
uint256 initialDeposit = 1e18;
Expand All @@ -41,7 +40,6 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
initialDeposit,
resultingAdditionalCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp,
borrowerWhitelistProof
);
Expand All @@ -52,7 +50,6 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
initialDeposit,
resultingAdditionalCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
new bytes32[](0)
);
Expand All @@ -63,7 +60,6 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
initialDeposit,
resultingAdditionalCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
borrowerWhitelistProof
);
Expand Down Expand Up @@ -95,7 +91,6 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
initialDeposit,
resultingAdditionalCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
borrowerWhitelistProof
);
Expand Down Expand Up @@ -129,9 +124,9 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
uint256 debtToRemove = normalizedDebtToRemove.rayMulUp(ionPool.rate(_getIlkIndex()));

vm.expectRevert(abi.encodeWithSelector(IonHandlerBase.TransactionDeadlineReached.selector, block.timestamp));
_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, 0, block.timestamp);
_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, block.timestamp);

_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, 0, block.timestamp + 1);
_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, block.timestamp + 1);

uint256 currentRate = ionPool.rate(_getIlkIndex());
uint256 roundingError = currentRate / RAY;
Expand All @@ -157,7 +152,6 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
initialDeposit,
resultingAdditionalCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
borrowerWhitelistProof
);
Expand Down Expand Up @@ -186,7 +180,7 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
// Remove all debt
uint256 debtToRemove = type(uint256).max;

_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, 0, block.timestamp + 1);
_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, block.timestamp + 1);

uint256 currentRate = ionPool.rate(_getIlkIndex());
uint256 roundingError = currentRate / RAY;
Expand Down Expand Up @@ -232,7 +226,6 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
initialDeposit,
resultingAdditionalCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
new bytes32[](0)
);
Expand All @@ -252,7 +245,6 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
initialDeposit,
resultingAdditionalCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
new bytes32[](0)
);
Expand All @@ -268,7 +260,7 @@ abstract contract AerodromeFlashswapHandler_Test is LrtHandler_ForkBase {
uint256 debtToRemove = normalizedDebtToRemove.rayMulUp(ionPool.rate(_getIlkIndex()));

vm.expectRevert();
_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, 0, block.timestamp + 1);
_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, block.timestamp + 1);
}

function _getTypedUFHandler() private view returns (AerodromeFlashswapHandler) {
Expand Down
1 change: 0 additions & 1 deletion test/fork/concrete/lrt/BaseMainnet/RsEthWethHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { AerodromeFlashswapHandler_Test } from
"../../../concrete/handlers-base/AerodromeFlashswapHandler.t.sol";
import { IProviderLibraryExposed } from "../../../../helpers/IProviderLibraryExposed.sol";
import { SafeCast } from "openzeppelin-contracts/contracts/utils/math/SafeCast.sol";
import {IPool} from "../../../../../src/interfaces/IPool.sol";

using SafeCast for int256;

Expand Down
15 changes: 3 additions & 12 deletions test/fork/fuzz/handlers-base/AerodromeFlashswapHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AerodromeFlashswapHandler } from "../../../../src/flash/AerodromeFlashs
import { IonHandlerBase } from "../../../../src/flash/IonHandlerBase.sol";
import { Whitelist } from "../../../../src/Whitelist.sol";
import { BASE_RSETH_WETH_AERODROME, BASE_RSETH, BASE_WETH } from "../../../../src/Constants.sol";
import { IPool } from "../../../../src/interfaces/IPool.sol";
import { IPool } from "../../../../src/interfaces/aerodrome/IPool.sol";

import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";

Expand All @@ -21,13 +21,7 @@ interface IPoolFactory {
function getFee(address pool, bool isStable) external view returns (uint256);
}

struct Config {
uint256 initialDepositLowerBound;
}

abstract contract AerodromeFlashswapHandler_FuzzTest is LrtHandler_ForkBase {
uint160 sqrtPriceLimitX96;
Config ufConfig;

function testForkFuzz_FlashswapLeverage(uint256 initialDeposit, uint256 resultingCollateralMultiplier) public {
uint256 lrtBalance = BASE_RSETH.balanceOf(address(BASE_RSETH_WETH_AERODROME));
Expand All @@ -46,7 +40,6 @@ abstract contract AerodromeFlashswapHandler_FuzzTest is LrtHandler_ForkBase {
initialDeposit,
resultingCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
new bytes32[](0)
);
Expand Down Expand Up @@ -81,7 +74,6 @@ abstract contract AerodromeFlashswapHandler_FuzzTest is LrtHandler_ForkBase {
initialDeposit,
resultingCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
new bytes32[](0)
);
Expand Down Expand Up @@ -114,7 +106,7 @@ abstract contract AerodromeFlashswapHandler_FuzzTest is LrtHandler_ForkBase {
// Round up otherwise can leave 1 wei of dust in debt left
uint256 debtToRemove = normalizedDebtToRemove.rayMulUp(ionPool.rate(_getIlkIndex()));

_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, 0, block.timestamp + 1);
_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, block.timestamp + 1);

uint256 currentRate = ionPool.rate(_getIlkIndex());
uint256 roundingError = currentRate / RAY;
Expand Down Expand Up @@ -148,7 +140,6 @@ abstract contract AerodromeFlashswapHandler_FuzzTest is LrtHandler_ForkBase {
initialDeposit,
resultingCollateral,
maxResultingDebt,
sqrtPriceLimitX96,
block.timestamp + 1,
new bytes32[](0)
);
Expand Down Expand Up @@ -178,7 +169,7 @@ abstract contract AerodromeFlashswapHandler_FuzzTest is LrtHandler_ForkBase {
// Remove all debt if any
uint256 debtToRemove = normalizedDebtCurrent == 0 ? 0 : type(uint256).max;

_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, 0, block.timestamp + 1);
_getTypedUFHandler().flashswapDeleverage(maxCollateralToRemove, debtToRemove, block.timestamp + 1);

uint256 currentRate = ionPool.rate(_getIlkIndex());
uint256 roundingError = currentRate / RAY;
Expand Down

0 comments on commit b9e057d

Please sign in to comment.