Skip to content

Commit

Permalink
more weight loss
Browse files Browse the repository at this point in the history
  • Loading branch information
bxmmm1 committed Dec 16, 2024
1 parent 8078dc6 commit 64cf816
Show file tree
Hide file tree
Showing 22 changed files with 66 additions and 1,088 deletions.
2 changes: 1 addition & 1 deletion mainnet-contracts/docs/PufferVaultV2.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The PufferVault maintains the addresses of important contracts related to EigenL

* `uint256 lidoLockedETH`: The amount of ETH the Puffer Protocol has locked inside of Lido
* `uint256 eigenLayerPendingWithdrawalSharesAmount`: The amount of stETH shares the Puffer vault has pending for withdrawal from EigenLayer
* `bool isLidoWithdrawal`: Deprecated from PufferVault version 1
* `bool deprecated_isLidoWithdrawal`: Deprecated from PufferVault version 1
* `EnumerableSet.UintSet lidoWithdrawals`: Deprecated from PufferVault version 1
* `EnumerableSet.Bytes32Set eigenLayerWithdrawals`: Tracks withdrawalRoots from EigenLayer withdrawals
* `EnumerableMap.UintToUintMap lidoWithdrawalAmounts`: Tracks the amounts of corresponding to each Lido withdrawal
Expand Down
9 changes: 3 additions & 6 deletions mainnet-contracts/script/DeployPufETH.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ contract DeployPufETH is BaseScript {
wethAddress = address(weth);

// Deploy implementation contracts
pufferVaultImplementation =
new PufferVault(IStETH(stETHAddress), lidoWithdrawalQueue, stETHStrategy, eigenStrategyManager);
pufferVaultImplementation = new PufferVault(IStETH(stETHAddress), lidoWithdrawalQueue);
vm.label(address(pufferVaultImplementation), "PufferVaultImplementation");
pufferDepositorImplementation =
new PufferDepositor({ stETH: IStETH(stETHAddress), pufferVault: PufferVault(payable(vaultProxy)) });
Expand Down Expand Up @@ -203,10 +202,8 @@ contract DeployPufETH is BaseScript {
function _setupOther() internal view returns (bytes[] memory) {
bytes[] memory calldatas = new bytes[](5);

bytes4[] memory selectors = new bytes4[](3);
selectors[0] = PufferVault.depositToEigenLayer.selector;
selectors[1] = PufferVault.initiateETHWithdrawalsFromLido.selector;
selectors[2] = PufferVault.initiateStETHWithdrawalFromEigenLayer.selector;
bytes4[] memory selectors = new bytes4[](1);
selectors[0] = PufferVault.initiateETHWithdrawalsFromLido.selector;

// Setup setup role
calldatas[0] = abi.encodeWithSelector(
Expand Down
8 changes: 1 addition & 7 deletions mainnet-contracts/script/DeployPufferVaultV3.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import { PufferVaultV3 } from "src/PufferVaultV3.sol";
import { IStETH } from "src/interface/Lido/IStETH.sol";
import { ILidoWithdrawalQueue } from "src/interface/Lido/ILidoWithdrawalQueue.sol";
import { IWETH } from "src/interface/Other/IWETH.sol";
import { IStrategy } from "src/interface/Eigenlayer-Slashing/IStrategy.sol";
import { IEigenLayer } from "src/interface/Eigenlayer-Slashing/IEigenLayer.sol";
import { IPufferOracle } from "src/interface/IPufferOracle.sol";
import { IDelegationManager } from "src/interface/Eigenlayer-Slashing/IDelegationManager.sol";

/**
* @title DeployPufferVaultV3
Expand All @@ -34,10 +31,7 @@ contract DeployPufferVaultV3 is DeployerHelper {
stETH: IStETH(_getStETH()),
weth: IWETH(_getWETH()),
lidoWithdrawalQueue: ILidoWithdrawalQueue(_getLidoWithdrawalQueue()),
stETHStrategy: IStrategy(_getStETHStrategy()),
eigenStrategyManager: IEigenLayer(_getEigenLayerStrategyManager()),
oracle: IPufferOracle(_getPufferOracle()),
delegationManager: IDelegationManager(_getEigenDelegationManager())
oracle: IPufferOracle(_getPufferOracle())
});

//@todo Double check reinitialization
Expand Down
5 changes: 1 addition & 4 deletions mainnet-contracts/script/UpgradePufETH.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,11 @@ contract UpgradePufETH is BaseScript {
//@todo this is for tests only
AccessManager(deployment.accessManager).grantRole(1, _broadcaster, 0);

PufferVaultV3 newImplementation = new PufferVaultV4Tests(
PufferVaultV4Tests newImplementation = new PufferVaultV4Tests(
IStETH(deployment.stETH),
IWETH(deployment.weth),
ILidoWithdrawalQueue(deployment.lidoWithdrawalQueueMock),
IStrategy(deployment.stETHStrategyMock),
IEigenLayer(deployment.eigenStrategyManagerMock),
IPufferOracle(pufferOracle),
_DELEGATION_MANAGER,
IPufferRevenueDepositor(revenueDepositor)
);

Expand Down
129 changes: 5 additions & 124 deletions mainnet-contracts/src/PufferVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
pragma solidity >=0.8.0 <0.9.0;

import { IPufferVault } from "./interface/IPufferVault.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IStETH } from "./interface/Lido/IStETH.sol";
import { ILidoWithdrawalQueue } from "./interface/Lido/ILidoWithdrawalQueue.sol";
import { IEigenLayer } from "./interface/Eigenlayer-Slashing/IEigenLayer.sol";
import { IStrategy } from "./interface/Eigenlayer-Slashing/IStrategy.sol";
import { PufferVaultStorage } from "./PufferVaultStorage.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { IERC721Receiver } from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
Expand Down Expand Up @@ -37,18 +34,6 @@ contract PufferVault is
using EnumerableSet for EnumerableSet.UintSet;
using SafeERC20 for address;

/**
* @dev EigenLayer stETH strategy
* @custom:oz-upgrades-unsafe-allow state-variable-immutable
*/
IStrategy internal immutable _EIGEN_STETH_STRATEGY;

/**
* @dev EigenLayer Strategy Manager
* @custom:oz-upgrades-unsafe-allow state-variable-immutable
*/
IEigenLayer internal immutable _EIGEN_STRATEGY_MANAGER;

/**
* @dev stETH contract
* @custom:oz-upgrades-unsafe-allow state-variable-immutable
Expand All @@ -64,16 +49,9 @@ contract PufferVault is
/**
* @custom:oz-upgrades-unsafe-allow constructor
*/
constructor(
IStETH stETH,
ILidoWithdrawalQueue lidoWithdrawalQueue,
IStrategy stETHStrategy,
IEigenLayer eigenStrategyManager
) payable {
constructor(IStETH stETH, ILidoWithdrawalQueue lidoWithdrawalQueue) payable {
_ST_ETH = stETH;
_LIDO_WITHDRAWAL_QUEUE = lidoWithdrawalQueue;
_EIGEN_STETH_STRATEGY = stETHStrategy;
_EIGEN_STRATEGY_MANAGER = eigenStrategyManager;
_disableInitializers();
}

Expand All @@ -89,7 +67,7 @@ contract PufferVault is
// If we don't use this pattern, somebody can create a Lido withdrawal, claim it to this contract
// Making `$.lidoLockedETH -= msg.value` revert
VaultStorage storage $ = _getPufferVaultStorage();
if ($.isLidoWithdrawal) {
if ($.deprecated_isLidoWithdrawal) {
$.lidoLockedETH -= msg.value;
}
}
Expand Down Expand Up @@ -118,7 +96,7 @@ contract PufferVault is
VaultStorage storage $ = _getPufferVaultStorage();

// Tell our receive() that we are doing a Lido claim
$.isLidoWithdrawal = true;
$.deprecated_isLidoWithdrawal = true;

for (uint256 i = 0; i < requestIds.length; ++i) {
bool isValidWithdrawal = $.lidoWithdrawals.remove(requestIds[i]);
Expand All @@ -131,24 +109,10 @@ contract PufferVault is
}

// Reset back the value
$.isLidoWithdrawal = false;
$.deprecated_isLidoWithdrawal = false;
emit ClaimedWithdrawals(requestIds);
}

/**
* @notice Not allowed
*/
function redeem(uint256, address, address) public virtual override returns (uint256) {
revert WithdrawalsAreDisabled();
}

/**
* @notice Not allowed
*/
function withdraw(uint256, address, address) public virtual override returns (uint256) {
revert WithdrawalsAreDisabled();
}

/**
* @dev See {IERC4626-totalAssets}.
* Eventually, stETH will not be part of this vault anymore, and the Vault(pufETH) will represent shares of total ETH holdings
Expand All @@ -158,21 +122,7 @@ contract PufferVault is
* + ETH balance of this vault
*/
function totalAssets() public view virtual override returns (uint256) {
return _ST_ETH.balanceOf(address(this)) + getELBackingEthAmount() + getPendingLidoETHAmount()
+ address(this).balance;
}

/**
* @notice Returns the ETH amount that is backing this vault locked in EigenLayer stETH strategy
*/
function getELBackingEthAmount() public view virtual returns (uint256 ethAmount) {
VaultStorage storage $ = _getPufferVaultStorage();
// When we initiate withdrawal from EigenLayer, the shares are deducted from the `lockedAmount`
// In that case the locked amount goes to 0 and the pendingWithdrawalAmount increases
uint256 lockedAmount = _EIGEN_STETH_STRATEGY.userUnderlyingView(address(this));
uint256 pendingWithdrawalAmount =
_EIGEN_STETH_STRATEGY.sharesToUnderlyingView($.eigenLayerPendingWithdrawalSharesAmount);
return lockedAmount + pendingWithdrawalAmount;
return _ST_ETH.balanceOf(address(this)) + getPendingLidoETHAmount() + address(this).balance;
}

/**
Expand All @@ -184,75 +134,6 @@ contract PufferVault is
return $.lidoLockedETH;
}

/**
* @notice Deposits stETH into `stETH EigenLayer strategy`
* Restricted access
* @param amount the amount of stETH to deposit
*/
function depositToEigenLayer(uint256 amount) external virtual restricted {
SafeERC20.safeIncreaseAllowance(_ST_ETH, address(_EIGEN_STRATEGY_MANAGER), amount);
_EIGEN_STRATEGY_MANAGER.depositIntoStrategy({ strategy: _EIGEN_STETH_STRATEGY, token: _ST_ETH, amount: amount });
}

/**
* @notice Initiates stETH withdrawals from EigenLayer
* Restricted access
* @param sharesToWithdraw An amount of EigenLayer shares that we want to queue
*/
function initiateStETHWithdrawalFromEigenLayer(uint256 sharesToWithdraw) external virtual restricted {
VaultStorage storage $ = _getPufferVaultStorage();

IStrategy[] memory strategies = new IStrategy[](1);
strategies[0] = IStrategy(_EIGEN_STETH_STRATEGY);

uint256[] memory shares = new uint256[](1);
shares[0] = sharesToWithdraw;

// Account for the shares
$.eigenLayerPendingWithdrawalSharesAmount += sharesToWithdraw;

bytes32 withdrawalRoot = _EIGEN_STRATEGY_MANAGER.queueWithdrawal({
strategyIndexes: new uint256[](1), // [0]
strategies: strategies,
shares: shares,
withdrawer: address(this),
undelegateIfPossible: true
});

$.eigenLayerWithdrawals.add(withdrawalRoot);
}

/**
* @notice Claims stETH withdrawals from EigenLayer
* Restricted access
* @param queuedWithdrawal The queued withdrawal details
* @param tokens The tokens to be withdrawn
* @param middlewareTimesIndex The index of middleware times
*/
function claimWithdrawalFromEigenLayer(
IEigenLayer.QueuedWithdrawal calldata queuedWithdrawal,
IERC20[] calldata tokens,
uint256 middlewareTimesIndex
) external virtual {
VaultStorage storage $ = _getPufferVaultStorage();

bytes32 withdrawalRoot = _EIGEN_STRATEGY_MANAGER.calculateWithdrawalRoot(queuedWithdrawal);
bool isValidWithdrawal = $.eigenLayerWithdrawals.remove(withdrawalRoot);
if (!isValidWithdrawal) {
revert InvalidWithdrawal();
}

// nosemgrep basic-arithmetic-underflow
$.eigenLayerPendingWithdrawalSharesAmount -= queuedWithdrawal.shares[0];

_EIGEN_STRATEGY_MANAGER.completeQueuedWithdrawal({
queuedWithdrawal: queuedWithdrawal,
tokens: tokens,
middlewareTimesIndex: middlewareTimesIndex,
receiveAsTokens: true
});
}

/**
* @notice Initiates ETH withdrawals from Lido
* Restricted access
Expand Down
6 changes: 3 additions & 3 deletions mainnet-contracts/src/PufferVaultStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ abstract contract PufferVaultStorage {
struct VaultStorage {
// 6 Slots for Redemption logic
uint256 lidoLockedETH;
uint256 eigenLayerPendingWithdrawalSharesAmount;
bool isLidoWithdrawal; // Not in use in PufferVaultV2
uint256 deprecated_eigenLayerPendingWithdrawalSharesAmount; // Not in use anymore
bool deprecated_isLidoWithdrawal; // Not in use in PufferVaultV2
EnumerableSet.UintSet lidoWithdrawals; // Not in use in PufferVaultV2
EnumerableSet.Bytes32Set eigenLayerWithdrawals;
EnumerableSet.Bytes32Set deprecated_eigenLayerWithdrawals; // Not in use anymore
EnumerableMap.UintToUintMap lidoWithdrawalAmounts;
// 1 Slot for daily withdrawal limits
uint96 dailyAssetsWithdrawalLimit;
Expand Down
37 changes: 5 additions & 32 deletions mainnet-contracts/src/PufferVaultV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ pragma solidity >=0.8.0 <0.9.0;
import { PufferVault } from "./PufferVault.sol";
import { IStETH } from "./interface/Lido/IStETH.sol";
import { ILidoWithdrawalQueue } from "./interface/Lido/ILidoWithdrawalQueue.sol";
import { IEigenLayer } from "./interface/Eigenlayer-Slashing/IEigenLayer.sol";
import { IStrategy } from "./interface/Eigenlayer-Slashing/IStrategy.sol";
import { IDelegationManager } from "./interface/Eigenlayer-Slashing/IDelegationManager.sol";
import { IWETH } from "./interface/Other/IWETH.sol";
import { IPufferVaultV2 } from "./interface/IPufferVaultV2.sol";
import { IPufferOracle } from "./interface/IPufferOracle.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { EnumerableMap } from "@openzeppelin/contracts/utils/structs/EnumerableMap.sol";
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

/**
Expand Down Expand Up @@ -41,12 +37,6 @@ contract PufferVaultV2 is PufferVault, IPufferVaultV2 {
*/
IPufferOracle public immutable PUFFER_ORACLE;

/**
* @notice Delegation manager from EigenLayer
* @custom:oz-upgrades-unsafe-allow state-variable-immutable
*/
IDelegationManager internal immutable _DELEGATION_MANAGER;

/**
* @dev Two wallets that transferred pufETH to the PufferVault by mistake.
* @custom:oz-upgrades-unsafe-allow state-variable-immutable
Expand All @@ -62,18 +52,11 @@ contract PufferVaultV2 is PufferVault, IPufferVaultV2 {
/**
* @custom:oz-upgrades-unsafe-allow constructor
*/
constructor(
IStETH stETH,
IWETH weth,
ILidoWithdrawalQueue lidoWithdrawalQueue,
IStrategy stETHStrategy,
IEigenLayer eigenStrategyManager,
IPufferOracle oracle,
IDelegationManager delegationManager
) PufferVault(stETH, lidoWithdrawalQueue, stETHStrategy, eigenStrategyManager) {
constructor(IStETH stETH, IWETH weth, ILidoWithdrawalQueue lidoWithdrawalQueue, IPufferOracle oracle)
PufferVault(stETH, lidoWithdrawalQueue)
{
_WETH = weth;
PUFFER_ORACLE = oracle;
_DELEGATION_MANAGER = delegationManager;
ERC4626Storage storage erc4626Storage = _getERC4626StorageInternal();
erc4626Storage._asset = _WETH;
// This redundant code is for the Echidna fuzz testing
Expand Down Expand Up @@ -134,8 +117,8 @@ contract PufferVaultV2 is PufferVault, IPufferVaultV2 {
assembly {
callValue := callvalue()
}
return _ST_ETH.balanceOf(address(this)) + getPendingLidoETHAmount() + getELBackingEthAmount()
+ _WETH.balanceOf(address(this)) + (address(this).balance - callValue) + PUFFER_ORACLE.getLockedEthAmount();
return _ST_ETH.balanceOf(address(this)) + getPendingLidoETHAmount() + _WETH.balanceOf(address(this))
+ (address(this).balance - callValue) + PUFFER_ORACLE.getLockedEthAmount();
}

/**
Expand Down Expand Up @@ -472,16 +455,6 @@ contract PufferVaultV2 is PufferVault, IPufferVaultV2 {
return $.exitFeeBasisPoints;
}

// Not compatible anymore
function claimWithdrawalFromEigenLayer(
IEigenLayer.QueuedWithdrawal calldata queuedWithdrawal,
IERC20[] calldata tokens,
uint256 middlewareTimesIndex
) external override { }

// Not needed anymore
function depositToEigenLayer(uint256 amount) external override { }

/**
* @dev Calculates the fees that should be added to an amount `assets` that does not already include fees.
* Used in {IERC4626-withdraw}.
Expand Down
18 changes: 3 additions & 15 deletions mainnet-contracts/src/PufferVaultV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ pragma solidity >=0.8.0 <0.9.0;
import { PufferVaultV2 } from "./PufferVaultV2.sol";
import { IStETH } from "./interface/Lido/IStETH.sol";
import { ILidoWithdrawalQueue } from "./interface/Lido/ILidoWithdrawalQueue.sol";
import { IEigenLayer } from "./interface/Eigenlayer-Slashing/IEigenLayer.sol";
import { IStrategy } from "./interface/Eigenlayer-Slashing/IStrategy.sol";
import { IDelegationManager } from "./interface/Eigenlayer-Slashing/IDelegationManager.sol";
import { IWETH } from "./interface/Other/IWETH.sol";
import { IPufferVaultV3 } from "./interface/IPufferVaultV3.sol";
import { IPufferOracle } from "./interface/IPufferOracle.sol";
Expand All @@ -26,21 +23,12 @@ contract PufferVaultV3 is PufferVaultV2, IPufferVaultV3 {
* @param stETH Address of the stETH token contract.
* @param weth Address of the WETH token contract.
* @param lidoWithdrawalQueue Address of the Lido withdrawal queue contract.
* @param stETHStrategy Address of the stETH strategy contract.
* @param eigenStrategyManager Address of the EigenLayer strategy manager contract.
* @param oracle Address of the PufferOracle contract.
* @param delegationManager Address of the delegation manager contract.
* @custom:oz-upgrades-unsafe-allow constructor
*/
constructor(
IStETH stETH,
IWETH weth,
ILidoWithdrawalQueue lidoWithdrawalQueue,
IStrategy stETHStrategy,
IEigenLayer eigenStrategyManager,
IPufferOracle oracle,
IDelegationManager delegationManager
) PufferVaultV2(stETH, weth, lidoWithdrawalQueue, stETHStrategy, eigenStrategyManager, oracle, delegationManager) {
constructor(IStETH stETH, IWETH weth, ILidoWithdrawalQueue lidoWithdrawalQueue, IPufferOracle oracle)
PufferVaultV2(stETH, weth, lidoWithdrawalQueue, oracle)
{
_disableInitializers();
}

Expand Down
Loading

0 comments on commit 64cf816

Please sign in to comment.