Merged latestTime, increaseTime and duration into a time helper. (#1364)

pull/1381/head
Nicolás Venturo 6 years ago committed by GitHub
parent 6ae041bca6
commit 34bc709bc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      test/crowdsale/FinalizableCrowdsale.test.js
  2. 23
      test/crowdsale/IncreasingPriceCrowdsale.test.js
  3. 13
      test/crowdsale/PostDeliveryCrowdsale.test.js
  4. 15
      test/crowdsale/RefundableCrowdsale.test.js
  5. 19
      test/crowdsale/TimedCrowdsale.test.js
  6. 23
      test/examples/SampleCrowdsale.test.js
  7. 11
      test/helpers/latestTime.js
  8. 21
      test/helpers/time.js
  9. 15
      test/token/ERC20/TokenTimelock.test.js
  10. 24
      test/token/ERC20/TokenVesting.test.js

@ -1,7 +1,6 @@
const expectEvent = require('../helpers/expectEvent');
const { advanceBlock } = require('../helpers/advanceToBlock');
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
const { latestTime } = require('../helpers/latestTime');
const time = require('../helpers/time');
const { expectThrow } = require('../helpers/expectThrow');
const { EVMRevert } = require('../helpers/EVMRevert');
@ -23,9 +22,9 @@ contract('FinalizableCrowdsale', function ([_, wallet, anyone]) {
});
beforeEach(async function () {
this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1);
this.openingTime = (await time.latest()) + time.duration.weeks(1);
this.closingTime = this.openingTime + time.duration.weeks(1);
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
this.token = await ERC20.new();
this.crowdsale = await FinalizableCrowdsaleImpl.new(
@ -38,18 +37,18 @@ contract('FinalizableCrowdsale', function ([_, wallet, anyone]) {
});
it('can be finalized by anyone after ending', async function () {
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: anyone });
});
it('cannot be finalized twice', async function () {
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: anyone });
await expectThrow(this.crowdsale.finalize({ from: anyone }), EVMRevert);
});
it('logs finalized', async function () {
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
const { logs } = await this.crowdsale.finalize({ from: anyone });
expectEvent.inLogs(logs, 'CrowdsaleFinalized');
});

@ -1,7 +1,6 @@
const { ether } = require('../helpers/ether');
const { advanceBlock } = require('../helpers/advanceToBlock');
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
const { latestTime } = require('../helpers/latestTime');
const time = require('../helpers/time');
const { assertRevert } = require('../helpers/assertRevert');
const BigNumber = web3.BigNumber;
@ -29,9 +28,9 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser])
beforeEach(async function () {
await advanceBlock();
this.startTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.startTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1);
this.startTime = (await time.latest()) + time.duration.weeks(1);
this.closingTime = this.startTime + time.duration.weeks(1);
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
this.token = await SimpleToken.new();
});
@ -61,43 +60,43 @@ contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser])
});
it('at start', async function () {
await increaseTimeTo(this.startTime);
await time.increaseTo(this.startTime);
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(initialRate));
});
it('at time 150', async function () {
await increaseTimeTo(this.startTime + 150);
await time.increaseTo(this.startTime + 150);
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150));
});
it('at time 300', async function () {
await increaseTimeTo(this.startTime + 300);
await time.increaseTo(this.startTime + 300);
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime300));
});
it('at time 1500', async function () {
await increaseTimeTo(this.startTime + 1500);
await time.increaseTo(this.startTime + 1500);
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime1500));
});
it('at time 30', async function () {
await increaseTimeTo(this.startTime + 30);
await time.increaseTo(this.startTime + 30);
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime30));
});
it('at time 150000', async function () {
await increaseTimeTo(this.startTime + 150000);
await time.increaseTo(this.startTime + 150000);
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150000));
});
it('at time 450000', async function () {
await increaseTimeTo(this.startTime + 450000);
await time.increaseTo(this.startTime + 450000);
await this.crowdsale.buyTokens(investor, { value, from: purchaser });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime450000));
});

