-
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
Changes from 2 commits
5ebe222
5dce5d6
ef93d35
0910f19
2afd181
f065d59
e7aba48
b36187a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pragma solidity ^0.4.18; | ||
|
||
|
||
library Uint64Helpers { | ||
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. I'd call this |
||
uint256 public constant MAX_UINT64 = uint64(-1); | ||
|
||
function toUint64(uint256 a) internal pure returns (uint64) { | ||
require(a <= MAX_UINT64); | ||
return uint64(a); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { assertRevert } = require('./helpers/assertThrow') | ||
|
||
contract('Uint64 Helpers test', accounts => { | ||
let uint64Mock | ||
|
||
before(async () => { | ||
uint64Mock = await artifacts.require('Uint64Mock').new() | ||
}) | ||
|
||
it('converts from uint256 to uint64', async () => { | ||
const a = 1234 | ||
assert.equal((await uint64Mock.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 () => { | ||
assert.equal((await uint64Mock.convert(a)).toString(), a, "Values 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. Small nitpick: this can just be 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. Haha, yes, problems of copy-pasting... |
||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pragma solidity 0.4.18; | ||
|
||
import "../../contracts/lib/misc/Uint64Helpers.sol"; | ||
|
||
|
||
contract Uint64Mock { | ||
using Uint64Helpers for uint256; | ||
|
||
function convert(uint256 a) public pure returns (uint64) { | ||
return a.toUint64(); | ||
} | ||
} |
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.
I think it'd be nice if we moved these time helpers into their own contract file, e.g.
TimeHelpers
.