-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add uint64 conversion library * Add timestamp getter to Initializable and 64 bit version for block
- Loading branch information
Showing
7 changed files
with
141 additions
and
10 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,48 @@ | ||
/* | ||
* SPDX-License-Identitifer: MIT | ||
*/ | ||
|
||
pragma solidity ^0.4.18; | ||
|
||
import "./Uint256Helpers.sol"; | ||
|
||
|
||
contract TimeHelpers { | ||
using Uint256Helpers for uint256; | ||
|
||
/** | ||
* @dev Returns the current block number. | ||
* Using a function rather than `block.number` allows us to easily mock the block number in | ||
* tests. | ||
*/ | ||
function getBlockNumber() internal view returns (uint256) { | ||
return block.number; | ||
} | ||
|
||
/** | ||
* @dev Returns the current block number, converted to uint64. | ||
* Using a function rather than `block.number` allows us to easily mock the block number in | ||
* tests. | ||
*/ | ||
function getBlockNumber64() internal view returns (uint64) { | ||
return getBlockNumber().toUint64(); | ||
} | ||
|
||
/** | ||
* @dev Returns the current timestamp. | ||
* Using a function rather than `now` allows us to easily mock it in | ||
* tests. | ||
*/ | ||
function getTimestamp() internal view returns (uint256) { | ||
return now; | ||
} | ||
|
||
/** | ||
* @dev Returns the current timestamp, covnerted to uint64. | ||
* Using a function rather than `now` allows us to easily mock it in | ||
* tests. | ||
*/ | ||
function getTimestamp64() internal view returns (uint64) { | ||
return getTimestamp().toUint64(); | ||
} | ||
} |
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,11 @@ | ||
pragma solidity ^0.4.18; | ||
|
||
|
||
library Uint256Helpers { | ||
uint256 public constant MAX_UINT64 = uint64(-1); | ||
|
||
function toUint64(uint256 a) internal pure returns (uint64) { | ||
require(a <= MAX_UINT64); | ||
return uint64(a); | ||
} | ||
} |
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,21 @@ | ||
const { assertRevert } = require('./helpers/assertThrow') | ||
|
||
contract('Uint256 Helpers test', accounts => { | ||
let uint256Mock | ||
|
||
before(async () => { | ||
uint256Mock = await artifacts.require('Uint256Mock').new() | ||
}) | ||
|
||
it('converts from uint256 to uint64', async () => { | ||
const a = 1234 | ||
assert.equal((await uint256Mock.convert.call(a)).toString(), a, "Values should match") | ||
}) | ||
|
||
it('fails converting from uint256 to uint64 if too big', async () => { | ||
const a = new web3.BigNumber(2).pow(64) | ||
return assertRevert(async () => { | ||
await uint256Mock.convert(a) | ||
}) | ||
}) | ||
}) |
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,30 @@ | ||
pragma solidity 0.4.18; | ||
|
||
import "../../contracts/common/TimeHelpers.sol"; | ||
|
||
|
||
contract TimeHelpersMock is TimeHelpers { | ||
function getBlockNumberDirect() public view returns (uint256) { | ||
return block.number; | ||
} | ||
|
||
function getBlockNumberExt() public view returns (uint256) { | ||
return getBlockNumber(); | ||
} | ||
|
||
function getBlockNumber64Ext() public view returns (uint64) { | ||
return getBlockNumber64(); | ||
} | ||
|
||
function getTimestampDirect() public view returns (uint256) { | ||
return now; | ||
} | ||
|
||
function getTimestampExt() public view returns (uint256) { | ||
return getTimestamp(); | ||
} | ||
|
||
function getTimestamp64Ext() public view returns (uint64) { | ||
return getTimestamp64(); | ||
} | ||
} |
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,12 @@ | ||
pragma solidity 0.4.18; | ||
|
||
import "../../contracts/common/Uint256Helpers.sol"; | ||
|
||
|
||
contract Uint256Mock { | ||
using Uint256Helpers for uint256; | ||
|
||
function convert(uint256 a) public pure returns (uint64) { | ||
return a.toUint64(); | ||
} | ||
} |
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,17 @@ | ||
contract('TimeHelpers test', accounts => { | ||
let timeHelpersMock | ||
|
||
before(async () => { | ||
timeHelpersMock = await artifacts.require('TimeHelpersMock').new() | ||
}) | ||
|
||
it('checks block number', async () => { | ||
assert.equal((await timeHelpersMock.getBlockNumberExt.call()).toString(), (await timeHelpersMock.getBlockNumber64Ext.call()).toString(), "block numbers should match") | ||
assert.equal((await timeHelpersMock.getBlockNumberExt.call()).toString(), (await timeHelpersMock.getBlockNumberDirect.call()).toString(), web3.eth.blockNumber, "block number should match with real one", "block number should match with real one") | ||
}) | ||
|
||
it('checks time stamp', async () => { | ||
assert.equal((await timeHelpersMock.getTimestampExt.call()).toString(), (await timeHelpersMock.getTimestamp64Ext.call()).toString(), "time stamps should match") | ||
assert.equal((await timeHelpersMock.getTimestampExt.call()).toString(), (await timeHelpersMock.getTimestampDirect.call()).toString(), "time stamp should match with real one") | ||
}) | ||
}) |