-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathVersionedInitializable.sol
50 lines (45 loc) · 1.91 KB
/
VersionedInitializable.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import {Errors} from 'contracts/libraries/constants/Errors.sol';
import {StorageLib} from 'contracts/libraries/StorageLib.sol';
/**
* @title VersionedInitializable
*
* @dev Helper contract to implement initializer functions. To use it, replace
* the constructor with a function that has the `initializer` modifier.
* WARNING: Unlike constructors, initializer functions must be manually
* invoked. This applies both to deploying an Initializable contract, as well
* as extending an Initializable contract via inheritance.
* WARNING: When used with inheritance, manual care must be taken to not invoke
* a parent initializer twice, or ensure that all initializers are idempotent,
* because this is not dealt with automatically as with constructors.
*
* This is slightly modified from [Aave's version.](https://github.com/aave/protocol-v2/blob/6a503eb0a897124d8b9d126c915ffdf3e88343a9/contracts/protocol/libraries/aave-upgradeability/VersionedInitializable.sol)
*
* @author Lens Protocol, inspired by Aave's implementation, which is in turn inspired by OpenZeppelin's
* Initializable contract
*/
abstract contract VersionedInitializable {
address private immutable originalImpl;
/**
* @dev Modifier to use in the initializer function of a contract.
*/
modifier initializer() {
if (address(this) == originalImpl) {
revert Errors.CannotInitImplementation();
}
if (getRevision() <= StorageLib.getLastInitializedRevision()) {
revert Errors.Initialized();
}
StorageLib.setLastInitializedRevision(getRevision());
_;
}
constructor() {
originalImpl = address(this);
}
/**
* @dev returns the revision number of the contract
* Needs to be defined in the inherited class as a constant.
**/
function getRevision() internal pure virtual returns (uint256);
}