-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathBaseForm.sol
321 lines (265 loc) · 11.3 KB
/
BaseForm.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
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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.23;
import { IBaseForm } from "src/interfaces/IBaseForm.sol";
import { ISuperRegistry } from "src/interfaces/ISuperRegistry.sol";
import { ISuperformFactory } from "src/interfaces/ISuperformFactory.sol";
import { IEmergencyQueue } from "src/interfaces/IEmergencyQueue.sol";
import { DataLib } from "src/libraries/DataLib.sol";
import { Error } from "src/libraries/Error.sol";
import { InitSingleVaultData } from "src/types/DataTypes.sol";
import { Initializable } from "openzeppelin-contracts/contracts/proxy/utils/Initializable.sol";
import { ERC165 } from "openzeppelin-contracts/contracts/utils/introspection/ERC165.sol";
import { IERC165 } from "openzeppelin-contracts/contracts/utils/introspection/IERC165.sol";
/// @title BaseForm
/// @dev Abstract contract to be inherited by different Form implementations
/// @author Zeropoint Labs
abstract contract BaseForm is IBaseForm, Initializable, ERC165 {
using DataLib for uint256;
//////////////////////////////////////////////////////////////
// CONSTANTS //
//////////////////////////////////////////////////////////////
ISuperRegistry public immutable superRegistry;
uint64 public immutable CHAIN_ID;
//////////////////////////////////////////////////////////////
// STATE VARIABLES //
//////////////////////////////////////////////////////////////
/// @dev the address of the vault that was added
address public vault;
/// @dev underlying asset of vault this form pertains to
address public asset;
//////////////////////////////////////////////////////////////
// MODIFIERS //
//////////////////////////////////////////////////////////////
modifier notPaused(InitSingleVaultData memory singleVaultData_) {
if (
!ISuperformFactory(superRegistry.getAddress(keccak256("SUPERFORM_FACTORY"))).isSuperform(
singleVaultData_.superformId
)
) {
revert Error.SUPERFORM_ID_NONEXISTENT();
}
(, uint32 formImplementationId_,) = singleVaultData_.superformId.getSuperform();
if (
ISuperformFactory(superRegistry.getAddress(keccak256("SUPERFORM_FACTORY"))).isFormImplementationPaused(
formImplementationId_
)
) revert Error.PAUSED();
_;
}
modifier onlySuperRouter() {
if (superRegistry.getAddress(keccak256("SUPERFORM_ROUTER")) != msg.sender) revert Error.NOT_SUPERFORM_ROUTER();
_;
}
modifier onlyCoreStateRegistry() {
if (superRegistry.getAddress(keccak256("CORE_STATE_REGISTRY")) != msg.sender) {
revert Error.NOT_CORE_STATE_REGISTRY();
}
_;
}
modifier onlyEmergencyQueue() {
if (msg.sender != superRegistry.getAddress(keccak256("EMERGENCY_QUEUE"))) {
revert Error.NOT_EMERGENCY_QUEUE();
}
_;
}
//////////////////////////////////////////////////////////////
// CONSTRUCTOR //
//////////////////////////////////////////////////////////////
constructor(address superRegistry_) {
if (superRegistry_ == address(0)) {
revert Error.ZERO_ADDRESS();
}
if (block.chainid > type(uint64).max) {
revert Error.BLOCK_CHAIN_ID_OUT_OF_BOUNDS();
}
CHAIN_ID = uint64(block.chainid);
superRegistry = ISuperRegistry(superRegistry_);
_disableInitializers();
}
//////////////////////////////////////////////////////////////
// EXTERNAL VIEW FUNCTIONS //
//////////////////////////////////////////////////////////////
/// @inheritdoc IBaseForm
function superformYieldTokenName() external view virtual override returns (string memory);
/// @inheritdoc IBaseForm
function superformYieldTokenSymbol() external view virtual override returns (string memory);
/// @inheritdoc IBaseForm
function getStateRegistryId() external view virtual override returns (uint8);
// @inheritdoc IBaseForm
function getVaultAddress() external view override returns (address) {
return vault;
}
// @inheritdoc IBaseForm
function getVaultAsset() public view override returns (address) {
return asset;
}
/// @inheritdoc IBaseForm
function getVaultName() public view virtual override returns (string memory);
/// @inheritdoc IBaseForm
function getVaultSymbol() public view virtual override returns (string memory);
/// @inheritdoc IBaseForm
function getVaultDecimals() public view virtual override returns (uint256);
/// @inheritdoc IBaseForm
function getPricePerVaultShare() public view virtual override returns (uint256);
/// @inheritdoc IBaseForm
function getVaultShareBalance() public view virtual override returns (uint256);
/// @inheritdoc IBaseForm
function getTotalAssets() public view virtual override returns (uint256);
/// @inheritdoc IBaseForm
function getTotalSupply() public view virtual override returns (uint256);
// @inheritdoc IBaseForm
function getPreviewPricePerVaultShare() public view virtual override returns (uint256);
/// @inheritdoc IBaseForm
function previewDepositTo(uint256 assets_) public view virtual override returns (uint256);
/// @inheritdoc IBaseForm
function previewWithdrawFrom(uint256 assets_) public view virtual override returns (uint256);
/// @inheritdoc IBaseForm
function previewRedeemFrom(uint256 shares_) public view virtual override returns (uint256);
//////////////////////////////////////////////////////////////
// EXTERNAL WRITE FUNCTIONS //
//////////////////////////////////////////////////////////////
/// @param superRegistry_ ISuperRegistry address deployed
/// @param vault_ The vault address this form pertains to
/// @param asset_ The underlying asset address of the vault this form pertains to
function initialize(address superRegistry_, address vault_, address asset_) external initializer {
if (ISuperRegistry(superRegistry_) != superRegistry) revert Error.NOT_SUPER_REGISTRY();
if (vault_ == address(0) || asset_ == address(0)) revert Error.ZERO_ADDRESS();
vault = vault_;
asset = asset_;
}
/// @inheritdoc IBaseForm
function directDepositIntoVault(
InitSingleVaultData memory singleVaultData_,
address srcSender_
)
external
payable
override
onlySuperRouter
notPaused(singleVaultData_)
returns (uint256 shares)
{
shares = _directDepositIntoVault(singleVaultData_, srcSender_);
}
/// @inheritdoc IBaseForm
function directWithdrawFromVault(
InitSingleVaultData memory singleVaultData_,
address srcSender_
)
external
override
onlySuperRouter
returns (uint256 assets)
{
if (!_isPaused(singleVaultData_.superformId)) {
assets = _directWithdrawFromVault(singleVaultData_, srcSender_);
} else {
IEmergencyQueue(superRegistry.getAddress(keccak256("EMERGENCY_QUEUE"))).queueWithdrawal(singleVaultData_);
}
}
/// @inheritdoc IBaseForm
function xChainDepositIntoVault(
InitSingleVaultData memory singleVaultData_,
address srcSender_,
uint64 srcChainId_
)
external
override
onlyCoreStateRegistry
notPaused(singleVaultData_)
returns (uint256 shares)
{
if (srcChainId_ != 0 && srcChainId_ != CHAIN_ID) {
shares = _xChainDepositIntoVault(singleVaultData_, srcSender_, srcChainId_);
} else {
revert Error.INVALID_CHAIN_ID();
}
}
/// @inheritdoc IBaseForm
function xChainWithdrawFromVault(
InitSingleVaultData memory singleVaultData_,
address srcSender_,
uint64 srcChainId_
)
external
override
onlyCoreStateRegistry
returns (uint256 assets)
{
if (srcChainId_ != 0 && srcChainId_ != CHAIN_ID) {
if (!_isPaused(singleVaultData_.superformId)) {
assets = _xChainWithdrawFromVault(singleVaultData_, srcSender_, srcChainId_);
} else {
IEmergencyQueue(superRegistry.getAddress(keccak256("EMERGENCY_QUEUE"))).queueWithdrawal(
singleVaultData_
);
}
} else {
revert Error.INVALID_CHAIN_ID();
}
}
/// @inheritdoc IBaseForm
function emergencyWithdraw(address receiverAddress_, uint256 amount_) external override onlyEmergencyQueue {
_emergencyWithdraw(receiverAddress_, amount_);
}
/// @inheritdoc IBaseForm
function forwardDustToPaymaster(address token_) external override {
if (token_ == vault) revert Error.CANNOT_FORWARD_4646_TOKEN();
_forwardDustToPaymaster(token_);
}
/// @dev Checks if the Form implementation has the appropriate interface support
/// @param interfaceId_ is the interfaceId to check
function supportsInterface(bytes4 interfaceId_) public view virtual override(ERC165, IERC165) returns (bool) {
return interfaceId_ == type(IBaseForm).interfaceId || super.supportsInterface(interfaceId_);
}
//////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS //
//////////////////////////////////////////////////////////////
/// @dev Deposits underlying tokens into a vault
function _directDepositIntoVault(
InitSingleVaultData memory singleVaultData_,
address srcSender_
)
internal
virtual
returns (uint256 shares);
/// @dev Deposits underlying tokens into a vault
function _xChainDepositIntoVault(
InitSingleVaultData memory singleVaultData_,
address srcSender_,
uint64 srcChainId_
)
internal
virtual
returns (uint256 shares);
/// @dev Withdraws underlying tokens from a vault
function _directWithdrawFromVault(
InitSingleVaultData memory singleVaultData_,
address srcSender_
)
internal
virtual
returns (uint256 assets);
/// @dev Withdraws underlying tokens from a vault
function _xChainWithdrawFromVault(
InitSingleVaultData memory singleVaultData_,
address srcSender_,
uint64 srcChainId_
)
internal
virtual
returns (uint256 assets);
/// @dev withdraws vault shares from form during emergency
function _emergencyWithdraw(address receiverAddress_, uint256 amount_) internal virtual;
/// @dev forwards dust to paymaster
function _forwardDustToPaymaster(address token_) internal virtual;
/// @dev returns if a form id is paused
function _isPaused(uint256 superformId) internal view returns (bool) {
address factory = superRegistry.getAddress(keccak256("SUPERFORM_FACTORY"));
if (!ISuperformFactory(factory).isSuperform(superformId)) {
revert Error.SUPERFORM_ID_NONEXISTENT();
}
(, uint32 formImplementationId_,) = superformId.getSuperform();
return ISuperformFactory(factory).isFormImplementationPaused(formImplementationId_);
}
}