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,11 +1,10 @@
|
||||
const { BN } = require('openzeppelin-test-helpers');
|
||||
|
||||
const CounterImpl = artifacts.require('CounterImpl');
|
||||
|
||||
require('../helpers/setup');
|
||||
|
||||
const EXPECTED = [1, 2, 3, 4];
|
||||
const KEY1 = web3.sha3('key1');
|
||||
const KEY2 = web3.sha3('key2');
|
||||
const EXPECTED = [new BN(1), new BN(2), new BN(3), new BN(4)];
|
||||
const KEY1 = web3.utils.sha3('key1');
|
||||
const KEY2 = web3.utils.sha3('key2');
|
||||
|
||||
contract('Counter', function ([_, owner]) {
|
||||
beforeEach(async function () {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
const ERC20WithMetadataMock = artifacts.require('ERC20WithMetadataMock');
|
||||
require('openzeppelin-test-helpers');
|
||||
|
||||
require('../../helpers/setup');
|
||||
const ERC20WithMetadataMock = artifacts.require('ERC20WithMetadataMock');
|
||||
|
||||
const metadataURI = 'https://example.com';
|
||||
|
||||
|
||||
@ -1,14 +1,12 @@
|
||||
const shouldFail = require('../helpers/shouldFail');
|
||||
const { ZERO_ADDRESS } = require('../helpers/constants');
|
||||
const { BN, constants, shouldFail } = require('openzeppelin-test-helpers');
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
|
||||
const ERC20Mock = artifacts.require('ERC20Mock');
|
||||
const ERC20Mintable = artifacts.require('ERC20Mintable');
|
||||
const ERC20Migrator = artifacts.require('ERC20Migrator');
|
||||
|
||||
require('../helpers/setup');
|
||||
|
||||
contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
const totalSupply = 200;
|
||||
const totalSupply = new BN('200');
|
||||
|
||||
it('reverts with a null legacy token address', async function () {
|
||||
await shouldFail.reverting(ERC20Migrator.new(ZERO_ADDRESS));
|
||||
@ -81,7 +79,7 @@ contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
currentBurnedBalance.should.be.bignumber.equal(amount);
|
||||
|
||||
const currentLegacyTokenBalance = await this.legacyToken.balanceOf(owner);
|
||||
currentLegacyTokenBalance.should.be.bignumber.equal(0);
|
||||
currentLegacyTokenBalance.should.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('updates the total supply', async function () {
|
||||
@ -91,7 +89,7 @@ contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
});
|
||||
|
||||
describe('when the approved balance is lower than the owned balance', function () {
|
||||
const amount = baseAmount - 1;
|
||||
const amount = baseAmount.subn(1);
|
||||
|
||||
beforeEach('approving part of the balance to the new contract', async function () {
|
||||
await this.legacyToken.approve(this.migrator.address, amount, { from: owner });
|
||||
@ -106,7 +104,7 @@ contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
});
|
||||
|
||||
describe('migrate', function () {
|
||||
const baseAmount = 50;
|
||||
const baseAmount = new BN(50);
|
||||
|
||||
beforeEach('approving tokens to the new contract', async function () {
|
||||
await this.legacyToken.approve(this.migrator.address, baseAmount, { from: owner });
|
||||
@ -129,7 +127,7 @@ contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
currentBurnedBalance.should.be.bignumber.equal(amount);
|
||||
|
||||
const currentLegacyTokenBalance = await this.legacyToken.balanceOf(owner);
|
||||
currentLegacyTokenBalance.should.be.bignumber.equal(totalSupply - amount);
|
||||
currentLegacyTokenBalance.should.be.bignumber.equal(totalSupply.sub(amount));
|
||||
});
|
||||
|
||||
it('updates the total supply', async function () {
|
||||
@ -139,7 +137,7 @@ contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
});
|
||||
|
||||
describe('when the given amount is higher than the one approved', function () {
|
||||
const amount = baseAmount + 1;
|
||||
const amount = baseAmount.addn(1);
|
||||
|
||||
it('reverts', async function () {
|
||||
await shouldFail.reverting(this.migrator.migrate(owner, amount));
|
||||
|
||||
@ -1,13 +1,11 @@
|
||||
const shouldFail = require('../helpers/shouldFail');
|
||||
const { shouldFail } = require('openzeppelin-test-helpers');
|
||||
const { getSignFor } = require('../helpers/sign');
|
||||
const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior');
|
||||
|
||||
const SignatureBouncerMock = artifacts.require('SignatureBouncerMock');
|
||||
|
||||
require('../helpers/setup');
|
||||
|
||||
const UINT_VALUE = 23;
|
||||
const BYTES_VALUE = web3.toHex('test');
|
||||
const BYTES_VALUE = web3.utils.toHex('test');
|
||||
const INVALID_SIGNATURE = '0xabcd';
|
||||
|
||||
contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authorizedUser, ...otherAccounts]) {
|
||||
@ -28,7 +26,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
describe('modifiers', function () {
|
||||
context('plain signature', function () {
|
||||
it('allows valid signature for sender', async function () {
|
||||
await this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: authorizedUser });
|
||||
await this.sigBouncer.onlyWithValidSignature(await this.signFor(authorizedUser), { from: authorizedUser });
|
||||
});
|
||||
|
||||
it('does not allow invalid signature for sender', async function () {
|
||||
@ -39,13 +37,13 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
|
||||
it('does not allow valid signature for other sender', async function () {
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: anyone })
|
||||
this.sigBouncer.onlyWithValidSignature(await this.signFor(authorizedUser), { from: anyone })
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow valid signature for method for sender', async function () {
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser, 'onlyWithValidSignature'),
|
||||
this.sigBouncer.onlyWithValidSignature(await this.signFor(authorizedUser, 'onlyWithValidSignature'),
|
||||
{ from: authorizedUser })
|
||||
);
|
||||
});
|
||||
@ -54,7 +52,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
context('method signature', function () {
|
||||
it('allows valid signature with correct method for sender', async function () {
|
||||
await this.sigBouncer.onlyWithValidSignatureAndMethod(
|
||||
this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: authorizedUser }
|
||||
await this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: authorizedUser }
|
||||
);
|
||||
});
|
||||
|
||||
@ -67,21 +65,21 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
it('does not allow valid signature with correct method for other sender', async function () {
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(
|
||||
this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone }
|
||||
await this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone }
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow valid method signature with incorrect method for sender', async function () {
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser, 'theWrongMethod'),
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(await this.signFor(authorizedUser, 'theWrongMethod'),
|
||||
{ from: authorizedUser })
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow valid non-method signature method for sender', async function () {
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser), { from: authorizedUser })
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(await this.signFor(authorizedUser), { from: authorizedUser })
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -89,7 +87,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
context('method and data signature', function () {
|
||||
it('allows valid signature with correct method and data for sender', async function () {
|
||||
await this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE,
|
||||
this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), { from: authorizedUser }
|
||||
await this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), { from: authorizedUser }
|
||||
);
|
||||
});
|
||||
|
||||
@ -102,7 +100,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
it('does not allow valid signature with correct method and incorrect data for sender', async function () {
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE + 10,
|
||||
this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
|
||||
await this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
|
||||
{ from: authorizedUser }
|
||||
)
|
||||
);
|
||||
@ -111,7 +109,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
it('does not allow valid signature with correct method and data for other sender', async function () {
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE,
|
||||
this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
|
||||
await this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
|
||||
{ from: anyone }
|
||||
)
|
||||
);
|
||||
@ -120,7 +118,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
it('does not allow valid non-method signature for sender', async function () {
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE,
|
||||
this.signFor(authorizedUser), { from: authorizedUser }
|
||||
await this.signFor(authorizedUser), { from: authorizedUser }
|
||||
)
|
||||
);
|
||||
});
|
||||
@ -136,7 +134,8 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
context('signature validation', function () {
|
||||
context('plain signature', function () {
|
||||
it('validates valid signature for valid user', async function () {
|
||||
(await this.sigBouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.equal(true);
|
||||
(await this.sigBouncer.checkValidSignature(authorizedUser, await this.signFor(authorizedUser)))
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it('does not validate invalid signature for valid user', async function () {
|
||||
@ -144,11 +143,12 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
});
|
||||
|
||||
it('does not validate valid signature for anyone', async function () {
|
||||
(await this.sigBouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.equal(false);
|
||||
(await this.sigBouncer.checkValidSignature(anyone, await this.signFor(authorizedUser))).should.equal(false);
|
||||
});
|
||||
|
||||
it('does not validate valid signature for method for valid user', async function () {
|
||||
(await this.sigBouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature'))
|
||||
(await this.sigBouncer.checkValidSignature(
|
||||
authorizedUser, await this.signFor(authorizedUser, 'checkValidSignature'))
|
||||
).should.equal(false);
|
||||
});
|
||||
});
|
||||
@ -156,7 +156,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
context('method signature', function () {
|
||||
it('validates valid signature with correct method for valid user', async function () {
|
||||
(await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser,
|
||||
this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
|
||||
await this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
|
||||
).should.equal(true);
|
||||
});
|
||||
|
||||
@ -166,12 +166,12 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
|
||||
it('does not validate valid signature with correct method for anyone', async function () {
|
||||
(await this.sigBouncer.checkValidSignatureAndMethod(anyone,
|
||||
this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
|
||||
await this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
|
||||
).should.equal(false);
|
||||
});
|
||||
|
||||
it('does not validate valid non-method signature with correct method for valid user', async function () {
|
||||
(await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser))
|
||||
(await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, await this.signFor(authorizedUser))
|
||||
).should.equal(false);
|
||||
});
|
||||
});
|
||||
@ -179,7 +179,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
context('method and data signature', function () {
|
||||
it('validates valid signature with correct method and data for valid user', async function () {
|
||||
(await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
|
||||
this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
|
||||
await this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
|
||||
).should.equal(true);
|
||||
});
|
||||
|
||||
@ -191,21 +191,21 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
it('does not validate valid signature with correct method and incorrect data for valid user',
|
||||
async function () {
|
||||
(await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10,
|
||||
this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
|
||||
await this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
|
||||
).should.equal(false);
|
||||
}
|
||||
);
|
||||
|
||||
it('does not validate valid signature with correct method and data for anyone', async function () {
|
||||
(await this.sigBouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE,
|
||||
this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
|
||||
await this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
|
||||
).should.equal(false);
|
||||
});
|
||||
|
||||
it('does not validate valid non-method-data signature with correct method and data for valid user',
|
||||
async function () {
|
||||
(await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
|
||||
this.signFor(authorizedUser, 'checkValidSignatureAndData'))
|
||||
await this.signFor(authorizedUser, 'checkValidSignatureAndData'))
|
||||
).should.equal(false);
|
||||
}
|
||||
);
|
||||
|
||||
@ -1,10 +1,8 @@
|
||||
const shouldFail = require('../helpers/shouldFail');
|
||||
const { MIN_INT256, MAX_INT256 } = require('../helpers/constants');
|
||||
const { BN, constants, shouldFail } = require('openzeppelin-test-helpers');
|
||||
const { MAX_INT256, MIN_INT256 } = constants;
|
||||
|
||||
const SignedSafeMathMock = artifacts.require('SignedSafeMathMock');
|
||||
|
||||
const { BigNumber } = require('../helpers/setup');
|
||||
|
||||
contract('SignedSafeMath', function () {
|
||||
beforeEach(async function () {
|
||||
this.safeMath = await SignedSafeMathMock.new();
|
||||
@ -12,10 +10,10 @@ contract('SignedSafeMath', function () {
|
||||
|
||||
describe('add', function () {
|
||||
it('adds correctly if it does not overflow and the result is positve', async function () {
|
||||
const a = new BigNumber(1234);
|
||||
const b = new BigNumber(5678);
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
(await this.safeMath.add(a, b)).should.be.bignumber.equal(a.plus(b));
|
||||
(await this.safeMath.add(a, b)).should.be.bignumber.equal(a.add(b));
|
||||
});
|
||||
|
||||
it('adds correctly if it does not overflow and the result is negative', async function () {
|
||||
@ -23,19 +21,19 @@ contract('SignedSafeMath', function () {
|
||||
const b = MIN_INT256;
|
||||
|
||||
const result = await this.safeMath.add(a, b);
|
||||
result.should.be.bignumber.equal(a.plus(b));
|
||||
result.should.be.bignumber.equal(a.add(b));
|
||||
});
|
||||
|
||||
it('reverts on positive addition overflow', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BigNumber(1);
|
||||
const b = new BN('1');
|
||||
|
||||
await shouldFail.reverting(this.safeMath.add(a, b));
|
||||
});
|
||||
|
||||
it('reverts on negative addition overflow', async function () {
|
||||
const a = MIN_INT256;
|
||||
const b = new BigNumber(-1);
|
||||
const b = new BN('-1');
|
||||
|
||||
await shouldFail.reverting(this.safeMath.add(a, b));
|
||||
});
|
||||
@ -43,31 +41,31 @@ contract('SignedSafeMath', function () {
|
||||
|
||||
describe('sub', function () {
|
||||
it('subtracts correctly if it does not overflow and the result is positive', async function () {
|
||||
const a = new BigNumber(5678);
|
||||
const b = new BigNumber(1234);
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
const result = await this.safeMath.sub(a, b);
|
||||
result.should.be.bignumber.equal(a.minus(b));
|
||||
result.should.be.bignumber.equal(a.sub(b));
|
||||
});
|
||||
|
||||
it('subtracts correctly if it does not overflow and the result is negative', async function () {
|
||||
const a = new BigNumber(1234);
|
||||
const b = new BigNumber(5678);
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
const result = await this.safeMath.sub(a, b);
|
||||
result.should.be.bignumber.equal(a.minus(b));
|
||||
result.should.be.bignumber.equal(a.sub(b));
|
||||
});
|
||||
|
||||
it('reverts on positive subtraction overflow', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BigNumber(-1);
|
||||
const b = new BN('-1');
|
||||
|
||||
await shouldFail.reverting(this.safeMath.sub(a, b));
|
||||
});
|
||||
|
||||
it('reverts on negative subtraction overflow', async function () {
|
||||
const a = MIN_INT256;
|
||||
const b = new BigNumber(1);
|
||||
const b = new BN('1');
|
||||
|
||||
await shouldFail.reverting(this.safeMath.sub(a, b));
|
||||
});
|
||||
@ -75,37 +73,37 @@ contract('SignedSafeMath', function () {
|
||||
|
||||
describe('mul', function () {
|
||||
it('multiplies correctly', async function () {
|
||||
const a = new BigNumber(5678);
|
||||
const b = new BigNumber(-1234);
|
||||
const a = new BN('5678');
|
||||
const b = new BN('-1234');
|
||||
|
||||
const result = await this.safeMath.mul(a, b);
|
||||
result.should.be.bignumber.equal(a.times(b));
|
||||
result.should.be.bignumber.equal(a.mul(b));
|
||||
});
|
||||
|
||||
it('handles a zero product correctly', async function () {
|
||||
const a = new BigNumber(0);
|
||||
const b = new BigNumber(5678);
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
const result = await this.safeMath.mul(a, b);
|
||||
result.should.be.bignumber.equal(a.times(b));
|
||||
result.should.be.bignumber.equal(a.mul(b));
|
||||
});
|
||||
|
||||
it('reverts on multiplication overflow, positive operands', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BigNumber(2);
|
||||
const b = new BN('2');
|
||||
|
||||
await shouldFail.reverting(this.safeMath.mul(a, b));
|
||||
});
|
||||
|
||||
it('reverts when minimum integer is multiplied by -1', async function () {
|
||||
const a = MIN_INT256;
|
||||
const b = new BigNumber(-1);
|
||||
const b = new BN('-1');
|
||||
|
||||
await shouldFail.reverting(this.safeMath.mul(a, b));
|
||||
});
|
||||
|
||||
it('reverts when -1 is multiplied by minimum integer', async function () {
|
||||
const a = new BigNumber(-1);
|
||||
const a = new BN('-1');
|
||||
const b = MIN_INT256;
|
||||
|
||||
await shouldFail.reverting(this.safeMath.mul(a, b));
|
||||
@ -114,23 +112,23 @@ contract('SignedSafeMath', function () {
|
||||
|
||||
describe('div', function () {
|
||||
it('divides correctly', async function () {
|
||||
const a = new BigNumber(-5678);
|
||||
const b = new BigNumber(5678);
|
||||
const a = new BN('-5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
const result = await this.safeMath.div(a, b);
|
||||
result.should.be.bignumber.equal(a.div(b));
|
||||
});
|
||||
|
||||
it('reverts on zero division', async function () {
|
||||
const a = new BigNumber(-5678);
|
||||
const b = new BigNumber(0);
|
||||
const a = new BN('-5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await shouldFail.reverting(this.safeMath.div(a, b));
|
||||
});
|
||||
|
||||
it('reverts on overflow, negative second', async function () {
|
||||
const a = new BigNumber(MIN_INT256);
|
||||
const b = new BigNumber(-1);
|
||||
const a = new BN(MIN_INT256);
|
||||
const b = new BN('-1');
|
||||
|
||||
await shouldFail.reverting(this.safeMath.div(a, b));
|
||||
});
|
||||
|
||||
@ -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