Migration to truffle 5 (and web3 1.0 (and BN)) (#1601)
* Now compiling using truffle 5.
* Migrated some test files, missing BN scientific notation usage.
* Now using BN time values.
* Migrate ERC20 tests.
* Migrate all ERC20 tests.
* Migrate utils, payment and ownership tests.
* All tests save ERC721 migrated.
* Migrated ERC721 tests.
* Fix lint errors.
* Delete old test helpers.
* Fix remaining crowdsale tests.
* Fix signature bouncer tests.
* Update how constants is used.
* Compile script pre-removes the build dir.
* Fix SafeMath tests.
* Revert "Compile script pre-removes the build dir."
This reverts commit 247e745113.
* Fix linter errors.
* Upgrade openzeppelin-test-helpers dependency.
* Update openzeppelin-test-helpers dependency.
* Define math constants globally.
* Remove unnecessary ether unit.
* Roll back reduced ether amounts in tests.
* Remove unnecessary toNumber conversions.
* Delete compile script.
* Fixed failing test.
This commit is contained in:
@ -1,20 +1,15 @@
|
||||
const shouldFail = require('../helpers/shouldFail');
|
||||
const expectEvent = require('../helpers/expectEvent');
|
||||
const time = require('../helpers/time');
|
||||
const { ethGetBlock } = require('../helpers/web3');
|
||||
const { ZERO_ADDRESS } = require('../helpers/constants');
|
||||
|
||||
const { BigNumber } = require('../helpers/setup');
|
||||
const { BN, constants, expectEvent, shouldFail, time } = require('openzeppelin-test-helpers');
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
|
||||
const ERC20Mintable = artifacts.require('ERC20Mintable');
|
||||
const TokenVesting = artifacts.require('TokenVesting');
|
||||
|
||||
contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
const amount = new BigNumber(1000);
|
||||
const amount = new BN('1000');
|
||||
|
||||
beforeEach(async function () {
|
||||
// +1 minute so it starts after contract instantiation
|
||||
this.start = (await time.latest()) + time.duration.minutes(1);
|
||||
this.start = (await time.latest()).add(time.duration.minutes(1));
|
||||
this.cliffDuration = time.duration.years(1);
|
||||
this.duration = time.duration.years(2);
|
||||
});
|
||||
@ -23,7 +18,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
const cliffDuration = this.duration;
|
||||
const duration = this.cliffDuration;
|
||||
|
||||
cliffDuration.should.be.gt(duration);
|
||||
cliffDuration.should.be.bignumber.that.is.at.least(duration);
|
||||
|
||||
await shouldFail.reverting(
|
||||
TokenVesting.new(beneficiary, this.start, cliffDuration, duration, true, { from: owner })
|
||||
@ -46,7 +41,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
it('reverts if the end time is in the past', async function () {
|
||||
const now = await time.latest();
|
||||
|
||||
this.start = now - this.duration - time.duration.minutes(1);
|
||||
this.start = now.sub(this.duration).sub(time.duration.minutes(1));
|
||||
await shouldFail.reverting(
|
||||
TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner })
|
||||
);
|
||||
@ -63,7 +58,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
|
||||
it('can get state', async function () {
|
||||
(await this.vesting.beneficiary()).should.be.equal(beneficiary);
|
||||
(await this.vesting.cliff()).should.be.bignumber.equal(this.start + this.cliffDuration);
|
||||
(await this.vesting.cliff()).should.be.bignumber.equal(this.start.add(this.cliffDuration));
|
||||
(await this.vesting.start()).should.be.bignumber.equal(this.start);
|
||||
(await this.vesting.duration()).should.be.bignumber.equal(this.duration);
|
||||
(await this.vesting.revocable()).should.be.equal(true);
|
||||
@ -74,7 +69,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
});
|
||||
|
||||
it('can be released after cliff', async function () {
|
||||
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(1));
|
||||
await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(1)));
|
||||
const { logs } = await this.vesting.release(this.token.address);
|
||||
expectEvent.inLogs(logs, 'TokensReleased', {
|
||||
token: this.token.address,
|
||||
@ -83,34 +78,33 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
});
|
||||
|
||||
it('should release proper amount after cliff', async function () {
|
||||
await time.increaseTo(this.start + this.cliffDuration);
|
||||
await time.increaseTo(this.start.add(this.cliffDuration));
|
||||
|
||||
const { receipt } = await this.vesting.release(this.token.address);
|
||||
const block = await ethGetBlock(receipt.blockNumber);
|
||||
const releaseTime = block.timestamp;
|
||||
await this.vesting.release(this.token.address);
|
||||
const releaseTime = await time.latest();
|
||||
|
||||
const releasedAmount = amount.mul(releaseTime - this.start).div(this.duration).floor();
|
||||
const releasedAmount = amount.mul(releaseTime.sub(this.start)).div(this.duration);
|
||||
(await this.token.balanceOf(beneficiary)).should.bignumber.equal(releasedAmount);
|
||||
(await this.vesting.released(this.token.address)).should.bignumber.equal(releasedAmount);
|
||||
});
|
||||
|
||||
it('should linearly release tokens during vesting period', async function () {
|
||||
const vestingPeriod = this.duration - this.cliffDuration;
|
||||
const vestingPeriod = this.duration.sub(this.cliffDuration);
|
||||
const checkpoints = 4;
|
||||
|
||||
for (let i = 1; i <= checkpoints; i++) {
|
||||
const now = this.start + this.cliffDuration + i * (vestingPeriod / checkpoints);
|
||||
const now = this.start.add(this.cliffDuration).add((vestingPeriod.muln(i).divn(checkpoints)));
|
||||
await time.increaseTo(now);
|
||||
|
||||
await this.vesting.release(this.token.address);
|
||||
const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
|
||||
const expectedVesting = amount.mul(now.sub(this.start)).div(this.duration);
|
||||
(await this.token.balanceOf(beneficiary)).should.bignumber.equal(expectedVesting);
|
||||
(await this.vesting.released(this.token.address)).should.bignumber.equal(expectedVesting);
|
||||
}
|
||||
});
|
||||
|
||||
it('should have released all after end', async function () {
|
||||
await time.increaseTo(this.start + this.duration);
|
||||
await time.increaseTo(this.start.add(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);
|
||||
@ -131,7 +125,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
});
|
||||
|
||||
it('should return the non-vested tokens when revoked by owner', async function () {
|
||||
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
|
||||
await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(12)));
|
||||
|
||||
const vested = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
|
||||
|
||||
@ -141,7 +135,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
});
|
||||
|
||||
it('should keep the vested tokens when revoked by owner', async function () {
|
||||
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
|
||||
await time.increaseTo(this.start.add(this.cliffDuration).add(time.duration.weeks(12)));
|
||||
|
||||
const vestedPre = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
|
||||
|
||||
@ -158,7 +152,7 @@ contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
});
|
||||
|
||||
function vestedAmount (total, now, start, cliffDuration, duration) {
|
||||
return (now < start + cliffDuration) ? 0 : Math.round(total * (now - start) / duration);
|
||||
return (now.lt(start.add(cliffDuration))) ? new BN(0) : total.mul((now.sub(start))).div(duration);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user