Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeManagerV5 Contract #12

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions contracts/TimeManagerV5.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
/// @dev Sets true when block timestamp is used
bool public isTimeBased;

/// @dev Sets true when contract is initialized
bool private isInitialized;

/**
* @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[46] private __gap;
chechu marked this conversation as resolved.
Show resolved Hide resolved

/**
* @dev Retrieves the current slot
Expand All @@ -25,29 +28,33 @@
function() view returns (uint256) private _getCurrentSlot;

/**
* @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();
}

/**
* @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");

Check warning on line 45 in contracts/TimeManagerV5.sol

View workflow job for this annotation

GitHub Actions / Lint

Use Custom Errors instead of revert statements

if (!timeBased_ && blocksPerYear_ == 0) {
revert("Invalid blocks per year");

Check warning on line 48 in contracts/TimeManagerV5.sol

View workflow job for this annotation

GitHub Actions / Lint

Use Custom Errors instead of revert statements
}
if (timeBased_ && blocksPerYear_ != 0) {
revert("Invalid time based configuration");

Check warning on line 51 in contracts/TimeManagerV5.sol

View workflow job for this annotation

GitHub Actions / Lint

Use Custom Errors instead of revert statements
}

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;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions contracts/test/ScenarioTimeManagerV5.sol
Original file line number Diff line number Diff line change
@@ -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_);
}
}
10 changes: 6 additions & 4 deletions tests/hardhat/TimeManagerV5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
Loading