-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathController_YieldLimitExecHook.sol
More file actions
294 lines (254 loc) · 10.1 KB
/
Controller_YieldLimitExecHook.sol
File metadata and controls
294 lines (254 loc) · 10.1 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "openzeppelin-contracts/contracts/utils/math/Math.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {IStrategy} from "../interfaces/IStrategy.sol";
import {IMintableERC20} from "../interfaces/IMintableERC20.sol";
import "solmate/utils/SafeTransferLib.sol";
import {IConnector} from "../ConnectorPlug.sol";
import "./LimitExecutionHook.sol";
interface IYieldToken {
function updateTotalUnderlyingAssets(uint256 amount_) external;
function calculateMintAmount(uint256 amount_) external returns (uint256);
function convertToShares(
uint256 underlyingAssets
) external view returns (uint256);
function transfer(address to_, uint256 amount_) external returns (bool);
function convertToAssets(uint256 shares) external view returns (uint256);
}
// limits on underlying or visible tokens
contract Controller_YieldLimitExecHook is LimitExecutionHook {
using SafeTransferLib for IMintableERC20;
using FixedPointMathLib for uint256;
uint256 private constant MAX_BPS = 10_000;
IYieldToken public immutable yieldToken__;
// total yield
uint256 public totalUnderlyingAssets;
// if true, no funds can be invested in the strategy
bool public emergencyShutdown;
event ShutdownStateUpdated(bool shutdownState);
modifier notShutdown() {
if (emergencyShutdown) revert VaultShutdown();
_;
}
constructor(
address underlyingAsset_,
address controller_,
address executionHelper_
) LimitExecutionHook(msg.sender, controller_, executionHelper_, true) {
yieldToken__ = IYieldToken(underlyingAsset_);
hookType = LIMIT_EXECUTION_YIELD_TOKEN_HOOK;
_grantRole(LIMIT_UPDATER_ROLE, msg.sender);
}
// assumed transfer info inputs are validated at controller
// transfer info data is untrusted
function srcPreHookCall(
SrcPreHookCallParams calldata params_
)
public
override
notShutdown
returns (TransferInfo memory transferInfo, bytes memory postHookData)
{
super.srcPreHookCall(params_);
uint256 amount = params_.transferInfo.amount;
postHookData = abi.encode(amount);
totalUnderlyingAssets -= amount;
transferInfo = params_.transferInfo;
transferInfo.amount = yieldToken__.convertToShares(amount);
}
function srcPostHookCall(
SrcPostHookCallParams memory srcPostHookCallParams_
)
public
override
isVaultOrController
returns (TransferInfo memory transferInfo)
{
yieldToken__.updateTotalUnderlyingAssets(totalUnderlyingAssets);
transferInfo.receiver = srcPostHookCallParams_.transferInfo.receiver;
transferInfo.extraData = abi.encode(
srcPostHookCallParams_.options,
srcPostHookCallParams_.transferInfo.extraData
);
transferInfo.amount = abi.decode(
srcPostHookCallParams_.postHookData,
(uint256)
);
}
/**
* @notice This function is called before the execution of a destination hook.
* @dev It checks if the sibling chain is supported, consumes a part of the limit, and prepares post-hook data.
*/
function dstPreHookCall(
DstPreHookCallParams calldata params_
)
public
override
notShutdown
isVaultOrController
returns (bytes memory postHookData, TransferInfo memory transferInfo)
{
(uint256 increasedUnderlying, bytes memory payload) = abi.decode(
params_.transferInfo.extraData,
(uint256, bytes)
);
_poolDstHook(params_.connector, increasedUnderlying);
totalUnderlyingAssets += increasedUnderlying;
yieldToken__.updateTotalUnderlyingAssets(totalUnderlyingAssets);
yieldToken__.updateTotalUnderlyingAssets(totalUnderlyingAssets);
if (params_.transferInfo.amount == 0)
return (abi.encode(0, 0, 0, address(0)), transferInfo);
(uint256 consumedUnderlying, uint256 pendingUnderlying) = _limitDstHook(
params_.connector,
params_.transferInfo.amount
);
uint256 sharesToMint = yieldToken__.calculateMintAmount(
params_.transferInfo.amount
);
postHookData = abi.encode(
consumedUnderlying,
pendingUnderlying,
params_.transferInfo.amount,
params_.transferInfo.receiver
);
transferInfo = params_.transferInfo;
if (pendingUnderlying != 0) transferInfo.receiver = address(this);
transferInfo.amount = sharesToMint;
transferInfo.extraData = payload;
}
/**
* @notice Handles post-hook logic after the execution of a destination hook.
* @dev This function processes post-hook data to update the identifier cache and sibling chain cache.
*/
function dstPostHookCall(
DstPostHookCallParams calldata params_
)
public
override
isVaultOrController
notShutdown
returns (CacheData memory cacheData)
{
(
uint256 consumedUnderlying,
uint256 pendingUnderlying,
uint256 depositUnderlying,
address receiver
) = abi.decode(
params_.postHookData,
(uint256, uint256, uint256, address)
);
bytes memory execPayload = params_.transferInfo.extraData;
uint256 connectorPendingShares = _getConnectorPendingAmount(
params_.connectorCache
);
uint256 pendingShares;
if (pendingUnderlying > 0) {
// totalShares * consumedU / totalU
uint256 consumedShares = (params_.transferInfo.amount *
pendingUnderlying) / depositUnderlying;
pendingShares = params_.transferInfo.amount - consumedShares;
cacheData.identifierCache = abi.encode(
params_.transferInfo.receiver,
pendingShares,
params_.connector,
execPayload
);
yieldToken__.transfer(receiver, consumedUnderlying);
emit TokensPending(
params_.connector,
params_.transferInfo.receiver,
consumedShares,
pendingShares,
params_.messageId
);
} else {
if (execPayload.length > 0) {
// execute
bool success = executionHelper__.execute(
params_.transferInfo.receiver,
execPayload,
params_.messageId,
depositUnderlying
);
if (success) {
emit MessageExecuted(
params_.messageId,
params_.transferInfo.receiver
);
cacheData.identifierCache = new bytes(0);
} else
cacheData.identifierCache = abi.encode(
params_.transferInfo.receiver,
0,
params_.connector,
execPayload
);
} else cacheData.identifierCache = new bytes(0);
}
cacheData.connectorCache = abi.encode(
connectorPendingShares + pendingShares
);
}
// /**
// * @notice Handles pre-retry hook logic before execution.
// * @dev This function can be used to mint funds which were in a pending state due to limits.
// * @param siblingChainSlug_ The unique identifier of the sibling chain.
// * @param identifierCache_ Identifier cache containing pending mint information.
// * @param connectorCache_ Sibling chain cache containing pending amount information.
// * @return updatedReceiver The updated receiver of the funds.
// * @return consumedUnderlying The amount consumed from the limit.
// * @return postHookData The post-hook data to be processed after the retry hook execution.
// */
function preRetryHook(
PreRetryHookCallParams calldata params_
)
public
override
isVaultOrController
notShutdown
returns (bytes memory postHookData, TransferInfo memory transferInfo)
{
(
address receiver,
uint256 totalPendingShares,
address connector,
) = abi.decode(
params_.cacheData.identifierCache,
(address, uint256, address, bytes)
);
if (connector != params_.connector) revert InvalidConnector();
(uint256 consumedShares, uint256 pendingShares) = _limitDstHook(
params_.connector,
totalPendingShares
);
postHookData = abi.encode(receiver, consumedShares, pendingShares);
uint256 consumedUnderlying = yieldToken__.convertToAssets(
consumedShares
);
yieldToken__.transfer(receiver, consumedUnderlying);
transferInfo = TransferInfo(transferInfo.receiver, 0, bytes(""));
}
// /**
// * @notice Handles post-retry hook logic after execution.
// * @dev This function updates the identifier cache and sibling chain cache based on the post-hook data.
// * @param siblingChainSlug_ The unique identifier of the sibling chain.
// * @param identifierCache_ Identifier cache containing pending mint information.
// * @param connectorCache_ Sibling chain cache containing pending amount information.
// * @param postHookData_ The post-hook data containing updated receiver and consumed/pending amounts.
// * @return newIdentifierCache The updated identifier cache.
// * @return newConnectorCache The updated sibling chain cache.
// */
function postRetryHook(
PostRetryHookCallParams calldata params_
) public override returns (CacheData memory cacheData) {
return super.postRetryHook(params_);
}
function updateEmergencyShutdownState(
bool shutdownState_
) external onlyOwner {
emergencyShutdown = shutdownState_;
emit ShutdownStateUpdated(shutdownState_);
}
}