Merge tag 'v2.1.1' of github.com:OpenZeppelin/openzeppelin-solidity
v2.1.1
This commit is contained in:
@ -1,9 +1,7 @@
|
||||
|
||||
const Counter = artifacts.require('CounterImpl');
|
||||
const CounterImpl = artifacts.require('CounterImpl');
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-bignumber')(web3.BigNumber))
|
||||
.should();
|
||||
require('../helpers/setup');
|
||||
|
||||
const EXPECTED = [1, 2, 3, 4];
|
||||
const KEY1 = web3.sha3('key1');
|
||||
@ -11,7 +9,7 @@ const KEY2 = web3.sha3('key2');
|
||||
|
||||
contract('Counter', function ([_, owner]) {
|
||||
beforeEach(async function () {
|
||||
this.mock = await Counter.new({ from: owner });
|
||||
this.mock = await CounterImpl.new({ from: owner });
|
||||
});
|
||||
|
||||
context('custom key', async function () {
|
||||
|
||||
@ -1,13 +1,12 @@
|
||||
const ERC20WithMetadata = artifacts.require('ERC20WithMetadataMock');
|
||||
const ERC20WithMetadataMock = artifacts.require('ERC20WithMetadataMock');
|
||||
|
||||
require('chai')
|
||||
.should();
|
||||
require('../../helpers/setup');
|
||||
|
||||
const metadataURI = 'https://example.com';
|
||||
|
||||
describe('ERC20WithMetadata', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC20WithMetadata.new(metadataURI);
|
||||
this.token = await ERC20WithMetadataMock.new(metadataURI);
|
||||
});
|
||||
|
||||
it('responds with the metadata', async function () {
|
||||
|
||||
@ -1,22 +1,17 @@
|
||||
const { assertRevert } = require('../helpers/assertRevert');
|
||||
const shouldFail = require('../helpers/shouldFail');
|
||||
const { ZERO_ADDRESS } = require('../helpers/constants');
|
||||
|
||||
const ERC20Mock = artifacts.require('ERC20Mock');
|
||||
const ERC20Mintable = artifacts.require('ERC20MintableMock');
|
||||
const ERC20Migrator = artifacts.require('ERC20MigratorMock');
|
||||
|
||||
const BigNumber = web3.eth.BigNumber;
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-bignumber')(BigNumber))
|
||||
.should();
|
||||
require('../helpers/setup');
|
||||
|
||||
contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
||||
|
||||
const totalSupply = 200;
|
||||
|
||||
it('reverts with a null legacy token address', async function () {
|
||||
await assertRevert(ERC20Migrator.new(ZERO_ADDRESS));
|
||||
await shouldFail.reverting(ERC20Migrator.new(ZERO_ADDRESS));
|
||||
});
|
||||
|
||||
describe('with tokens and migrator', function () {
|
||||
@ -32,11 +27,11 @@ contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
|
||||
describe('beginMigration', function () {
|
||||
it('reverts with a null new token address', async function () {
|
||||
await assertRevert(this.migrator.beginMigration(ZERO_ADDRESS));
|
||||
await shouldFail.reverting(this.migrator.beginMigration(ZERO_ADDRESS));
|
||||
});
|
||||
|
||||
it('reverts if not a minter of the token', async function () {
|
||||
await assertRevert(this.migrator.beginMigration(this.newToken.address));
|
||||
await shouldFail.reverting(this.migrator.beginMigration(this.newToken.address));
|
||||
});
|
||||
|
||||
it('succeeds if it is a minter of the token', async function () {
|
||||
@ -47,7 +42,7 @@ contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
it('reverts the second time it is called', async function () {
|
||||
await this.newToken.addMinter(this.migrator.address);
|
||||
await this.migrator.beginMigration(this.newToken.address);
|
||||
await assertRevert(this.migrator.beginMigration(this.newToken.address));
|
||||
await shouldFail.reverting(this.migrator.beginMigration(this.newToken.address));
|
||||
});
|
||||
});
|
||||
|
||||
@ -147,7 +142,7 @@ contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
|
||||
const amount = baseAmount + 1;
|
||||
|
||||
it('reverts', async function () {
|
||||
await assertRevert(this.migrator.migrate(owner, amount));
|
||||
await shouldFail.reverting(this.migrator.migrate(owner, amount));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,14 +1,10 @@
|
||||
const { assertRevert } = require('../helpers/assertRevert');
|
||||
const shouldFail = require('../helpers/shouldFail');
|
||||
const { getSignFor } = require('../helpers/sign');
|
||||
const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior');
|
||||
|
||||
const SignatureBouncerMock = artifacts.require('SignatureBouncerMock');
|
||||
|
||||
const BigNumber = web3.BigNumber;
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-bignumber')(BigNumber))
|
||||
.should();
|
||||
require('../helpers/setup');
|
||||
|
||||
const UINT_VALUE = 23;
|
||||
const BYTES_VALUE = web3.toHex('test');
|
||||
@ -36,19 +32,19 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
});
|
||||
|
||||
it('does not allow invalid signature for sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignature(INVALID_SIGNATURE, { from: authorizedUser })
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow valid signature for other sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: anyone })
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow valid signature for method for sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser, 'onlyWithValidSignature'),
|
||||
{ from: authorizedUser })
|
||||
);
|
||||
@ -63,13 +59,13 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
});
|
||||
|
||||
it('does not allow invalid signature with correct method for sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(INVALID_SIGNATURE, { from: authorizedUser })
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow valid signature with correct method for other sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(
|
||||
this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone }
|
||||
)
|
||||
@ -77,14 +73,14 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
});
|
||||
|
||||
it('does not allow valid method signature with incorrect method for sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser, 'theWrongMethod'),
|
||||
{ from: authorizedUser })
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow valid non-method signature method for sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser), { from: authorizedUser })
|
||||
);
|
||||
});
|
||||
@ -98,13 +94,13 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
});
|
||||
|
||||
it('does not allow invalid signature with correct method and data for sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE, INVALID_SIGNATURE, { from: authorizedUser })
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow valid signature with correct method and incorrect data for sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE + 10,
|
||||
this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
|
||||
{ from: authorizedUser }
|
||||
@ -113,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 assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE,
|
||||
this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
|
||||
{ from: anyone }
|
||||
@ -122,7 +118,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
});
|
||||
|
||||
it('does not allow valid non-method signature for sender', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE,
|
||||
this.signFor(authorizedUser), { from: authorizedUser }
|
||||
)
|
||||
@ -130,7 +126,7 @@ contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authoriz
|
||||
});
|
||||
|
||||
it('does not allow msg.data shorter than SIGNATURE_SIZE', async function () {
|
||||
await assertRevert(
|
||||
await shouldFail.reverting(
|
||||
this.sigBouncer.tooShortMsgData({ from: authorizedUser })
|
||||
);
|
||||
});
|
||||
|
||||
138
test/drafts/SignedSafeMath.test.js
Normal file
138
test/drafts/SignedSafeMath.test.js
Normal file
@ -0,0 +1,138 @@
|
||||
const shouldFail = require('../helpers/shouldFail');
|
||||
const { MIN_INT256, MAX_INT256 } = require('../helpers/constants');
|
||||
|
||||
const SignedSafeMathMock = artifacts.require('SignedSafeMathMock');
|
||||
|
||||
const { BigNumber } = require('../helpers/setup');
|
||||
|
||||
contract('SignedSafeMath', function () {
|
||||
beforeEach(async function () {
|
||||
this.safeMath = await SignedSafeMathMock.new();
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
(await this.safeMath.add(a, b)).should.be.bignumber.equal(a.plus(b));
|
||||
});
|
||||
|
||||
it('adds correctly if it does not overflow and the result is negative', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = MIN_INT256;
|
||||
|
||||
const result = await this.safeMath.add(a, b);
|
||||
result.should.be.bignumber.equal(a.plus(b));
|
||||
});
|
||||
|
||||
it('reverts on positive addition overflow', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BigNumber(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);
|
||||
|
||||
await shouldFail.reverting(this.safeMath.add(a, b));
|
||||
});
|
||||
});
|
||||
|
||||
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 result = await this.safeMath.sub(a, b);
|
||||
result.should.be.bignumber.equal(a.minus(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 result = await this.safeMath.sub(a, b);
|
||||
result.should.be.bignumber.equal(a.minus(b));
|
||||
});
|
||||
|
||||
it('reverts on positive subtraction overflow', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BigNumber(-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);
|
||||
|
||||
await shouldFail.reverting(this.safeMath.sub(a, b));
|
||||
});
|
||||
});
|
||||
|
||||
describe('mul', function () {
|
||||
it('multiplies correctly', async function () {
|
||||
const a = new BigNumber(5678);
|
||||
const b = new BigNumber(-1234);
|
||||
|
||||
const result = await this.safeMath.mul(a, b);
|
||||
result.should.be.bignumber.equal(a.times(b));
|
||||
});
|
||||
|
||||
it('handles a zero product correctly', async function () {
|
||||
const a = new BigNumber(0);
|
||||
const b = new BigNumber(5678);
|
||||
|
||||
const result = await this.safeMath.mul(a, b);
|
||||
result.should.be.bignumber.equal(a.times(b));
|
||||
});
|
||||
|
||||
it('reverts on multiplication overflow, positive operands', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BigNumber(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);
|
||||
|
||||
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 b = MIN_INT256;
|
||||
|
||||
await shouldFail.reverting(this.safeMath.mul(a, b));
|
||||
});
|
||||
});
|
||||
|
||||
describe('div', function () {
|
||||
it('divides correctly', async function () {
|
||||
const a = new BigNumber(-5678);
|
||||
const b = new BigNumber(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);
|
||||
|
||||
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);
|
||||
|
||||
await shouldFail.reverting(this.safeMath.div(a, b));
|
||||
});
|
||||
});
|
||||
});
|
||||
164
test/drafts/TokenVesting.test.js
Normal file
164
test/drafts/TokenVesting.test.js
Normal file
@ -0,0 +1,164 @@
|
||||
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 ERC20Mintable = artifacts.require('ERC20MintableMock');
|
||||
const TokenVesting = artifacts.require('TokenVestingMock');
|
||||
|
||||
contract('TokenVesting', function ([_, owner, beneficiary]) {
|
||||
const amount = new BigNumber(1000);
|
||||
|
||||
beforeEach(async function () {
|
||||
// +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('reverts with a duration shorter than the cliff', async function () {
|
||||
const cliffDuration = this.duration;
|
||||
const duration = this.cliffDuration;
|
||||
|
||||
cliffDuration.should.be.gt(duration);
|
||||
|
||||
await shouldFail.reverting(
|
||||
TokenVesting.new(beneficiary, this.start, cliffDuration, duration, true, { from: owner })
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts with a null beneficiary', async function () {
|
||||
await shouldFail.reverting(
|
||||
TokenVesting.new(ZERO_ADDRESS, this.start, this.cliffDuration, this.duration, true, { from: owner })
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts with a null duration', async function () {
|
||||
// cliffDuration should also be 0, since the duration must be larger than the cliff
|
||||
await shouldFail.reverting(
|
||||
TokenVesting.new(beneficiary, this.start, 0, 0, true, { from: owner })
|
||||
);
|
||||
});
|
||||
|
||||
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);
|
||||
await shouldFail.reverting(
|
||||
TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner })
|
||||
);
|
||||
});
|
||||
|
||||
context('once deployed', function () {
|
||||
beforeEach(async function () {
|
||||
this.vesting = await TokenVesting.new(
|
||||
beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner });
|
||||
|
||||
this.token = await ERC20Mintable.new({ from: owner });
|
||||
await this.token.mint(this.vesting.address, amount, { from: owner });
|
||||
});
|
||||
|
||||
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.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);
|
||||
});
|
||||
|
||||
it('cannot be released before cliff', async function () {
|
||||
await shouldFail.reverting(this.vesting.release(this.token.address));
|
||||
});
|
||||
|
||||
it('can be released after cliff', async function () {
|
||||
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(1));
|
||||
const { logs } = await this.vesting.release(this.token.address);
|
||||
expectEvent.inLogs(logs, 'TokensReleased', {
|
||||
token: this.token.address,
|
||||
amount: await this.token.balanceOf(beneficiary),
|
||||
});
|
||||
});
|
||||
|
||||
it('should release proper amount after cliff', async function () {
|
||||
await time.increaseTo(this.start + this.cliffDuration);
|
||||
|
||||
const { receipt } = await this.vesting.release(this.token.address);
|
||||
const block = await ethGetBlock(receipt.blockNumber);
|
||||
const releaseTime = block.timestamp;
|
||||
|
||||
const releasedAmount = amount.mul(releaseTime - this.start).div(this.duration).floor();
|
||||
(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 checkpoints = 4;
|
||||
|
||||
for (let i = 1; i <= checkpoints; i++) {
|
||||
const now = this.start + this.cliffDuration + i * (vestingPeriod / checkpoints);
|
||||
await time.increaseTo(now);
|
||||
|
||||
await this.vesting.release(this.token.address);
|
||||
const expectedVesting = amount.mul(now - this.start).div(this.duration).floor();
|
||||
(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 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);
|
||||
});
|
||||
|
||||
it('should be revoked by owner if revocable is set', async function () {
|
||||
const { logs } = await this.vesting.revoke(this.token.address, { from: owner });
|
||||
expectEvent.inLogs(logs, 'TokenVestingRevoked', { token: this.token.address });
|
||||
(await this.vesting.revoked(this.token.address)).should.equal(true);
|
||||
});
|
||||
|
||||
it('should fail to be revoked by owner if revocable not set', async function () {
|
||||
const vesting = await TokenVesting.new(
|
||||
beneficiary, this.start, this.cliffDuration, this.duration, false, { from: owner }
|
||||
);
|
||||
|
||||
await shouldFail.reverting(vesting.revoke(this.token.address, { from: owner }));
|
||||
});
|
||||
|
||||
it('should return the non-vested tokens when revoked by owner', async function () {
|
||||
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
|
||||
|
||||
const vested = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
|
||||
|
||||
await this.vesting.revoke(this.token.address, { from: owner });
|
||||
|
||||
(await this.token.balanceOf(owner)).should.bignumber.equal(amount.sub(vested));
|
||||
});
|
||||
|
||||
it('should keep the vested tokens when revoked by owner', async function () {
|
||||
await time.increaseTo(this.start + this.cliffDuration + time.duration.weeks(12));
|
||||
|
||||
const vestedPre = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
|
||||
|
||||
await this.vesting.revoke(this.token.address, { from: owner });
|
||||
|
||||
const vestedPost = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
|
||||
|
||||
vestedPre.should.bignumber.equal(vestedPost);
|
||||
});
|
||||
|
||||
it('should fail to be revoked a second time', async function () {
|
||||
await this.vesting.revoke(this.token.address, { from: owner });
|
||||
await shouldFail.reverting(this.vesting.revoke(this.token.address, { from: owner }));
|
||||
});
|
||||
|
||||
function vestedAmount (total, now, start, cliffDuration, duration) {
|
||||
return (now < start + cliffDuration) ? 0 : Math.round(total * (now - start) / duration);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user