Skip to content

Commit

Permalink
Merge pull request #12 from VenusProtocol/fix/time-manager-v5
Browse files Browse the repository at this point in the history
TimeManagerV5 Contract
  • Loading branch information
chechu authored Apr 19, 2024
2 parents c5bfe3a + 7d24a65 commit 670fe1e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
33 changes: 20 additions & 13 deletions contracts/TimeManagerV5.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");

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
}
Expand All @@ -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;
}

/**
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

0 comments on commit 670fe1e

Please sign in to comment.