diff --git a/contracts/TimeManagerV5.sol b/contracts/TimeManagerV5.sol index 59ba7c3..52167d1 100644 --- a/contracts/TimeManagerV5.sol +++ b/contracts/TimeManagerV5.sol @@ -11,25 +11,39 @@ contract TimeManagerV5 { /// @dev Sets true when block timestamp is used bool public isTimeBased; + /// @dev Sets true when contract is initialized + bool private isInitialized; + + /** + * @dev Retrieves the current slot + * @return Current slot + */ + function() view returns (uint256) private _getCurrentSlot; + /** * @dev This empty reserved space is put in place to allow future versions to add new * variables without shifting down storage in the inheritance chain * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps */ - uint256[47] private __gap; + uint256[48] private __gap; /** - * @dev Retrieves the current slot - * @return Current slot + * @dev Function to simply retrieve block number or block timestamp + * @return Current block number or block timestamp */ - function() view returns (uint256) private _getCurrentSlot; + function getBlockNumberOrTimestamp() public view returns (uint256) { + return _getCurrentSlot(); + } /** + * @dev Initializes the contract to use either blocks or seconds * @param timeBased_ A boolean indicating whether the contract is based on time or block * If timeBased is true than blocksPerYear_ param is ignored as blocksOrSecondsPerYear is set to SECONDS_PER_YEAR * @param blocksPerYear_ The number of blocks per year */ - constructor(bool timeBased_, uint256 blocksPerYear_) public { + function _initializeTimeManager(bool timeBased_, uint256 blocksPerYear_) internal { + if (isInitialized) revert("Already initialized TimeManager"); + if (!timeBased_ && blocksPerYear_ == 0) { revert("Invalid blocks per year"); } @@ -40,14 +54,7 @@ contract TimeManagerV5 { isTimeBased = timeBased_; blocksOrSecondsPerYear = timeBased_ ? SECONDS_PER_YEAR : blocksPerYear_; _getCurrentSlot = timeBased_ ? _getBlockTimestamp : _getBlockNumber; - } - - /** - * @dev Function to simply retrieve block number or block timestamp - * @return Current block number or block timestamp - */ - function getBlockNumberOrTimestamp() public view returns (uint256) { - return _getCurrentSlot(); + isInitialized = true; } /** diff --git a/contracts/test/ScenarioTimeManagerV5.sol b/contracts/test/ScenarioTimeManagerV5.sol new file mode 100644 index 0000000..26ef5c6 --- /dev/null +++ b/contracts/test/ScenarioTimeManagerV5.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-3-Clause +pragma solidity 0.5.16; + +import { TimeManagerV5 } from "../TimeManagerV5.sol"; + +contract ScenarioTimeManagerV5 is TimeManagerV5 { + function initializeTimeManager(bool timeBased_, uint256 blocksPerYear_) external { + _initializeTimeManager(timeBased_, blocksPerYear_); + } +} diff --git a/tests/hardhat/TimeManagerV5.ts b/tests/hardhat/TimeManagerV5.ts index f27c852..caf9413 100644 --- a/tests/hardhat/TimeManagerV5.ts +++ b/tests/hardhat/TimeManagerV5.ts @@ -8,8 +8,9 @@ describe("TimeManagerV5: tests", async () => { describe("For block number", async () => { const blocksPerYear = 10512000; beforeEach(async () => { - const timeManagerV5 = await ethers.getContractFactory("TimeManagerV5"); - timeManager = await timeManagerV5.deploy(false, blocksPerYear); + const timeManagerV5 = await ethers.getContractFactory("ScenarioTimeManagerV5"); + timeManager = await timeManagerV5.deploy(); + await timeManager.initializeTimeManager(false, blocksPerYear); }); it("Retrieves block timestamp", async () => { @@ -20,8 +21,9 @@ describe("TimeManagerV5: tests", async () => { }); describe("For block timestamp", async () => { beforeEach(async () => { - const timeManagerV5 = await ethers.getContractFactory("TimeManagerV5"); - timeManager = await timeManagerV5.deploy(true, 0); + const timeManagerV5 = await ethers.getContractFactory("ScenarioTimeManagerV5"); + timeManager = await timeManagerV5.deploy(); + await timeManager.initializeTimeManager(true, 0); }); it("Retrieves block timestamp", async () => {