Draft and lifecycles directories cleanup (#2122)

* Move Pausable into utils

* Move Strings into utils

* Move Counters into utils

* Move SignedSafeMath into math

* Remove ERC1046

* Make ERC20Snapshot.snapshot internal

* Move ERC20Snapshot into ERC20

* Add drafts deprecation notice

* Remove drafts directory

* Add changelog entry

* Apply suggestions from code review

Co-Authored-By: Francisco Giordano <frangio.1@gmail.com>

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
Nicolás Venturo
2020-03-16 16:27:15 -03:00
committed by GitHub
parent 8176a901a9
commit c9630526e2
30 changed files with 79 additions and 299 deletions

View File

@ -1,61 +0,0 @@
const { contract } = require('@openzeppelin/test-environment');
const { expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const CountersImpl = contract.fromArtifact('CountersImpl');
describe('Counters', function () {
beforeEach(async function () {
this.counter = await CountersImpl.new();
});
it('starts at zero', async function () {
expect(await this.counter.current()).to.be.bignumber.equal('0');
});
describe('increment', function () {
it('increments the current value by one', async function () {
await this.counter.increment();
expect(await this.counter.current()).to.be.bignumber.equal('1');
});
it('can be called multiple times', async function () {
await this.counter.increment();
await this.counter.increment();
await this.counter.increment();
expect(await this.counter.current()).to.be.bignumber.equal('3');
});
});
describe('decrement', function () {
beforeEach(async function () {
await this.counter.increment();
expect(await this.counter.current()).to.be.bignumber.equal('1');
});
it('decrements the current value by one', async function () {
await this.counter.decrement();
expect(await this.counter.current()).to.be.bignumber.equal('0');
});
it('reverts if the current value is 0', async function () {
await this.counter.decrement();
await expectRevert(this.counter.decrement(), 'SafeMath: subtraction overflow');
});
it('can be called multiple times', async function () {
await this.counter.increment();
await this.counter.increment();
expect(await this.counter.current()).to.be.bignumber.equal('3');
await this.counter.decrement();
await this.counter.decrement();
await this.counter.decrement();
expect(await this.counter.current()).to.be.bignumber.equal('0');
});
});
});

View File

@ -1,26 +0,0 @@
const { contract } = require('@openzeppelin/test-environment');
require('@openzeppelin/test-helpers');
const ERC20MetadataMock = contract.fromArtifact('ERC20MetadataMock');
const { expect } = require('chai');
const metadataURI = 'https://example.com';
describe('ERC20Metadata', function () {
beforeEach(async function () {
this.token = await ERC20MetadataMock.new(metadataURI);
});
it('responds with the metadata', async function () {
expect(await this.token.tokenURI()).to.equal(metadataURI);
});
describe('setTokenURI', function () {
it('changes the original URI', async function () {
const newMetadataURI = 'https://betterexample.com';
await this.token.setTokenURI(newMetadataURI);
expect(await this.token.tokenURI()).to.equal(newMetadataURI);
});
});
});

View File

@ -1,203 +0,0 @@
const { accounts, contract } = require('@openzeppelin/test-environment');
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const ERC20SnapshotMock = contract.fromArtifact('ERC20SnapshotMock');
const { expect } = require('chai');
describe('ERC20Snapshot', function () {
const [ initialHolder, recipient, other ] = accounts;
const initialSupply = new BN(100);
beforeEach(async function () {
this.token = await ERC20SnapshotMock.new(initialHolder, initialSupply);
});
describe('snapshot', function () {
it('emits a snapshot event', async function () {
const { logs } = await this.token.snapshot();
expectEvent.inLogs(logs, 'Snapshot');
});
it('creates increasing snapshots ids, starting from 1', async function () {
for (const id of ['1', '2', '3', '4', '5']) {
const { logs } = await this.token.snapshot();
expectEvent.inLogs(logs, 'Snapshot', { id });
}
});
});
describe('totalSupplyAt', function () {
it('reverts with a snapshot id of 0', async function () {
await expectRevert(this.token.totalSupplyAt(0), 'ERC20Snapshot: id is 0');
});
it('reverts with a not-yet-created snapshot id', async function () {
await expectRevert(this.token.totalSupplyAt(1), 'ERC20Snapshot: nonexistent id');
});
context('with initial snapshot', function () {
beforeEach(async function () {
this.initialSnapshotId = new BN('1');
const { logs } = await this.token.snapshot();
expectEvent.inLogs(logs, 'Snapshot', { id: this.initialSnapshotId });
});
context('with no supply changes after the snapshot', function () {
it('returns the current total supply', async function () {
expect(await this.token.totalSupplyAt(this.initialSnapshotId)).to.be.bignumber.equal(initialSupply);
});
});
context('with supply changes after the snapshot', function () {
beforeEach(async function () {
await this.token.mint(other, new BN('50'));
await this.token.burn(initialHolder, new BN('20'));
});
it('returns the total supply before the changes', async function () {
expect(await this.token.totalSupplyAt(this.initialSnapshotId)).to.be.bignumber.equal(initialSupply);
});
context('with a second snapshot after supply changes', function () {
beforeEach(async function () {
this.secondSnapshotId = new BN('2');
const { logs } = await this.token.snapshot();
expectEvent.inLogs(logs, 'Snapshot', { id: this.secondSnapshotId });
});
it('snapshots return the supply before and after the changes', async function () {
expect(await this.token.totalSupplyAt(this.initialSnapshotId)).to.be.bignumber.equal(initialSupply);
expect(await this.token.totalSupplyAt(this.secondSnapshotId)).to.be.bignumber.equal(
await this.token.totalSupply()
);
});
});
context('with multiple snapshots after supply changes', function () {
beforeEach(async function () {
this.secondSnapshotIds = ['2', '3', '4'];
for (const id of this.secondSnapshotIds) {
const { logs } = await this.token.snapshot();
expectEvent.inLogs(logs, 'Snapshot', { id });
}
});
it('all posterior snapshots return the supply after the changes', async function () {
expect(await this.token.totalSupplyAt(this.initialSnapshotId)).to.be.bignumber.equal(initialSupply);
const currentSupply = await this.token.totalSupply();
for (const id of this.secondSnapshotIds) {
expect(await this.token.totalSupplyAt(id)).to.be.bignumber.equal(currentSupply);
}
});
});
});
});
});
describe('balanceOfAt', function () {
it('reverts with a snapshot id of 0', async function () {
await expectRevert(this.token.balanceOfAt(other, 0), 'ERC20Snapshot: id is 0');
});
it('reverts with a not-yet-created snapshot id', async function () {
await expectRevert(this.token.balanceOfAt(other, 1), 'ERC20Snapshot: nonexistent id');
});
context('with initial snapshot', function () {
beforeEach(async function () {
this.initialSnapshotId = new BN('1');
const { logs } = await this.token.snapshot();
expectEvent.inLogs(logs, 'Snapshot', { id: this.initialSnapshotId });
});
context('with no balance changes after the snapshot', function () {
it('returns the current balance for all accounts', async function () {
expect(await this.token.balanceOfAt(initialHolder, this.initialSnapshotId))
.to.be.bignumber.equal(initialSupply);
expect(await this.token.balanceOfAt(recipient, this.initialSnapshotId)).to.be.bignumber.equal('0');
expect(await this.token.balanceOfAt(other, this.initialSnapshotId)).to.be.bignumber.equal('0');
});
});
context('with balance changes after the snapshot', function () {
beforeEach(async function () {
await this.token.transfer(recipient, new BN('10'), { from: initialHolder });
await this.token.mint(recipient, new BN('50'));
await this.token.burn(initialHolder, new BN('20'));
});
it('returns the balances before the changes', async function () {
expect(await this.token.balanceOfAt(initialHolder, this.initialSnapshotId))
.to.be.bignumber.equal(initialSupply);
expect(await this.token.balanceOfAt(recipient, this.initialSnapshotId)).to.be.bignumber.equal('0');
expect(await this.token.balanceOfAt(other, this.initialSnapshotId)).to.be.bignumber.equal('0');
});
context('with a second snapshot after supply changes', function () {
beforeEach(async function () {
this.secondSnapshotId = new BN('2');
const { logs } = await this.token.snapshot();
expectEvent.inLogs(logs, 'Snapshot', { id: this.secondSnapshotId });
});
it('snapshots return the balances before and after the changes', async function () {
expect(await this.token.balanceOfAt(initialHolder, this.initialSnapshotId))
.to.be.bignumber.equal(initialSupply);
expect(await this.token.balanceOfAt(recipient, this.initialSnapshotId)).to.be.bignumber.equal('0');
expect(await this.token.balanceOfAt(other, this.initialSnapshotId)).to.be.bignumber.equal('0');
expect(await this.token.balanceOfAt(initialHolder, this.secondSnapshotId)).to.be.bignumber.equal(
await this.token.balanceOf(initialHolder)
);
expect(await this.token.balanceOfAt(recipient, this.secondSnapshotId)).to.be.bignumber.equal(
await this.token.balanceOf(recipient)
);
expect(await this.token.balanceOfAt(other, this.secondSnapshotId)).to.be.bignumber.equal(
await this.token.balanceOf(other)
);
});
});
context('with multiple snapshots after supply changes', function () {
beforeEach(async function () {
this.secondSnapshotIds = ['2', '3', '4'];
for (const id of this.secondSnapshotIds) {
const { logs } = await this.token.snapshot();
expectEvent.inLogs(logs, 'Snapshot', { id });
}
});
it('all posterior snapshots return the supply after the changes', async function () {
expect(await this.token.balanceOfAt(initialHolder, this.initialSnapshotId))
.to.be.bignumber.equal(initialSupply);
expect(await this.token.balanceOfAt(recipient, this.initialSnapshotId)).to.be.bignumber.equal('0');
expect(await this.token.balanceOfAt(other, this.initialSnapshotId)).to.be.bignumber.equal('0');
for (const id of this.secondSnapshotIds) {
expect(await this.token.balanceOfAt(initialHolder, id)).to.be.bignumber.equal(
await this.token.balanceOf(initialHolder)
);
expect(await this.token.balanceOfAt(recipient, id)).to.be.bignumber.equal(
await this.token.balanceOf(recipient)
);
expect(await this.token.balanceOfAt(other, id)).to.be.bignumber.equal(
await this.token.balanceOf(other)
);
}
});
});
});
});
});
});

View File

@ -1,154 +0,0 @@
const { contract } = require('@openzeppelin/test-environment');
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
const { MAX_INT256, MIN_INT256 } = constants;
const { expect } = require('chai');
const SignedSafeMathMock = contract.fromArtifact('SignedSafeMathMock');
describe('SignedSafeMath', function () {
beforeEach(async function () {
this.safeMath = await SignedSafeMathMock.new();
});
async function testCommutative (fn, lhs, rhs, expected) {
expect(await fn(lhs, rhs)).to.be.bignumber.equal(expected);
expect(await fn(rhs, lhs)).to.be.bignumber.equal(expected);
}
async function testFailsCommutative (fn, lhs, rhs, reason) {
await expectRevert(fn(lhs, rhs), reason);
await expectRevert(fn(rhs, lhs), reason);
}
describe('add', function () {
it('adds correctly if it does not overflow and the result is positive', async function () {
const a = new BN('1234');
const b = new BN('5678');
await testCommutative(this.safeMath.add, a, b, a.add(b));
});
it('adds correctly if it does not overflow and the result is negative', async function () {
const a = MAX_INT256;
const b = MIN_INT256;
await testCommutative(this.safeMath.add, a, b, a.add(b));
});
it('reverts on positive addition overflow', async function () {
const a = MAX_INT256;
const b = new BN('1');
await testFailsCommutative(this.safeMath.add, a, b, 'SignedSafeMath: addition overflow');
});
it('reverts on negative addition overflow', async function () {
const a = MIN_INT256;
const b = new BN('-1');
await testFailsCommutative(this.safeMath.add, a, b, 'SignedSafeMath: addition overflow');
});
});
describe('sub', function () {
it('subtracts correctly if it does not overflow and the result is positive', async function () {
const a = new BN('5678');
const b = new BN('1234');
const result = await this.safeMath.sub(a, b);
expect(result).to.be.bignumber.equal(a.sub(b));
});
it('subtracts correctly if it does not overflow and the result is negative', async function () {
const a = new BN('1234');
const b = new BN('5678');
const result = await this.safeMath.sub(a, b);
expect(result).to.be.bignumber.equal(a.sub(b));
});
it('reverts on positive subtraction overflow', async function () {
const a = MAX_INT256;
const b = new BN('-1');
await expectRevert(this.safeMath.sub(a, b), 'SignedSafeMath: subtraction overflow');
});
it('reverts on negative subtraction overflow', async function () {
const a = MIN_INT256;
const b = new BN('1');
await expectRevert(this.safeMath.sub(a, b), 'SignedSafeMath: subtraction overflow');
});
});
describe('mul', function () {
it('multiplies correctly', async function () {
const a = new BN('5678');
const b = new BN('-1234');
await testCommutative(this.safeMath.mul, a, b, a.mul(b));
});
it('multiplies by zero correctly', async function () {
const a = new BN('0');
const b = new BN('5678');
await testCommutative(this.safeMath.mul, a, b, '0');
});
it('reverts on multiplication overflow, positive operands', async function () {
const a = MAX_INT256;
const b = new BN('2');
await testFailsCommutative(this.safeMath.mul, a, b, 'SignedSafeMath: multiplication overflow');
});
it('reverts when minimum integer is multiplied by -1', async function () {
const a = MIN_INT256;
const b = new BN('-1');
await testFailsCommutative(this.safeMath.mul, a, b, 'SignedSafeMath: multiplication overflow');
});
});
describe('div', function () {
it('divides correctly', async function () {
const a = new BN('-5678');
const b = new BN('5678');
const result = await this.safeMath.div(a, b);
expect(result).to.be.bignumber.equal(a.div(b));
});
it('divides zero correctly', async function () {
const a = new BN('0');
const b = new BN('5678');
expect(await this.safeMath.div(a, b)).to.be.bignumber.equal('0');
});
it('returns complete number result on non-even division', async function () {
const a = new BN('7000');
const b = new BN('5678');
expect(await this.safeMath.div(a, b)).to.be.bignumber.equal('1');
});
it('reverts on division by zero', async function () {
const a = new BN('-5678');
const b = new BN('0');
await expectRevert(this.safeMath.div(a, b), 'SignedSafeMath: division by zero');
});
it('reverts on overflow, negative second', async function () {
const a = new BN(MIN_INT256);
const b = new BN('-1');
await expectRevert(this.safeMath.div(a, b), 'SignedSafeMath: division overflow');
});
});
});

View File

@ -1,26 +0,0 @@
const { contract } = require('@openzeppelin/test-environment');
const { constants } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const StringsMock = contract.fromArtifact('StringsMock');
describe('Strings', function () {
beforeEach(async function () {
this.strings = await StringsMock.new();
});
describe('from uint256', function () {
it('converts 0', async function () {
expect(await this.strings.fromUint256(0)).to.equal('0');
});
it('converts a positive number', async function () {
expect(await this.strings.fromUint256(4132)).to.equal('4132');
});
it('converts MAX_UINT256', async function () {
expect(await this.strings.fromUint256(constants.MAX_UINT256)).to.equal(constants.MAX_UINT256.toString());
});
});
});

View File

@ -1,172 +0,0 @@
const { accounts, contract } = require('@openzeppelin/test-environment');
const { BN, constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { expect } = require('chai');
const ERC20Mock = contract.fromArtifact('ERC20Mock');
const TokenVesting = contract.fromArtifact('TokenVesting');
describe('TokenVesting', function () {
const [ owner, beneficiary ] = accounts;
const amount = new BN('1000');
beforeEach(async function () {
// +1 minute so it starts after contract instantiation
this.start = (await time.latest()).add(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;
expect(cliffDuration).to.be.bignumber.that.is.at.least(duration);
await expectRevert(
TokenVesting.new(beneficiary, this.start, cliffDuration, duration, true, { from: owner }),
'TokenVesting: cliff is longer than duration'
);
});
it('reverts with a null beneficiary', async function () {
await expectRevert(
TokenVesting.new(ZERO_ADDRESS, this.start, this.cliffDuration, this.duration, true, { from: owner }),
'TokenVesting: beneficiary is the zero address'
);
});
it('reverts with a null duration', async function () {
// cliffDuration should also be 0, since the duration must be larger than the cliff
await expectRevert(
TokenVesting.new(beneficiary, this.start, 0, 0, true, { from: owner }), 'TokenVesting: duration is 0'
);
});
it('reverts if the end time is in the past', async function () {
const now = await time.latest();
this.start = now.sub(this.duration).sub(time.duration.minutes(1));
await expectRevert(
TokenVesting.new(beneficiary, this.start, this.cliffDuration, this.duration, true, { from: owner }),
'TokenVesting: final time is before current time'
);
});
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 ERC20Mock.new(this.vesting.address, amount);
});
it('can get state', async function () {
expect(await this.vesting.beneficiary()).to.equal(beneficiary);
expect(await this.vesting.cliff()).to.be.bignumber.equal(this.start.add(this.cliffDuration));
expect(await this.vesting.start()).to.be.bignumber.equal(this.start);
expect(await this.vesting.duration()).to.be.bignumber.equal(this.duration);
expect(await this.vesting.revocable()).to.be.equal(true);
});
it('cannot be released before cliff', async function () {
await expectRevert(this.vesting.release(this.token.address),
'TokenVesting: no tokens are due'
);
});
it('can be released after cliff', async function () {
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,
amount: await this.token.balanceOf(beneficiary),
});
});
it('should release proper amount after cliff', async function () {
await time.increaseTo(this.start.add(this.cliffDuration));
await this.vesting.release(this.token.address);
const releaseTime = await time.latest();
const releasedAmount = amount.mul(releaseTime.sub(this.start)).div(this.duration);
expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(releasedAmount);
expect(await this.vesting.released(this.token.address)).to.be.bignumber.equal(releasedAmount);
});
it('should linearly release tokens during vesting period', async function () {
const vestingPeriod = this.duration.sub(this.cliffDuration);
const checkpoints = 4;
for (let i = 1; i <= checkpoints; i++) {
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.sub(this.start)).div(this.duration);
expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(expectedVesting);
expect(await this.vesting.released(this.token.address)).to.be.bignumber.equal(expectedVesting);
}
});
it('should have released all after end', async function () {
await time.increaseTo(this.start.add(this.duration));
await this.vesting.release(this.token.address);
expect(await this.token.balanceOf(beneficiary)).to.be.bignumber.equal(amount);
expect(await this.vesting.released(this.token.address)).to.be.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 });
expect(await this.vesting.revoked(this.token.address)).to.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 expectRevert(vesting.revoke(this.token.address, { from: owner }),
'TokenVesting: cannot revoke'
);
});
it('should return the non-vested tokens when revoked by owner', async function () {
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);
await this.vesting.revoke(this.token.address, { from: owner });
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal(amount.sub(vested));
});
it('should keep the vested tokens when revoked by owner', async function () {
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);
await this.vesting.revoke(this.token.address, { from: owner });
const vestedPost = vestedAmount(amount, await time.latest(), this.start, this.cliffDuration, this.duration);
expect(vestedPre).to.be.bignumber.equal(vestedPost);
});
it('should fail to be revoked a second time', async function () {
await this.vesting.revoke(this.token.address, { from: owner });
await expectRevert(this.vesting.revoke(this.token.address, { from: owner }),
'TokenVesting: token already revoked'
);
});
function vestedAmount (total, now, start, cliffDuration, duration) {
return (now.lt(start.add(cliffDuration))) ? new BN(0) : total.mul((now.sub(start))).div(duration);
}
});
});