@ -1,6 +1,5 @@
const { advanceBlock } = require('../helpers/advanceToBlock');
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
const { latestTime } = require('../helpers/latestTime');
const time = require('../helpers/time');
const { expectThrow } = require('../helpers/expectThrow');
const { EVMRevert } = require('../helpers/EVMRevert');
const { ether } = require('../helpers/ether');
@ -24,9 +23,9 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
});
beforeEach(async function () {
this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1);
this.openingTime = (await time.latest()) + time.duration.weeks(1);
this.closingTime = this.openingTime + time.duration.weeks(1);
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
this.token = await SimpleToken.new();
this.crowdsale = await PostDeliveryCrowdsaleImpl.new(
this.openingTime, this.closingTime, rate, wallet, this.token.address
@ -36,7 +35,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
context('after opening time', function () {
beforeEach(async function () {
await increaseTimeTo(this.openingTime);
await time.increaseTo(this.openingTime);
});
context('with bought tokens', function () {
@ -57,7 +56,7 @@ contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
context('after closing time', function () {
beforeEach(async function () {
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
});
it('allows beneficiaries to withdraw tokens', async function () {

@ -1,7 +1,6 @@
const { ether } = require('../helpers/ether');
const { advanceBlock } = require('../helpers/advanceToBlock');
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
const { latestTime } = require('../helpers/latestTime');
const time = require('../helpers/time');
const { expectThrow } = require('../helpers/expectThrow');
const { EVMRevert } = require('../helpers/EVMRevert');
const { ethGetBalance } = require('../helpers/web3');
@ -27,9 +26,9 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
});
beforeEach(async function () {
this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1);
this.openingTime = (await time.latest()) + time.duration.weeks(1);
this.closingTime = this.openingTime + time.duration.weeks(1);
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
this.preWalletBalance = await ethGetBalance(wallet);
this.token = await SimpleToken.new();
@ -61,7 +60,7 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
context('after opening time', function () {
beforeEach(async function () {
await increaseTimeTo(this.openingTime);
await time.increaseTo(this.openingTime);
});
it('denies refunds', async function () {
@ -75,7 +74,7 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
context('after closing time and finalization', function () {
beforeEach(async function () {
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: anyone });
});
@ -95,7 +94,7 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon
context('after closing time and finalization', function () {
beforeEach(async function () {
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: anyone });
});

@ -1,7 +1,6 @@
const { ether } = require('../helpers/ether');
const { advanceBlock } = require('../helpers/advanceToBlock');
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
const { latestTime } = require('../helpers/latestTime');
const time = require('../helpers/time');
const { expectThrow } = require('../helpers/expectThrow');
const { EVMRevert } = require('../helpers/EVMRevert');
@ -25,21 +24,21 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
});
beforeEach(async function () {
this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1);
this.openingTime = (await time.latest()) + time.duration.weeks(1);
this.closingTime = this.openingTime + time.duration.weeks(1);
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
this.token = await SimpleToken.new();
});
it('rejects an opening time in the past', async function () {
await expectThrow(TimedCrowdsaleImpl.new(
(await latestTime()) - duration.days(1), this.closingTime, rate, wallet, this.token.address
(await time.latest()) - time.duration.days(1), this.closingTime, rate, wallet, this.token.address
), EVMRevert);
});
it('rejects a closing time before the opening time', async function () {
await expectThrow(TimedCrowdsaleImpl.new(
this.openingTime, this.openingTime - duration.seconds(1), rate, wallet, this.token.address
this.openingTime, this.openingTime - time.duration.seconds(1), rate, wallet, this.token.address
), EVMRevert);
});
@ -53,7 +52,7 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
it('should be ended only after end', async function () {
(await this.crowdsale.hasClosed()).should.equal(false);
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
(await this.crowdsale.isOpen()).should.equal(false);
(await this.crowdsale.hasClosed()).should.equal(true);
});
@ -66,14 +65,14 @@ contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
});
it('should accept payments after start', async function () {
await increaseTimeTo(this.openingTime);
await time.increaseTo(this.openingTime);
(await this.crowdsale.isOpen()).should.equal(true);
await this.crowdsale.send(value);
await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
});
it('should reject payments after end', async function () {
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
await expectThrow(this.crowdsale.send(value), EVMRevert);
await expectThrow(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }), EVMRevert);
});

@ -1,7 +1,6 @@
const { ether } = require('../helpers/ether');
const { advanceBlock } = require('../helpers/advanceToBlock');
const { increaseTimeTo, duration } = require('../helpers/increaseTime');
const { latestTime } = require('../helpers/latestTime');
const time = require('../helpers/time');
const { expectThrow } = require('../helpers/expectThrow');
const { EVMRevert } = require('../helpers/EVMRevert');
const { assertRevert } = require('../helpers/assertRevert');
@ -27,9 +26,9 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
});
beforeEach(async function () {
this.openingTime = (await latestTime()) + duration.weeks(1);
this.closingTime = this.openingTime + duration.weeks(1);
this.afterClosingTime = this.closingTime + duration.seconds(1);
this.openingTime = (await time.latest()) + time.duration.weeks(1);
this.closingTime = this.openingTime + time.duration.weeks(1);
this.afterClosingTime = this.closingTime + time.duration.seconds(1);
this.token = await SampleCrowdsaleToken.new({ from: deployer });
this.crowdsale = await SampleCrowdsale.new(
@ -68,7 +67,7 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
const investmentAmount = ether(1);
const expectedTokenAmount = RATE.mul(investmentAmount);
await increaseTimeTo(this.openingTime);
await time.increaseTo(this.openingTime);
await this.crowdsale.buyTokens(investor, { value: investmentAmount, from: investor });
(await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
@ -76,23 +75,23 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
});
it('should reject payments after end', async function () {
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
await expectThrow(this.crowdsale.send(ether(1)), EVMRevert);
await expectThrow(this.crowdsale.buyTokens(investor, { value: ether(1), from: investor }), EVMRevert);
});
it('should reject payments over cap', async function () {
await increaseTimeTo(this.openingTime);
await time.increaseTo(this.openingTime);
await this.crowdsale.send(CAP);
await expectThrow(this.crowdsale.send(1), EVMRevert);
});
it('should allow finalization and transfer funds to wallet if the goal is reached', async function () {
await increaseTimeTo(this.openingTime);
await time.increaseTo(this.openingTime);
await this.crowdsale.send(GOAL);
const beforeFinalization = await ethGetBalance(wallet);
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: owner });
const afterFinalization = await ethGetBalance(wallet);
@ -102,9 +101,9 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) {
it('should allow refunds if the goal is not reached', async function () {
const balanceBeforeInvestment = await ethGetBalance(investor);
await increaseTimeTo(this.openingTime);
await time.increaseTo(this.openingTime);
await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 });
await increaseTimeTo(this.afterClosingTime);
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: owner });
await this.crowdsale.claimRefund(investor, { gasPrice: 0 });

@ -1,11 +0,0 @@
const { ethGetBlock } = require('./web3');
// Returns the time of the last mined block in seconds
async function latestTime () {
const block = await ethGetBlock('latest');
return block.timestamp;
}
module.exports = {
latestTime,
};

@ -1,7 +1,13 @@
const { latestTime } = require('./latestTime');
const { ethGetBlock } = require('./web3');
// Returns the time of the last mined block in seconds
async function latest () {
const block = await ethGetBlock('latest');
return block.timestamp;
}
// Increases ganache time by the passed duration in seconds
function increaseTime (duration) {
function increase (duration) {
const id = Date.now();
return new Promise((resolve, reject) => {
@ -31,12 +37,12 @@ function increaseTime (duration) {
*
* @param target time in seconds
*/
async function increaseTimeTo (target) {
const now = (await latestTime());
async function increaseTo (target) {
const now = (await latest());
if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`);
const diff = target - now;
return increaseTime(diff);
return increase(diff);
}
const duration = {
@ -49,7 +55,8 @@ const duration = {
};
module.exports = {
increaseTime,
increaseTimeTo,
latest,
increase,
increaseTo,
duration,
};

@ -1,5 +1,4 @@
const { latestTime } = require('../../helpers/latestTime');
const { increaseTimeTo, duration } = require('../../helpers/increaseTime');
const time = require('../../helpers/time');
const { expectThrow } = require('../../helpers/expectThrow');
const BigNumber = web3.BigNumber;
@ -20,7 +19,7 @@ contract('TokenTimelock', function ([_, minter, beneficiary]) {
});
it('rejects a release time in the past', async function () {
const pastReleaseTime = (await latestTime()) - duration.years(1);
const pastReleaseTime = (await time.latest()) - time.duration.years(1);
await expectThrow(
TokenTimelock.new(this.token.address, beneficiary, pastReleaseTime)
);
@ -28,7 +27,7 @@ contract('TokenTimelock', function ([_, minter, beneficiary]) {
context('once deployed', function () {
beforeEach(async function () {
this.releaseTime = (await latestTime()) + duration.years(1);
this.releaseTime = (await time.latest()) + time.duration.years(1);
this.timelock = await TokenTimelock.new(this.token.address, beneficiary, this.releaseTime);
await this.token.mint(this.timelock.address, amount, { from: minter });
});
@ -44,24 +43,24 @@ contract('TokenTimelock', function ([_, minter, beneficiary]) {
});
it('cannot be released just before time limit', async function () {
await increaseTimeTo(this.releaseTime - duration.seconds(3));
await time.increaseTo(this.releaseTime - time.duration.seconds(3));
await expectThrow(this.timelock.release());
});
it('can be released just after limit', async function () {
await increaseTimeTo(this.releaseTime + duration.seconds(1));
await time.increaseTo(this.releaseTime + time.duration.seconds(1));
await this.timelock.release();
(await this.token.balanceOf(beneficiary)).should.be.bignumber.equal(amount);
});
it('can be released after time limit', async function () {
await increaseTimeTo(this.releaseTime + duration.years(1));
await time.increaseTo(this.releaseTime + time.duration.years(1));
await this.timelock.release();
(await this.token.balanceOf(beneficiary)).should.be.bignumber.equal(amount);
});
it('cannot be released twice', async function () {
await increaseTimeTo(this.releaseTime + duration.years(1));
await time.increaseTo(this.releaseTime + time.duration.years(1));
await this.timelock.release();
await expectThrow(this.timelock.release());
(await this.token.balanceOf(beneficiary)).should.be.bignumber.equal(amount);

@ -1,7 +1,6 @@
const { expectThrow } = require('../../helpers/expectThrow');
const { EVMRevert } = require('../../helpers/EVMRevert');
const { latestTime } = require('../../helpers/latestTime');
const { increaseTimeTo, duration } = require('../../helpers/increaseTime');
const time = require('../../helpers/time');
const { ethGetBlock } = require('../../helpers/web3');
const BigNumber = web3.BigNumber;
@ -18,9 +17,10 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
beforeEach(async function () {
this.start = (await latestTime()) + duration.minutes(1); // +1 minute so it starts after contract instantiation
this.cliffDuration = duration.years(1);
this.duration = duration.years(2);
// +1 minute so it starts after contract instantiation
this.start = (await time.latest()) + time.duration.minutes(1);
this.cliffDuration = time.duration.years(1);
this.duration = time.duration.years(2);
});
it('rejects a duration shorter than the cliff', async function () {
@ -65,12 +65,12 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('can be released after cliff', async function () {
await increaseTimeTo(this.start + this.cliffDuration + duration.weeks(1));
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(1));
await this.vesting.release(this.token.address);
});
it('should release proper amount after cliff', async function () {
await increaseTimeTo(this.start + this.cliffDuration);
await time.increaseTo(this.start + this.cliffDuration);
const { receipt } = await this.vesting.release(this.token.address);
const block = await ethGetBlock(receipt.blockNumber);
@ -87,7 +87,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
for (let i = 1; i <= checkpoints; i++) {
const now = this.start + this.cliffDuration + i * (vestingPeriod / checkpoints);
await increaseTimeTo(now);
await time.increaseTo(now);
await this.vesting.release(this.token.address);
const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
@ -97,7 +97,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('should have released all after end', async function () {
await increaseTimeTo(this.start + this.duration);
await time.increaseTo(this.start + this.duration);
await this.vesting.release(this.token.address);
(await this.token.balanceOf(beneficiary)).should.bignumber.equal(amount);
(await this.vesting.released(this.token.address)).should.bignumber.equal(amount);
@ -120,7 +120,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('should return the non-vested tokens when revoked by owner', async function () {
await increaseTimeTo(this.start + this.cliffDuration + duration.weeks(12));
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
const vested = await this.vesting.vestedAmount(this.token.address);
@ -130,7 +130,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('should keep the vested tokens when revoked by owner', async function () {
await increaseTimeTo(this.start + this.cliffDuration + duration.weeks(12));
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
const vestedPre = await this.vesting.vestedAmount(this.token.address);
@ -142,7 +142,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
});
it('should fail to be revoked a second time', async function () {
await increaseTimeTo(this.start + this.cliffDuration + duration.weeks(12));
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
await this.vesting.vestedAmount(this.token.address);

Loading…
Cancel
Save