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

Remove moment.js dependencies #368

Merged
merged 4 commits into from
Aug 16, 2017
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"coveralls": "^2.13.1",
"ethereumjs-testrpc": "^3.0.2",
"mocha-lcov-reporter": "^1.3.0",
"moment": "^2.18.1",
"solidity-coverage": "^0.2.1",
"truffle": "^3.4.6",
"truffle-hdwallet-provider": "0.0.3"
Expand Down
2 changes: 1 addition & 1 deletion test/CappedCrowdsale.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract('CappedCrowdsale', function ([_, wallet]) {
})

beforeEach(async function () {
this.startTime = latestTime().unix() + duration.weeks(1);
this.startTime = latestTime() + duration.weeks(1);
this.endTime = this.startTime + duration.weeks(1);

this.crowdsale = await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, cap)
Expand Down
2 changes: 1 addition & 1 deletion test/Crowdsale.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
})

beforeEach(async function () {
this.startTime = latestTime().unix() + duration.weeks(1);
this.startTime = latestTime() + duration.weeks(1);
this.endTime = this.startTime + duration.weeks(1);
this.afterEndTime = this.endTime + duration.seconds(1)

Expand Down
2 changes: 1 addition & 1 deletion test/FinalizableCrowdsale.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) {
})

beforeEach(async function () {
this.startTime = latestTime().unix() + duration.weeks(1)
this.startTime = latestTime() + duration.weeks(1)
this.endTime = this.startTime + duration.weeks(1)
this.afterEndTime = this.endTime + duration.seconds(1)

Expand Down
2 changes: 1 addition & 1 deletion test/RefundableCrowdsale.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) {
})

