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

[VEN-2258]: add TimeManagerV5 and TimeManagerV8 #8

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
61 changes: 61 additions & 0 deletions contracts/TimeManagerV5.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.5.16;

contract TimeManagerV5 {
/// @dev The approximate number of seconds per year
uint256 public constant SECONDS_PER_YEAR = 31_536_000;

/// @notice Number of blocks per year or seconds per year
uint256 public blocksOrSecondsPerYear;

/// @dev Sets true when block timestamp is used
Debugger022 marked this conversation as resolved.
Show resolved Hide resolved
bool public isTimeBased;

GitGuru7 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @dev Retrieves the current slot
* @return Current slot
*/
function() view returns (uint256) private _getCurrentSlot;

/**
* @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 {
if (!timeBased_ && blocksPerYear_ == 0) {
revert("Invalid blocks per year");

Check warning on line 27 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");

Check warning on line 30 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();
}

/**
* @notice Returns the current timestamp in seconds
Debugger022 marked this conversation as resolved.
Show resolved Hide resolved
* @return The current timestamp
*/
function _getBlockTimestamp() private view returns (uint256) {
return block.timestamp;
}

/**
* @notice Returns the current block number
* @return The current block number
*/
function _getBlockNumber() private view returns (uint256) {
return block.number;
}
}
66 changes: 66 additions & 0 deletions contracts/TimeManagerV8.sol
chechu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: BSD-3-Clause
pragma solidity 0.8.13;

import { SECONDS_PER_YEAR } from "./constants.sol";

abstract contract TimeManagerV8 {
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
uint256 public immutable blocksOrSecondsPerYear;

Check warning on line 8 in contracts/TimeManagerV8.sol

View workflow job for this annotation

GitHub Actions / Lint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
bool public immutable isTimeBased;

Check warning on line 11 in contracts/TimeManagerV8.sol

View workflow job for this annotation

GitHub Actions / Lint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
function() view returns (uint256) private immutable _getCurrentSlot;

Check warning on line 14 in contracts/TimeManagerV8.sol

View workflow job for this annotation

GitHub Actions / Lint

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @notice Thrown when blocks per year is invalid
error InvalidBlocksPerYear();

/// @notice Thrown when time based but blocks per year is provided
Debugger022 marked this conversation as resolved.
Show resolved Hide resolved
error InvalidTimeBased();

/**
* @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
* @custom:error InvalidBlocksPerYear is thrown if blocksPerYear entered is zero
Debugger022 marked this conversation as resolved.
Show resolved Hide resolved
* @custom:oz-upgrades-unsafe-allow constructor
*/
constructor(bool timeBased_, uint256 blocksPerYear_) {
if (!timeBased_ && blocksPerYear_ == 0) {
revert InvalidBlocksPerYear();
}

if (timeBased_ && blocksPerYear_ != 0) {
revert InvalidTimeBased();
Debugger022 marked this conversation as resolved.
Show resolved Hide resolved
}

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 virtual returns (uint256) {
return _getCurrentSlot();
}

/**
* @notice Returns the current timestamp in seconds
* @return The current timestamp
*/
function _getBlockTimestamp() private view returns (uint256) {
return block.timestamp;
}

/**
* @notice Returns the current block number
* @return The current block number
*/
function _getBlockNumber() private view returns (uint256) {
return block.number;
}
}
16 changes: 16 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ const config: HardhatUserConfig = {
},
},
},
{
version: "0.5.16",
settings: {
optimizer: {
enabled: true,
details: {
yul: !process.env.CI,
},
},
outputSelection: {
"*": {
"*": ["storageLayout"],
},
},
},
},
],
},
networks: {
Expand Down
34 changes: 34 additions & 0 deletions tests/hardhat/TimeManagerV5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import chai from "chai";
import { ethers } from "hardhat";

const { expect } = chai;

describe("TimeManagerV5: tests", async () => {
let timeManager: ethers.Contracts;
describe("For block number", async () => {
const blocksPerYear = 10512000;
beforeEach(async () => {
const timeManagerV5 = await ethers.getContractFactory("TimeManagerV5");
timeManager = await timeManagerV5.deploy(false, blocksPerYear);
});

it("Retrieves block timestamp", async () => {
const currentBlockNumber = (await ethers.provider.getBlock("latest")).number;
expect(await timeManager.getBlockNumberOrTimestamp()).to.be.equal(currentBlockNumber);
expect(await timeManager.blocksOrSecondsPerYear()).to.be.equal(blocksPerYear);
});
});
describe("For block timestamp", async () => {
beforeEach(async () => {
const timeManagerV5 = await ethers.getContractFactory("TimeManagerV5");
timeManager = await timeManagerV5.deploy(true, 0);
});

it("Retrieves block timestamp", async () => {
const secondsPerYear = await timeManager.SECONDS_PER_YEAR();
const currentBlocktimestamp = (await ethers.provider.getBlock("latest")).timestamp;
expect(await timeManager.getBlockNumberOrTimestamp()).to.be.equal(currentBlocktimestamp);
expect(await timeManager.blocksOrSecondsPerYear()).to.be.equal(secondsPerYear);
});
});
});
Loading