-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPufferModule.sol
More file actions
370 lines (321 loc) · 12.8 KB
/
PufferModule.sol
File metadata and controls
370 lines (321 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;
import { AccessManagedUpgradeable } from "openzeppelin-upgradeable/access/manager/AccessManagedUpgradeable.sol";
import { IDelegationManager } from "eigenlayer/interfaces/IDelegationManager.sol";
import { IEigenPodManager } from "eigenlayer/interfaces/IEigenPodManager.sol";
import { ISignatureUtils } from "eigenlayer/interfaces/ISignatureUtils.sol";
import { IStrategy } from "eigenlayer/interfaces/IStrategy.sol";
import { BeaconChainProofs } from "eigenlayer/libraries/BeaconChainProofs.sol";
import { IPufferProtocol } from "puffer/interface/IPufferProtocol.sol";
import { IEigenPod } from "eigenlayer/interfaces/IEigenPod.sol";
import { IGuardianModule } from "puffer/interface/IGuardianModule.sol";
import { IPufferModuleManager } from "puffer/interface/IPufferModuleManager.sol";
import { IDelayedWithdrawalRouter } from "eigenlayer/interfaces/IDelayedWithdrawalRouter.sol";
import { IPufferModule } from "puffer/interface/IPufferModule.sol";
import { Unauthorized } from "puffer/Errors.sol";
import { Initializable } from "openzeppelin-upgradeable/proxy/utils/Initializable.sol";
import { MerkleProof } from "openzeppelin/utils/cryptography/MerkleProof.sol";
import { LibGuardianMessages } from "puffer/LibGuardianMessages.sol";
import { Address } from "openzeppelin/utils/Address.sol";
import { IERC20 } from "openzeppelin/token/ERC20/IERC20.sol";
import { ModuleStorage } from "puffer/struct/ModuleStorage.sol";
/**
* @title PufferModule
* @author Puffer Finance
* @notice PufferModule
* @custom:security-contact security@puffer.fi
*/
contract PufferModule is IPufferModule, Initializable, AccessManagedUpgradeable {
using Address for address;
using Address for address payable;
/**
* @dev Represents the Beacon Chain strategy in EigenLayer
*/
address internal constant _BEACON_CHAIN_STRATEGY = 0xbeaC0eeEeeeeEEeEeEEEEeeEEeEeeeEeeEEBEaC0;
/**
* @dev Upgradeable contract from EigenLayer
*/
IEigenPodManager public immutable EIGEN_POD_MANAGER;
/**
* @dev Upgradeable contract from EigenLayer
*/
IDelayedWithdrawalRouter public immutable EIGEN_WITHDRAWAL_ROUTER;
/**
* @dev Upgradeable contract from EigenLayer
*/
IDelegationManager public immutable EIGEN_DELEGATION_MANAGER;
/**
* @dev Upgradeable PufferProtocol
*/
IPufferProtocol public immutable PUFFER_PROTOCOL;
/**
* @dev Upgradeable Puffer Module Manager
*/
IPufferModuleManager public immutable PUFFER_MODULE_MANAGER;
// keccak256(abi.encode(uint256(keccak256("PufferModule.storage")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant _PUFFER_MODULE_BASE_STORAGE =
0x501caad7d5b9c1542c99d193b659cbf5c57571609bcfc93d65f1e159821d6200;
constructor(
IPufferProtocol protocol,
address eigenPodManager,
IDelayedWithdrawalRouter eigenWithdrawalRouter,
IDelegationManager delegationManager,
IPufferModuleManager moduleManager
) payable {
EIGEN_POD_MANAGER = IEigenPodManager(eigenPodManager);
EIGEN_WITHDRAWAL_ROUTER = eigenWithdrawalRouter;
EIGEN_DELEGATION_MANAGER = delegationManager;
PUFFER_PROTOCOL = protocol;
PUFFER_MODULE_MANAGER = moduleManager;
_disableInitializers();
}
function initialize(bytes32 moduleName, address initialAuthority) external initializer {
__AccessManaged_init(initialAuthority);
ModuleStorage storage $ = _getPufferModuleStorage();
$.moduleName = moduleName;
$.eigenPod = IEigenPod(address(EIGEN_POD_MANAGER.createPod()));
}
/**
* @dev Calls PufferProtocol to check if it is paused
*/
modifier whenNotPaused() {
PUFFER_PROTOCOL.revertIfPaused();
_;
}
modifier onlyPufferProtocol() {
if (msg.sender != address(PUFFER_PROTOCOL)) {
revert Unauthorized();
}
_;
}
modifier onlyPufferModuleManager() {
if (msg.sender != address(PUFFER_MODULE_MANAGER)) {
revert Unauthorized();
}
_;
}
receive() external payable { }
/**
* @inheritdoc IPufferModule
*/
function callStake(bytes calldata pubKey, bytes calldata signature, bytes32 depositDataRoot)
external
payable
onlyPufferProtocol
{
// EigenPod is deployed in this call
EIGEN_POD_MANAGER.stake{ value: 32 ether }(pubKey, signature, depositDataRoot);
}
/**
* @inheritdoc IPufferModule
* @dev Restricted to PufferModuleManager
*/
function queueWithdrawals(uint256 shareAmount)
external
virtual
onlyPufferModuleManager
returns (bytes32[] memory)
{
IDelegationManager.QueuedWithdrawalParams[] memory withdrawals =
new IDelegationManager.QueuedWithdrawalParams[](1);
uint256[] memory shares = new uint256[](1);
shares[0] = shareAmount;
IStrategy[] memory strategies = new IStrategy[](1);
strategies[0] = IStrategy(_BEACON_CHAIN_STRATEGY);
withdrawals[0] = IDelegationManager.QueuedWithdrawalParams({
strategies: strategies,
shares: shares,
withdrawer: address(this)
});
return EIGEN_DELEGATION_MANAGER.queueWithdrawals(withdrawals);
}
/**
* @inheritdoc IPufferModule
*/
function completeQueuedWithdrawals(
IDelegationManager.Withdrawal[] calldata withdrawals,
IERC20[][] calldata tokens,
uint256[] calldata middlewareTimesIndexes,
bool[] calldata receiveAsTokens
) external virtual whenNotPaused onlyPufferModuleManager {
EIGEN_DELEGATION_MANAGER.completeQueuedWithdrawals({
withdrawals: withdrawals,
tokens: tokens,
middlewareTimesIndexes: middlewareTimesIndexes,
receiveAsTokens: receiveAsTokens
});
}
/**
* @inheritdoc IPufferModule
* @dev Restricted to PufferModuleManager
*/
function verifyAndProcessWithdrawals(
uint64 oracleTimestamp,
BeaconChainProofs.StateRootProof calldata stateRootProof,
BeaconChainProofs.WithdrawalProof[] calldata withdrawalProofs,
bytes[] calldata validatorFieldsProofs,
bytes32[][] calldata validatorFields,
bytes32[][] calldata withdrawalFields
) external virtual whenNotPaused onlyPufferModuleManager {
ModuleStorage storage $ = _getPufferModuleStorage();
$.eigenPod.verifyAndProcessWithdrawals({
oracleTimestamp: oracleTimestamp,
stateRootProof: stateRootProof,
withdrawalProofs: withdrawalProofs,
validatorFieldsProofs: validatorFieldsProofs,
validatorFields: validatorFields,
withdrawalFields: withdrawalFields
});
}
/**
* @inheritdoc IPufferModule
* @dev Restricted to PufferModuleManager
*/
function verifyWithdrawalCredentials(
uint64 oracleTimestamp,
BeaconChainProofs.StateRootProof calldata stateRootProof,
uint40[] calldata validatorIndices,
bytes[] calldata validatorFieldsProofs,
bytes32[][] calldata validatorFields
) external virtual onlyPufferModuleManager {
ModuleStorage storage $ = _getPufferModuleStorage();
$.eigenPod.verifyWithdrawalCredentials({
oracleTimestamp: oracleTimestamp,
stateRootProof: stateRootProof,
validatorIndices: validatorIndices,
withdrawalCredentialProofs: validatorFieldsProofs,
validatorFields: validatorFields
});
}
/**
* @inheritdoc IPufferModule
* @dev Restricted to PufferModuleManager
*/
function withdrawNonBeaconChainETHBalanceWei(uint256 amountToWithdraw) external virtual onlyPufferModuleManager {
ModuleStorage storage $ = _getPufferModuleStorage();
$.eigenPod.withdrawNonBeaconChainETHBalanceWei(address(this), amountToWithdraw);
}
/**
* @dev Restricted to PufferProtocol
*/
function call(address to, uint256 amount, bytes calldata data)
external
onlyPufferProtocol
returns (bool success, bytes memory)
{
// slither-disable-next-line arbitrary-send-eth
return to.call{ value: amount }(data);
}
/**
* @notice Submit a valid MerkleProof all their validators' staking rewards will be sent to node operator
* @dev Anybody can trigger a claim of the rewards for any node operator as long as the proofs submitted are valid
*
* @param node is a node operator's wallet address
* @param blockNumbers is the array of block numbers for which the sender is claiming the rewards
* @param amounts is the array of amounts to claim
* @param merkleProofs is the array of Merkle proofs
*/
function collectRewards(
address node,
uint256[] calldata blockNumbers,
uint256[] calldata amounts,
bytes32[][] calldata merkleProofs
) external virtual whenNotPaused {
ModuleStorage storage $ = _getPufferModuleStorage();
// Anybody can submit a valid proof and the ETH will be sent to the node operator
uint256 ethToSend = 0;
for (uint256 i = 0; i < amounts.length; ++i) {
if ($.claimedRewards[blockNumbers[i]][node]) {
revert AlreadyClaimed(blockNumbers[i], node);
}
bytes32 rewardsRoot = $.rewardsRoots[blockNumbers[i]];
bytes32 leaf = keccak256(bytes.concat(keccak256(abi.encode(node, amounts[i]))));
if (MerkleProof.verifyCalldata(merkleProofs[i], rewardsRoot, leaf)) {
$.claimedRewards[blockNumbers[i]][node] = true;
ethToSend += amounts[i];
}
}
if (ethToSend == 0) {
revert NothingToClaim(node);
}
payable(node).sendValue(ethToSend);
emit RewardsClaimed(node, ethToSend);
}
/**
* @notice Posts the rewards root for this module
* @param root is the Merkle Root hash
* @param blockNumber is the block number for when the Merkle Proof was generated
*/
function postRewardsRoot(bytes32 root, uint256 blockNumber, bytes[] calldata guardianSignatures)
external
virtual
whenNotPaused
{
ModuleStorage storage $ = _getPufferModuleStorage();
if (blockNumber <= $.lastProofOfRewardsBlockNumber) {
revert InvalidBlockNumber(blockNumber);
}
IGuardianModule guardianModule = PUFFER_PROTOCOL.GUARDIAN_MODULE();
bytes32 signedMessageHash = LibGuardianMessages._getModuleRewardsRootMessage($.moduleName, root, blockNumber);
bool validSignatures = guardianModule.validateGuardiansEOASignatures(guardianSignatures, signedMessageHash);
if (!validSignatures) {
revert Unauthorized();
}
$.lastProofOfRewardsBlockNumber = blockNumber;
$.rewardsRoots[blockNumber] = root;
emit RewardsRootPosted(blockNumber, root);
}
/**
* @inheritdoc IPufferModule
* @dev Restricted to PufferModuleManager
*/
function callDelegateTo(
address operator,
ISignatureUtils.SignatureWithExpiry calldata approverSignatureAndExpiry,
bytes32 approverSalt
) external virtual onlyPufferModuleManager {
EIGEN_DELEGATION_MANAGER.delegateTo(operator, approverSignatureAndExpiry, approverSalt);
}
/**
* @inheritdoc IPufferModule
* @dev Restricted to PufferModuleManager
*/
function callUndelegate() external virtual onlyPufferModuleManager returns (bytes32[] memory withdrawalRoot) {
return EIGEN_DELEGATION_MANAGER.undelegate(address(this));
}
/**
* @notice Returns the block number of when the latest rewards proof was posted
*/
function getLastProofOfRewardsBlock() external view returns (uint256) {
ModuleStorage storage $ = _getPufferModuleStorage();
return $.lastProofOfRewardsBlockNumber;
}
/**
* @inheritdoc IPufferModule
*/
function getWithdrawalCredentials() public view returns (bytes memory) {
// Withdrawal credentials for EigenLayer modules are EigenPods
ModuleStorage storage $ = _getPufferModuleStorage();
return abi.encodePacked(bytes1(uint8(1)), bytes11(0), $.eigenPod);
}
/**
* @inheritdoc IPufferModule
*/
function getEigenPod() external view returns (address) {
ModuleStorage storage $ = _getPufferModuleStorage();
return address($.eigenPod);
}
/**
* @inheritdoc IPufferModule
*/
function NAME() external view returns (bytes32) {
ModuleStorage storage $ = _getPufferModuleStorage();
return $.moduleName;
}
function _getPufferModuleStorage() internal pure returns (ModuleStorage storage $) {
// solhint-disable-next-line
assembly {
$.slot := _PUFFER_MODULE_BASE_STORAGE
}
}
}