beforeEach(async function () {
this.startTime = latestTime().unix() + duration.weeks(1)
this.startTime = latestTime() + duration.weeks(1)
this.endTime = this.startTime + duration.weeks(1)
this.afterEndTime = this.endTime + duration.seconds(1)

Expand Down
2 changes: 1 addition & 1 deletion test/SampleCrowdsale.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract('Crowdsale', function ([owner, wallet, investor]) {
})

beforeEach(async function () {
this.startTime = latestTime().unix() + duration.weeks(1);
this.startTime = latestTime() + duration.weeks(1);
this.endTime = this.startTime + duration.weeks(1);
this.afterEndTime = this.endTime + duration.seconds(1);

Expand Down
13 changes: 6 additions & 7 deletions test/TokenTimelock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ require('chai')
.use(require('chai-bignumber')(BigNumber))
.should()

import moment from 'moment'

import latestTime from './helpers/latestTime'
import increaseTime from './helpers/increaseTime'
import {increaseTimeTo, duration} from './helpers/increaseTime'

const MintableToken = artifacts.require('MintableToken')
const TokenTimelock = artifacts.require('TokenTimelock')
Expand All @@ -19,7 +18,7 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) {

beforeEach(async function () {
this.token = await MintableToken.new({from: owner})
this.releaseTime = latestTime().add(1, 'year').unix()
this.releaseTime = latestTime() + duration.years(1)
this.timelock = await TokenTimelock.new(this.token.address, beneficiary, this.releaseTime)
await this.token.mint(this.timelock.address, amount, {from: owner})
})
Expand All @@ -29,26 +28,26 @@ contract('TokenTimelock', function ([_, owner, beneficiary]) {
})

it('cannot be released just before time limit', async function () {
await increaseTime(moment.duration(0.99, 'year').asSeconds())
await increaseTimeTo(this.releaseTime - duration.seconds(3))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this works. Given the lack of precision of increaseTimeTo that we've discussed, I would've thought 3 seconds was not enough to make up for it. Did you try other values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As testrpc is run on localhost the lag is usually up to 1s. Due to time rounding, subtracting a single second is not enough to have a robust behaviour but 3s seems to be safe enough.

Obviously, the situation may change if someone decides to run tests across testrpc instance connected. But, to be honest, I don't expect it to be a common practice.

await this.timelock.release().should.be.rejected
})

it('can be released just after limit', async function () {
await increaseTime(moment.duration(1.01, 'year').asSeconds())
await increaseTimeTo(this.releaseTime + duration.seconds(1))
await this.timelock.release().should.be.fulfilled
const balance = await this.token.balanceOf(beneficiary)
balance.should.be.bignumber.equal(amount)
})

it('can be released after time limit', async function () {
await increaseTime(moment.duration(2, 'year').asSeconds())
await increaseTimeTo(this.releaseTime + duration.years(1))
await this.timelock.release().should.be.fulfilled
const balance = await this.token.balanceOf(beneficiary)
balance.should.be.bignumber.equal(amount)
})

it('cannot be released twice', async function () {
await increaseTime(moment.duration(2, 'year').asSeconds())
await increaseTimeTo(this.releaseTime + duration.years(1))
await this.timelock.release().should.be.fulfilled
await this.timelock.release().should.be.rejected
const balance = await this.token.balanceOf(beneficiary)
Expand Down
6 changes: 4 additions & 2 deletions test/helpers/increaseTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function increaseTime(duration) {
* @param target time in seconds
*/
export function increaseTimeTo(target) {
let now = latestTime().unix();
let now = latestTime();
if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`);
let diff = target - now;
return increaseTime(diff);
Expand All @@ -43,5 +43,7 @@ export const duration = {
minutes: function(val) { return val * this.seconds(60) },
hours: function(val) { return val * this.minutes(60) },
days: function(val) { return val * this.hours(24) },
weeks: function(val) { return val * this.days(7) }
weeks: function(val) { return val * this.days(7) },
months: function(val) { return val * this.days(30)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we should only provide time units matching those of Solidity. months is not included and I see you haven't used it anywhere either. What do you think about removing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it makes a total sense to mimic the solidity constants. Thanks for pointing that out!

years: function(val) { return val * this.days(365)}
};
6 changes: 2 additions & 4 deletions test/helpers/latestTime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import moment from 'moment'

// Returns a moment.js instance representing the time of the last mined block
// Returns the time of the last mined block in seconds
export default function latestTime() {
return moment.unix(web3.eth.getBlock('latest').timestamp)
return web3.eth.getBlock('latest').timestamp;
}
72 changes: 15 additions & 57 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
# yarn lockfile v1


abbrev@1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f"

abbrev@1.0.x:
abbrev@1, abbrev@1.0.x:
version "1.0.9"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135"

Expand Down Expand Up @@ -763,13 +759,13 @@ big.js@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.1.3.tgz#4cada2193652eb3ca9ec8e55c9015669c9806978"

"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
"bignumber.js@git+https://github.com/debris/bignumber.js#master":
version "2.0.7"
resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
resolved "git+https://github.com/debris/bignumber.js#c7a38de919ed75e6fb6ba38051986e294b328df9"

"bignumber.js@git+https://github.com/debris/bignumber.js.git#master":
"bignumber.js@git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2":
version "2.0.7"
resolved "git+https://github.com/debris/bignumber.js.git#c7a38de919ed75e6fb6ba38051986e294b328df9"
resolved "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"

binary-extensions@^1.0.0:
version "1.9.0"
Expand Down Expand Up @@ -1468,10 +1464,6 @@ esprima@2.7.x, esprima@^2.6.0, esprima@^2.7.1:
version "2.7.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"

esprima@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.0.tgz#4499eddcd1110e0b218bacf2fa7f7f59f55ca804"

esrecurse@^4.1.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/esrecurse/-/esrecurse-4.2.0.tgz#fa9568d98d3823f9a41d91e902dcab9ea6e5b163"
Expand Down Expand Up @@ -2248,20 +2240,13 @@ js-tokens@^3.0.0:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"

js-yaml@3.6.1:
js-yaml@3.6.1, js-yaml@3.x:
version "3.6.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30"
dependencies:
argparse "^1.0.7"
esprima "^2.6.0"

js-yaml@3.x:
version "3.9.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.9.1.tgz#08775cebdfdd359209f0d2acd383c8f86a6904a0"
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"

jsbn@~0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
Expand Down Expand Up @@ -2649,18 +2634,14 @@ minimatch@0.3:
dependencies:
brace-expansion "^1.1.7"

minimist@0.0.8:
minimist@0.0.8, minimist@~0.0.1:
version "0.0.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"

minimist@1.2.0, minimist@^1.2.0, minimist@~1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"

minimist@~0.0.1:
version "0.0.10"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"

mkdirp@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.3.0.tgz#1bbf5ab1ba827af23575143490426455f481fe1e"
Expand Down Expand Up @@ -2706,10 +2687,6 @@ mocha@^3.4.2:
mkdirp "0.5.1"
supports-color "3.1.2"

moment@^2.18.1:
version "2.18.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"

ms@0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"
Expand Down Expand Up @@ -3188,9 +3165,9 @@ read-pkg@^2.0.0:
normalize-package-data "^2.3.2"
path-type "^2.0.0"

readable-stream@^1.0.33:
version "1.1.14"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.1.14.tgz#7cf4c54ef648e3813084c636dd2079e166c081d9"
readable-stream@^1.0.33, readable-stream@~1.0.15:
version "1.0.34"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
Expand All @@ -3209,15 +3186,6 @@ readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.2, readable
string_decoder "~1.0.3"
util-deprecate "~1.0.1"

readable-stream@~1.0.15:
version "1.0.34"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "0.0.1"
string_decoder "~0.10.x"

readdirp@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-2.1.0.tgz#4ed0ad060df3073300c48440373f72d1cc642d78"
Expand Down Expand Up @@ -3304,7 +3272,7 @@ req-from@^1.0.1:
dependencies:
resolve-from "^2.0.0"

request@2.79.0:
request@2.79.0, request@^2.67.0:
version "2.79.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.79.0.tgz#4dfe5bf6be8b8cdc37fcf93e04b65577722710de"
dependencies:
Expand All @@ -3329,7 +3297,7 @@ request@2.79.0:
tunnel-agent "~0.4.1"
uuid "^3.0.0"

request@^2.67.0, request@^2.81.0:
request@^2.81.0:
version "2.81.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0"
dependencies:
Expand Down Expand Up @@ -3372,11 +3340,11 @@ resolve-from@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-2.0.0.tgz#9480ab20e94ffa1d9e80a804c7ea147611966b57"

resolve@1.1.x:
resolve@1.1.x, resolve@^1.1.6:
version "1.1.7"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"

resolve@^1.1.6, resolve@~1.4.0:
resolve@~1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.4.0.tgz#a75be01c53da25d934a98ebd0e4c4a7312f92a86"
dependencies:
Expand Down Expand Up @@ -3528,7 +3496,7 @@ sol-explore@^1.6.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/sol-explore/-/sol-explore-1.6.2.tgz#43ae8c419fd3ac056a05f8a9d1fb1022cd41ecc2"

solc@0.4.13:
solc@0.4.13, solc@^0.4.2:
version "0.4.13"
resolved "https://registry.yarnpkg.com/solc/-/solc-0.4.13.tgz#aa5cbdcce3e6ae3c190d20f5fdf8bc880702ec75"
dependencies:
Expand All @@ -3538,16 +3506,6 @@ solc@0.4.13:
semver "^5.3.0"
yargs "^4.7.1"

solc@^0.4.2:
version "0.4.15"
resolved "https://registry.yarnpkg.com/solc/-/solc-0.4.15.tgz#8ae8f1606a124a3f81c28b9dcecb0964ebdf9f25"
dependencies:
fs-extra "^0.30.0"
memorystream "^0.3.1"
require-from-string "^1.1.0"
semver "^5.3.0"
yargs "^4.7.1"

solidity-coverage@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/solidity-coverage/-/solidity-coverage-0.2.1.tgz#aa6fd8e7d49a6fcd1dc9913e9b61bc84f8ad046f"
Expand Down