-
Notifications
You must be signed in to change notification settings - Fork 246
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
Uint and Time helpers #375
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ebe222
Add uint64 conversion library
5dce5d6
Add timestamp getter to Initializable and 64 bit version for block
ef93d35
Uint and Time Helpers small changes
0910f19
Time Helpers, add tests
2afd181
Move time helpers to common folder
f065d59
Use Uint256Helpers in TimeHelpers
e7aba48
Time helpers
b36187a
Time helpers: add test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,44 @@ | ||
/* | ||
* SPDX-License-Identitifer: MIT | ||
*/ | ||
|
||
pragma solidity ^0.4.18; | ||
|
||
|
||
contract TimeHelpers { | ||
/** | ||
* @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 uint64(block.number); | ||
} | ||
|
||
/** | ||
* @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 uint64(now); | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this file be in the |
||
|
||
|
||
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,22 @@ | ||
pragma solidity 0.4.18; | ||
|
||
import "../../contracts/lib/misc/TimeHelpers.sol"; | ||
|
||
|
||
contract TimeHelpersMock is TimeHelpers { | ||
function getBlockNumberExt() public view returns (uint256) { | ||
return getBlockNumber(); | ||
} | ||
|
||
function getBlockNumber64Ext() public view returns (uint64) { | ||
return getBlockNumber64(); | ||
} | ||
|
||
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/lib/misc/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,15 @@ | ||
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 number should match") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also test that the returned value is the actual timestamp or block number. |
||
}) | ||
|
||
it('checks time stamp', async () => { | ||
assert.equal((await timeHelpersMock.getTimestampExt.call()).toString(), (await timeHelpersMock.getTimestamp64Ext.call()).toString(), "time stamp should match") | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last thing 😅, I'd move this to
contracts/common
since it's not imported / copied from somewhere else (it's similar toIsContract
, which also lives there).