Skip to content

Commit

Permalink
fix: the strategy contracts takes in WETH as a constructor arg instea…
Browse files Browse the repository at this point in the history
…d of importing the mainnet WETH address as a constant
  • Loading branch information
junkim012 committed Jul 3, 2024
1 parent 0b4cfcc commit 98e624e
Show file tree
Hide file tree
Showing 30 changed files with 125 additions and 57 deletions.
12 changes: 8 additions & 4 deletions script/deploy/05_DeployInitialReserveAndSpotOracles.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ contract DeployInitialReserveAndSpotOraclesScript is DeployScript {
)
);
spotOracle = address(
new WeEthWethSpotOracle{ salt: DEFAULT_SALT }(ltv, address(reserveOracle), maxTimeFromLastUpdate, gracePeriod)
new WeEthWethSpotOracle{ salt: DEFAULT_SALT }(
ltv, address(reserveOracle), maxTimeFromLastUpdate, gracePeriod
)
);
} else {
reserveOracle =
address(new WeEthWethReserveOracle(0, new address[](3), 0, maxChange, maxTimeFromLastUpdate, gracePeriod));
spotOracle = address(new WeEthWethSpotOracle(ltv, address(reserveOracle), maxTimeFromLastUpdate, gracePeriod));
reserveOracle = address(
new WeEthWethReserveOracle(0, new address[](3), 0, maxChange, maxTimeFromLastUpdate, gracePeriod)
);
spotOracle =
address(new WeEthWethSpotOracle(ltv, address(reserveOracle), maxTimeFromLastUpdate, gracePeriod));
}
}
}
18 changes: 15 additions & 3 deletions script/deploy/08_DeployHandlers.s.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import { BASE_WEETH_WETH_BALANCER_POOL_ID, BASE_WSTETH_WETH_UNISWAP } from "../../src/Constants.sol";
import { BASE_WETH, BASE_WEETH_WETH_BALANCER_POOL_ID, BASE_WSTETH_WETH_UNISWAP } from "../../src/Constants.sol";
import { DeployScript } from "../Deploy.s.sol";
import { IonPool } from "../../src/IonPool.sol";
import { GemJoin } from "../../src/join/GemJoin.sol";
Expand Down Expand Up @@ -29,11 +29,23 @@ contract DeployHandlersScript is DeployScript {

if (deployCreate2) {
handler = new WeEthWethHandler{ salt: DEFAULT_SALT }(
ILK_INDEX_ZERO, ionPool, gemJoin, whitelist, BASE_WSTETH_WETH_UNISWAP, BASE_WEETH_WETH_BALANCER_POOL_ID
ILK_INDEX_ZERO,
ionPool,
gemJoin,
whitelist,
BASE_WSTETH_WETH_UNISWAP,
BASE_WEETH_WETH_BALANCER_POOL_ID,
BASE_WETH
);
} else {
handler = new WeEthWethHandler{ salt: DEFAULT_SALT }(
ILK_INDEX_ZERO, ionPool, gemJoin, whitelist, BASE_WSTETH_WETH_UNISWAP, BASE_WEETH_WETH_BALANCER_POOL_ID
ILK_INDEX_ZERO,
ionPool,
gemJoin,
whitelist,
BASE_WSTETH_WETH_UNISWAP,
BASE_WEETH_WETH_BALANCER_POOL_ID,
BASE_WETH
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
IRenzoOracle,
IRestakeManager
} from "./interfaces/ProviderInterfaces.sol";
import { IWETH9 } from "./interfaces/IWETH9.sol";
import { IRedstonePriceFeed } from "./interfaces/IRedstone.sol";
import { IChainlink } from "./interfaces/IChainlink.sol";
import { ICreateX } from "./interfaces/ICreateX.sol";
Expand Down Expand Up @@ -108,4 +109,4 @@ bytes32 constant BASE_WEETH_WETH_BALANCER_POOL_ID = 0xab99a3e856deb448ed99713dfc
IUniswapV3Pool constant BASE_WSTETH_WETH_UNISWAP = IUniswapV3Pool(0x20E068D76f9E90b90604500B84c7e19dCB923e7e);
IChainlink constant BASE_SEQUENCER_UPTIME_FEED = IChainlink(0xBCF85224fc0756B9Fa45aA7892530B47e10b6433);
IERC20 constant BASE_WEETH = IERC20(0x04C0599Ae5A44757c0af6F9eC3b93da8976c150A);
IERC20 constant BASE_WETH = IERC20(0x4200000000000000000000000000000000000006);
IWETH9 constant BASE_WETH = IWETH9(0x4200000000000000000000000000000000000006);
4 changes: 3 additions & 1 deletion src/flash/BalancerFlashloanDirectMintHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ IVault constant VAULT = IVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8);

/**
* @notice This contract allows for easy creation of leverage positions through
* Balancer flashloans and LST mints through the LST provider.
* Balancer flashloans and LST mints through the LST provider. This contract
* should be used when the base asset of the market is `WETH` and when the asset
* required to mint the collateral is also `WETH`.
*
* @dev There are a couple things to consider here from a security perspective. The
* first one is that the flashloan callback must only be callable from the
Expand Down
10 changes: 4 additions & 6 deletions src/flash/IonHandlerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { IWETH9 } from "../interfaces/IWETH9.sol";
import { GemJoin } from "../join/GemJoin.sol";
import { WadRayMath, RAY } from "../libraries/math/WadRayMath.sol";
import { Whitelist } from "../Whitelist.sol";
import { WETH_ADDRESS } from "../Constants.sol";

import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
Expand Down Expand Up @@ -70,9 +69,8 @@ abstract contract IonHandlerBase {
}

IERC20 public immutable BASE;
// Will keep WETH for compatibility with other strategies. But this should
// be removed eventually to remove dependence on WETH as a base asset.
IWETH9 public immutable WETH;

uint8 public immutable ILK_INDEX;
IonPool public immutable POOL;
GemJoin public immutable JOIN;
Expand All @@ -86,14 +84,14 @@ abstract contract IonHandlerBase {
* @param _gemJoin the `GemJoin` associated with the `ilkIndex` of this
* contract.
* @param _whitelist the `Whitelist` module address.
* @param _weth The WETH address of the chain.
*/
constructor(uint8 _ilkIndex, IonPool _ionPool, GemJoin _gemJoin, Whitelist _whitelist) {
constructor(uint8 _ilkIndex, IonPool _ionPool, GemJoin _gemJoin, Whitelist _whitelist, IWETH9 _weth) {
POOL = _ionPool;
ILK_INDEX = _ilkIndex;

BASE = IERC20(_ionPool.underlying());

IWETH9 _weth = WETH_ADDRESS;
WETH = _weth;

address ilkAddress = POOL.getIlkAddress(_ilkIndex);
Expand Down Expand Up @@ -262,6 +260,6 @@ abstract contract IonHandlerBase {
* @dev To allow unwrapping of WETH into ETH.
*/
receive() external payable {
if (msg.sender != address(WETH_ADDRESS)) revert CannotSendEthToContract();
if (msg.sender != address(WETH)) revert CannotSendEthToContract();
}
}
6 changes: 4 additions & 2 deletions src/flash/PtHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IonPool } from "../IonPool.sol";
import { GemJoin } from "../join/GemJoin.sol";
import { Whitelist } from "../Whitelist.sol";
import { IonHandlerBase } from "./IonHandlerBase.sol";
import { IWETH9 } from "./../interfaces/IWETH9.sol";

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

Expand Down Expand Up @@ -47,9 +48,10 @@ contract PtHandler is IonHandlerBase, IPMarketSwapCallback {
IonPool pool,
GemJoin join,
Whitelist whitelist,
IPMarketV3 _market
IPMarketV3 _market,
IWETH9 _weth
)
IonHandlerBase(0, pool, join, whitelist)
IonHandlerBase(0, pool, join, whitelist, _weth)
{
if (!pool.hasRole(pool.GEM_JOIN_ROLE(), address(join))) revert InvalidGemJoin(address(join));

Expand Down
5 changes: 5 additions & 0 deletions src/flash/UniswapFlashloanBalancerSwapHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.s
* rates. DEXes also provide an avenue for atomic deleveraging since the LST ->
* ETH exchange can be made.
*
* This contract is used when the Balancer has a collateral asset <> base asset
* pool, and the Uniswap has a base asset flashloan.
*
* NOTE: Uniswap flashloans do charge a small fee.
*
* @dev Some tokens only have liquidity on Balancer. Due to the reentrancy lock
Expand Down Expand Up @@ -60,6 +63,8 @@ abstract contract UniswapFlashloanBalancerSwapHandler is IUniswapV3FlashCallback
address token0 = IUniswapV3Pool(_flashloanPool).token0();
address token1 = IUniswapV3Pool(_flashloanPool).token1();

// The naming convention uses `_weth`, but this terminology refers to
// the `BASE` token of the underlying IonPool.
bool _wethIsToken0 = token0 == address(BASE);
bool _wethIsToken1 = token1 == address(BASE);

Expand Down
4 changes: 4 additions & 0 deletions src/flash/UniswapFlashswapHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import { IUniswapV3SwapCallback } from "@uniswap/v3-core/contracts/interfaces/ca
* @dev When using the `UniswapFlashSwapHandler`, the `IUniswapV3Pool pool` fed to the
* constructor should be the WETH/[LST] pool.
*
* This flow can be used in case when the UniswapV3 Pool has a collateral <>
* base asset pair. However, the current version of this contract always assumes
* that the base asset is `WETH`.
*
* 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`.
Expand Down
7 changes: 5 additions & 2 deletions src/flash/lrt/EzEthHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { IonHandlerBase } from "../IonHandlerBase.sol";
import { RenzoLibrary } from "./../../libraries/lrt/RenzoLibrary.sol";
import { WETH_ADDRESS } from "../../Constants.sol";

import { IWETH9 } from "./../../interfaces/IWETH9.sol";

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

/**
Expand All @@ -30,9 +32,10 @@ contract EzEthHandler is UniswapFlashswapDirectMintHandlerWithDust {
IonPool _ionPool,
GemJoin _gemJoin,
Whitelist _whitelist,
IUniswapV3Pool _wstEthUniswapPool
IUniswapV3Pool _wstEthUniswapPool,
IWETH9 _weth
)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashswapDirectMintHandlerWithDust(_wstEthUniswapPool, WETH_ADDRESS)
{ }

Expand Down
8 changes: 5 additions & 3 deletions src/flash/lrt/EzEthWethHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { IonPool } from "../../IonPool.sol";
import { GemJoin } from "../../join/GemJoin.sol";
import { Whitelist } from "../../Whitelist.sol";
import { IonHandlerBase } from "../IonHandlerBase.sol";
import { IWETH9 } from "./../../interfaces/IWETH9.sol";
import { UniswapFlashloanBalancerSwapHandler } from "./../UniswapFlashloanBalancerSwapHandler.sol";
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";

/**
* @notice Handler for the ezETH collateral in the ezETH/WETH market.
*
* @custom:security-contact security@molecularlabs.io
*/

contract EzEthWethHandler is UniswapFlashloanBalancerSwapHandler {
/**
* @notice Creates a new `EzEthWethHandler` instance.
Expand All @@ -29,9 +30,10 @@ contract EzEthWethHandler is UniswapFlashloanBalancerSwapHandler {
GemJoin _gemJoin,
Whitelist _whitelist,
IUniswapV3Pool _wstEthUniswapPool,
bytes32 _balancerPoolId
bytes32 _balancerPoolId,
IWETH9 _weth
)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashloanBalancerSwapHandler(_wstEthUniswapPool, _balancerPoolId)
{ }
}
7 changes: 5 additions & 2 deletions src/flash/lrt/RsEthHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { IonHandlerBase } from "../IonHandlerBase.sol";
import { RSETH, WETH_ADDRESS } from "../../Constants.sol";
import { KelpDaoLibrary } from "../../libraries/lrt/KelpDaoLibrary.sol";

import { IWETH9 } from "./../../interfaces/IWETH9.sol";

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

/**
Expand All @@ -33,9 +35,10 @@ contract RsEthHandler is UniswapFlashswapDirectMintHandler {
IonPool _ionPool,
GemJoin _gemJoin,
Whitelist _whitelist,
IUniswapV3Pool _wstEthUniswapPool
IUniswapV3Pool _wstEthUniswapPool,
IWETH9 _weth
)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashswapDirectMintHandler(_wstEthUniswapPool, WETH_ADDRESS)
{ }

Expand Down
6 changes: 4 additions & 2 deletions src/flash/lrt/RswEthHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UniswapFlashswapDirectMintHandler } from "../UniswapFlashswapDirectMint
import { IonHandlerBase } from "../IonHandlerBase.sol";
import { RSWETH, WETH_ADDRESS } from "../../Constants.sol";
import { RestakedSwellLibrary } from "../../libraries/lrt/RestakedSwellLibrary.sol";
import { IWETH9 } from "./../../interfaces/IWETH9.sol";

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

Expand All @@ -33,9 +34,10 @@ contract RswEthHandler is UniswapFlashswapDirectMintHandler {
IonPool _ionPool,
GemJoin _gemJoin,
Whitelist _whitelist,
IUniswapV3Pool _wstEthUniswapPool
IUniswapV3Pool _wstEthUniswapPool,
IWETH9 _weth
)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashswapDirectMintHandler(_wstEthUniswapPool, WETH_ADDRESS)
{ }

Expand Down
6 changes: 4 additions & 2 deletions src/flash/lrt/WeEthHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UniswapFlashswapDirectMintHandler } from "../UniswapFlashswapDirectMint
import { IonHandlerBase } from "../IonHandlerBase.sol";
import { EtherFiLibrary } from "../../libraries/lrt/EtherFiLibrary.sol";
import { WEETH_ADDRESS, WETH_ADDRESS, EETH_ADDRESS } from "../../Constants.sol";
import { IWETH9 } from "./../../interfaces/IWETH9.sol";

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

Expand All @@ -33,9 +34,10 @@ contract WeEthHandler is UniswapFlashswapDirectMintHandler {
IonPool _ionPool,
GemJoin _gemJoin,
Whitelist _whitelist,
IUniswapV3Pool _wstEthUniswapPool
IUniswapV3Pool _wstEthUniswapPool,
IWETH9 _weth
)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashswapDirectMintHandler(_wstEthUniswapPool, WETH_ADDRESS)
{
EETH_ADDRESS.approve(address(WEETH_ADDRESS), type(uint256).max);
Expand Down
6 changes: 4 additions & 2 deletions src/flash/lrt/WeEthWethHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Whitelist } from "../../Whitelist.sol";
import { IonHandlerBase } from "../IonHandlerBase.sol";
import { UniswapFlashloanBalancerSwapHandler } from "./../UniswapFlashloanBalancerSwapHandler.sol";
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import { IWETH9 } from "./../../interfaces/IWETH9.sol";

/**
* @notice Handler for the weETH collateral in the weETH/WETH market.
Expand All @@ -29,9 +30,10 @@ contract WeEthWethHandler is UniswapFlashloanBalancerSwapHandler {
GemJoin _gemJoin,
Whitelist _whitelist,
IUniswapV3Pool _wstEthUniswapPool,
bytes32 _balancerPoolId
bytes32 _balancerPoolId,
IWETH9 _weth
)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashloanBalancerSwapHandler(_wstEthUniswapPool, _balancerPoolId)
{ }
}
6 changes: 4 additions & 2 deletions src/flash/lst/EthXHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IonHandlerBase } from "../IonHandlerBase.sol";
import { UniswapFlashloanBalancerSwapHandler } from "../UniswapFlashloanBalancerSwapHandler.sol";
import { BalancerFlashloanDirectMintHandler } from "../BalancerFlashloanDirectMintHandler.sol";
import { UniswapFlashswapHandler } from "../UniswapFlashswapHandler.sol";
import { IWETH9 } from "./../../interfaces/IWETH9.sol";

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

Expand Down Expand Up @@ -47,10 +48,11 @@ contract EthXHandler is
Whitelist _whitelist,
IUniswapV3Pool _wstEthUniswapPool,
IUniswapV3Pool _ethXUniswapPool,
bytes32 _balancerPoolId
bytes32 _balancerPoolId,
IWETH9 _weth
)
UniswapFlashloanBalancerSwapHandler(_wstEthUniswapPool, _balancerPoolId)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashswapHandler(_ethXUniswapPool, false)
{
STADER_DEPOSIT = _staderDeposit;
Expand Down
6 changes: 4 additions & 2 deletions src/flash/lst/SwEthHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ISwEth } from "../../interfaces/ProviderInterfaces.sol";
import { SwellLibrary } from "../../libraries/lst/SwellLibrary.sol";
import { WadRayMath } from "../../libraries/math/WadRayMath.sol";
import { Whitelist } from "../../Whitelist.sol";
import { IWETH9 } from "./../../interfaces/IWETH9.sol";

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

Expand All @@ -35,9 +36,10 @@ contract SwEthHandler is UniswapFlashswapHandler, BalancerFlashloanDirectMintHan
IonPool _ionPool,
GemJoin _gemJoin,
Whitelist _whitelist,
IUniswapV3Pool _swEthPool
IUniswapV3Pool _swEthPool,
IWETH9 _weth
)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashswapHandler(_swEthPool, true)
{ }

Expand Down
6 changes: 4 additions & 2 deletions src/flash/lst/WstEthHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BalancerFlashloanDirectMintHandler } from "../BalancerFlashloanDirectMi
import { IWstEth } from "../../interfaces/ProviderInterfaces.sol";
import { LidoLibrary } from "../../libraries/lst/LidoLibrary.sol";
import { Whitelist } from "../../Whitelist.sol";
import { IWETH9 } from "./../../interfaces/IWETH9.sol";

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
Expand Down Expand Up @@ -36,9 +37,10 @@ contract WstEthHandler is UniswapFlashswapHandler, BalancerFlashloanDirectMintHa
IonPool _ionPool,
GemJoin _gemJoin,
Whitelist _whitelist,
IUniswapV3Pool _wstEthUniswapPool
IUniswapV3Pool _wstEthUniswapPool,
IWETH9 _weth
)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist)
IonHandlerBase(_ilkIndex, _ionPool, _gemJoin, _whitelist, _weth)
UniswapFlashswapHandler(_wstEthUniswapPool, false)
{
// NOTE: approves wstETH contract infinite approval to move this contract's stEth
Expand Down
2 changes: 1 addition & 1 deletion src/oracles/reserve/lrt/WeEthWethReserveOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ contract WeEthWethReserveOracle is ReserveOracle {

if (answer == 1) revert SequencerDown();
if (block.timestamp - startedAt <= GRACE_PERIOD) revert GracePeriodNotOver();

(, int256 ethPerWeEth,, uint256 ethPerWeEthUpdatedAt,) =
BASE_WEETH_ETH_EXCHANGE_RATE_CHAINLINK.latestRoundData();

Expand Down
Loading

0 comments on commit 98e624e

Please sign in to comment.