-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from VenusProtocol/develop
New release
- Loading branch information
Showing
7 changed files
with
223 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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 | ||
bool public isTimeBased; | ||
|
||
/** | ||
* @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; | ||
|
||
/** | ||
* @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"); | ||
} | ||
if (timeBased_ && blocksPerYear_ != 0) { | ||
revert("Invalid time based configuration"); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
/** | ||
* @dev Returns the current timestamp in seconds | ||
* @return The current timestamp | ||
*/ | ||
function _getBlockTimestamp() private view returns (uint256) { | ||
return block.timestamp; | ||
} | ||
|
||
/** | ||
* @dev Returns the current block number | ||
* @return The current block number | ||
*/ | ||
function _getBlockNumber() private view returns (uint256) { | ||
return block.number; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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; | ||
|
||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
bool public immutable isTimeBased; | ||
|
||
/** | ||
* @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[48] private __gap; | ||
|
||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
function() view returns (uint256) private immutable _getCurrentSlot; | ||
|
||
/// @notice Thrown when blocks per year is invalid | ||
error InvalidBlocksPerYear(); | ||
|
||
/// @notice Thrown when time based but blocks per year is provided | ||
error InvalidTimeBasedConfiguration(); | ||
|
||
/** | ||
* @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 and timeBased is false | ||
* @custom:error InvalidTimeBasedConfiguration is thrown if blocksPerYear entered is non zero and timeBased is true | ||
* @custom:oz-upgrades-unsafe-allow constructor | ||
*/ | ||
constructor(bool timeBased_, uint256 blocksPerYear_) { | ||
if (!timeBased_ && blocksPerYear_ == 0) { | ||
revert InvalidBlocksPerYear(); | ||
} | ||
|
||
if (timeBased_ && blocksPerYear_ != 0) { | ||
revert InvalidTimeBasedConfiguration(); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
/** | ||
* @dev Returns the current timestamp in seconds | ||
* @return The current timestamp | ||
*/ | ||
function _getBlockTimestamp() private view returns (uint256) { | ||
return block.timestamp; | ||
} | ||
|
||
/** | ||
* @dev Returns the current block number | ||
* @return The current block number | ||
*/ | ||
function _getBlockNumber() private view returns (uint256) { | ||
return block.number; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |