-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathUser.t.sol
221 lines (174 loc) · 8.18 KB
/
User.t.sol
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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.12;
import "forge-std/Test.sol";
import "src/contracts/core/DelegationManager.sol";
import "src/contracts/core/StrategyManager.sol";
import "src/contracts/pods/EigenPodManager.sol";
import "src/contracts/interfaces/IDelegationManager.sol";
import "src/contracts/interfaces/IStrategy.sol";
import "src/test/integration/TimeMachine.t.sol";
interface IUserDeployer {
function delegationManager() external view returns (DelegationManager);
function strategyManager() external view returns (StrategyManager);
function eigenPodManager() external view returns (EigenPodManager);
function timeMachine() external view returns (TimeMachine);
}
contract User is Test {
Vm cheats = Vm(HEVM_ADDRESS);
DelegationManager delegationManager;
StrategyManager strategyManager;
EigenPodManager eigenPodManager;
TimeMachine timeMachine;
IStrategy constant BEACONCHAIN_ETH_STRAT = IStrategy(0xbeaC0eeEeeeeEEeEeEEEEeeEEeEeeeEeeEEBEaC0);
constructor() {
IUserDeployer deployer = IUserDeployer(msg.sender);
delegationManager = deployer.delegationManager();
strategyManager = deployer.strategyManager();
eigenPodManager = deployer.eigenPodManager();
timeMachine = deployer.timeMachine();
}
modifier createSnapshot() virtual {
timeMachine.createSnapshot();
_;
}
function registerAsOperator() public createSnapshot virtual {
IDelegationManager.OperatorDetails memory details = IDelegationManager.OperatorDetails({
earningsReceiver: address(this),
delegationApprover: address(0),
stakerOptOutWindowBlocks: 0
});
delegationManager.registerAsOperator(details, "metadata");
}
/// @dev For each strategy/token balance, call the relevant deposit method
function depositIntoEigenlayer(IStrategy[] memory strategies, uint[] memory tokenBalances) public createSnapshot virtual {
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
uint tokenBalance = tokenBalances[i];
if (strat == BEACONCHAIN_ETH_STRAT) {
// TODO handle this flow - need to deposit into EPM + prove credentials
revert("depositIntoEigenlayer: unimplemented");
} else {
IERC20 underlyingToken = strat.underlyingToken();
underlyingToken.approve(address(strategyManager), tokenBalance);
strategyManager.depositIntoStrategy(strat, underlyingToken, tokenBalance);
}
}
}
/// @dev Delegate to the operator without a signature
function delegateTo(User operator) public createSnapshot virtual {
ISignatureUtils.SignatureWithExpiry memory emptySig;
delegationManager.delegateTo(address(operator), emptySig, bytes32(0));
}
/// @dev Queues a single withdrawal for every share and strategy pair
function queueWithdrawals(
IStrategy[] memory strategies,
uint[] memory shares
) public createSnapshot virtual returns (IDelegationManager.Withdrawal[] memory, bytes32[] memory) {
address operator = delegationManager.delegatedTo(address(this));
address withdrawer = address(this);
uint nonce = delegationManager.cumulativeWithdrawalsQueued(address(this));
bytes32[] memory withdrawalRoots;
// Create queueWithdrawals params
IDelegationManager.QueuedWithdrawalParams[] memory params = new IDelegationManager.QueuedWithdrawalParams[](1);
params[0] = IDelegationManager.QueuedWithdrawalParams({
strategies: strategies,
shares: shares,
withdrawer: withdrawer
});
// Create Withdrawal struct using same info
IDelegationManager.Withdrawal[] memory withdrawals = new IDelegationManager.Withdrawal[](1);
withdrawals[0] = IDelegationManager.Withdrawal({
staker: address(this),
delegatedTo: operator,
withdrawer: withdrawer,
nonce: nonce,
startBlock: uint32(block.number),
strategies: strategies,
shares: shares
});
withdrawalRoots = delegationManager.queueWithdrawals(params);
// Basic sanity check - we do all other checks outside this file
assertEq(withdrawals.length, withdrawalRoots.length, "User.queueWithdrawals: length mismatch");
return (withdrawals, withdrawalRoots);
}
function completeQueuedWithdrawal(
IDelegationManager.Withdrawal memory withdrawal,
bool receiveAsTokens
) public createSnapshot virtual returns (IERC20[] memory) {
IERC20[] memory tokens = new IERC20[](withdrawal.strategies.length);
for (uint i = 0; i < tokens.length; i++) {
IStrategy strat = withdrawal.strategies[i];
if (strat == BEACONCHAIN_ETH_STRAT) {
tokens[i] = IERC20(address(0));
} else {
tokens[i] = strat.underlyingToken();
}
}
delegationManager.completeQueuedWithdrawal(withdrawal, tokens, 0, receiveAsTokens);
return tokens;
}
}
/// @notice A user contract that implements 1271 signatures
contract User_SignedMethods is User {
mapping(bytes32 => bool) public signedHashes;
constructor() User() {}
function delegateTo(User operator) public createSnapshot override {
// Create empty data
ISignatureUtils.SignatureWithExpiry memory emptySig;
uint256 expiry = type(uint256).max;
// Get signature
ISignatureUtils.SignatureWithExpiry memory stakerSignatureAndExpiry;
stakerSignatureAndExpiry.expiry = expiry;
bytes32 digestHash = delegationManager.calculateCurrentStakerDelegationDigestHash(address(this), address(operator), expiry);
stakerSignatureAndExpiry.signature = bytes(abi.encodePacked(digestHash)); // dummy sig data
// Mark hash as signed
signedHashes[digestHash] = true;
// Delegate
delegationManager.delegateToBySignature(address(this), address(operator), stakerSignatureAndExpiry, emptySig, bytes32(0));
// Mark hash as used
signedHashes[digestHash] = false;
}
function depositIntoEigenlayer(IStrategy[] memory strategies, uint[] memory tokenBalances) public createSnapshot override {
uint256 expiry = type(uint256).max;
for (uint i = 0; i < strategies.length; i++) {
IStrategy strat = strategies[i];
uint tokenBalance = tokenBalances[i];
if (strat == BEACONCHAIN_ETH_STRAT) {
// TODO handle this flow - need to deposit into EPM + prove credentials
revert("depositIntoEigenlayer: unimplemented");
} else {
// Approve token
IERC20 underlyingToken = strat.underlyingToken();
underlyingToken.approve(address(strategyManager), tokenBalance);
// Get signature
uint256 nonceBefore = strategyManager.nonces(address(this));
bytes32 structHash = keccak256(
abi.encode(strategyManager.DEPOSIT_TYPEHASH(), strat, underlyingToken, tokenBalance, nonceBefore, expiry)
);
bytes32 digestHash = keccak256(abi.encodePacked("\x19\x01", strategyManager.domainSeparator(), structHash));
bytes memory signature = bytes(abi.encodePacked(digestHash)); // dummy sig data
// Mark hash as signed
signedHashes[digestHash] = true;
// Deposit
strategyManager.depositIntoStrategyWithSignature(
strat,
underlyingToken,
tokenBalance,
address(this),
expiry,
signature
);
// Mark hash as used
signedHashes[digestHash] = false;
}
}
}
bytes4 internal constant MAGIC_VALUE = 0x1626ba7e;
function isValidSignature(bytes32 hash, bytes memory) external view returns (bytes4) {
if(signedHashes[hash]){
return MAGIC_VALUE;
} else {
return 0xffffffff;
}
}
}